Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/ Using the URLconf defined in question_post.urls, Django tried these URL patterns, in this order:
1.admin/ 2.signup/ [name='signup'] 3.login/ [name='login'] 4.logout/ [name='logout'] 5.post_question/ [name='post_question'] 6.question_list/ [name='question_list'] 7.question/<int:question_id>/ [name='question_detail'] 8.question/<int:question_id>/post_answer/ [name='post_answer'] 9.answer/<int:answer_id>/like/ [name='like_answer'] 10.question_detail/<int:question_id>/ [name='question_detail'] The empty path didn’t match any of these. Below are my django project files. models.py --------- from django.db import models from django.contrib.auth.models import User class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) # Remove the username field from here, as it belongs to the User model. first_name = models.CharField(max_length=100, blank=True, null=True) last_name = models.CharField(max_length=100, blank=True, null=True) email = models.EmailField(blank=True, null=True) profile_picture = models.ImageField( upload_to='profile_pics/', blank=True, null=True) def __str__(self): return self.user.username class Question(models.Model): user = models.ForeignKey(UserProfile, on_delete=models.CASCADE) text = models.TextField() created_at = models.DateTimeField(auto_now_add=True) class Answer(models.Model): user = models.ForeignKey(UserProfile, on_delete=models.CASCADE) question = models.ForeignKey(Question, on_delete=models.CASCADE) text = models.TextField() created_at = models.DateTimeField(auto_now_add=True) class Like(models.Model): user = models.ForeignKey(UserProfile, on_delete=models.CASCADE) answer = models.ForeignKey(Answer, on_delete=models.CASCADE) views.py -------- from django.shortcuts import render, redirect from django.contrib.auth.mixins import LoginRequiredMixin from django.views import View from django.urls import reverse_lazy from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from .models import Question, Answer, Like, UserProfile from .forms import QuestionForm, AnswerForm, LikeForm, CustomUserCreationForm class SignupView(View): def get(self, request): form = UserCreationForm() return render(request, 'signup.html', {'form': form}) def post(self, request): form = UserCreationForm(request.POST) if form.is_valid(): form.save() return redirect('login') return render(request, 'signup.html', {'form': form}) class UserLoginView(View): def get(self, request): form = AuthenticationForm() return render(request, 'login.html', {'form': form}) def post(self, request): form = AuthenticationForm(request, data=request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) if user is not None: login(request, user) # Redirect to the home page on successful login return redirect('home') return render(request, 'login.html', {'form': form}) class UserLogoutView(View): def get(self, request): logout(request) return redirect('login') class QuestionPostView(LoginRequiredMixin, View): login_url = reverse_lazy('login') def get(self, request): form = QuestionForm() return render(request, 'post_question.html', {'form': form}) def post(self, request): form = QuestionForm(request.POST) if form.is_valid(): new_question = form.save(commit=False) new_question.user = request.user new_question.save() return redirect('question_list') return render(request, 'post_question.html', {'form': form}) class QuestionListView(View): def get(self, request): questions = Question.objects.all() return render(request, 'question_list.html', {'questions': questions}) class AnswerPostView(LoginRequiredMixin, View): login_url = reverse_lazy('login') def get(self, request, question_id): question = Question.objects.get(pk=question_id) form = AnswerForm() return render(request, 'post_answer.html', {'question': question, 'form': form}) def post(self, request, question_id): question = Question.objects.get(pk=question_id) form = AnswerForm(request.POST) if form.is_valid(): new_answer = form.save(commit=False) new_answer.user = request.user.userprofile new_answer.question = question new_answer.save() return redirect('question_detail', question_id=question.id) return render(request, 'post_answer.html', {'question': question, 'form': form}) class LikeAnswerView(LoginRequiredMixin, View): login_url = reverse_lazy('login') def post(self, request, answer_id): answer = Answer.objects.get(pk=answer_id) Like.objects.create(user=request.user, answer=answer) return redirect('question_detail', question_id=answer.question.id) class QuestionDetailView(View): def get(self, request, question_id): question = Question.objects.get(pk=question_id) answers = Answer.objects.filter(question=question) return render(request, 'question_detail.html', {'question': question, 'answers': answers}) urls.py ------- from django.urls import path from . import views urlpatterns = [ path('signup/', views.SignupView.as_view(), name='signup'), path('login/', views.UserLoginView.as_view(), name='login'), path('logout/', views.UserLogoutView.as_view(), name='logout'), path('post_question/', views.QuestionPostView.as_view(), name='post_question'), path('question_list/', views.QuestionListView.as_view(), name='question_list'), path('question/<int:question_id>/', views.QuestionDetailView.as_view(), name='question_detail'), path('question/<int:question_id>/post_answer/', views.AnswerPostView.as_view(), name='post_answer'), path('answer/<int:answer_id>/like/', views.LikeAnswerView.as_view(), name='like_answer'), path('question_detail/<int:question_id>/', views.QuestionDetailView.as_view(), name='question_detail'), ] project/urls.py ---------------- from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('question_app.urls')), ] -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB11hN_wTaRpR%3DJb%2B4wy_NuyS-0MWXARKnLB6CHOEZVmdv%3DFCQ%40mail.gmail.com.