I need your helf, I got this below error, after post the django server 8000 on google browser. Please correct my django project code?

63 views
Skip to first unread message

AKHIL KORE

unread,
Jul 24, 2023, 1:12:33 AM7/24/23
to django...@googlegroups.com
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')),
]

Fikayo Soetan

unread,
Jul 24, 2023, 2:20:50 AM7/24/23
to Django users
Good day,
In the project urlpatterns it's meant to be ""(two quotation marks) instead of "(one).
Also, in the app you have not configured any path to handle the  http://127.0.0.1:8000/. What you have is for the other views like signup and all. You need a base one like path("", ...
Adjust that and see if it works.

Ryan Nowakowski

unread,
Jul 24, 2023, 7:25:45 AM7/24/23
to django...@googlegroups.com
You haven't defined any URL pattern for '/'. What do you want to happen when the user goes to '/'?

James Kalu

unread,
Jul 24, 2023, 7:44:26 AM7/24/23
to django...@googlegroups.com
You should define something for your home URL or call it the index URL. That is like an application entry point.
In the question_app URLs, you should have at least one URL like this;
       path('', views.index.as_view(), name='index'),   
-- just an example --

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/24EEE790-22B6-4EA8-9C7F-1CC5C75F4908%40fattuba.com.

Real Enlight

unread,
Jul 25, 2023, 2:14:07 PM7/25/23
to django...@googlegroups.com
Well Ryan thank you for sending the complete code. I think that in your `projects/urls.py` file you have defined where the user should go when it goes to `/` .i.e. it is redirected to ``` question_app.urls.``` but you have not defined here any default page for the link. I think you should renamed url_pattern for the page you want the user to visit from just ```   path('question_list/', views.QuestionListView.as_view(), name='question_list'),``` to path('' ,  views.QuestionListView.as_view(), name='question_list'). In this way the user will see the question lists when it goes to '/' . 

--

ivan harold

unread,
Aug 9, 2023, 10:06:09 AM8/9/23
to Django users
You haven't defined any URL pattern for '/'. What do you want to happen when the user goes to '/'
for ex: Request URL: http://127.0.0.1:8000/ index
Reply all
Reply to author
Forward
0 new messages