kingmathers92
kingmathers92

Reputation: 5

Can't render template, getting page not found (404) Django

I can't seem to find the error, site works fine but when I request http://127.0.0.1:8000/test I get the 404 error, I feel so stupid I'm really stuck on this, please help:(

Error:


Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/test

Using the URLconf defined in project4.urls, Django tried these URL patterns, in this order:

    admin/
    [name='index']
    login [name='login']
    logout [name='logout']
    register [name='register']
    posts-json/ [name='posts-view-json']
    ^network/static/(?P<path>.*)$
    ^media/(?P<path>.*)$

The current path, test, didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

This is my urls


from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

from . import views
from .views import post_view_json, profile_test_view


urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("test/", profile_test_view, name="test"),

    # endpoints
    path("posts-json/", post_view_json, name="posts-view-json")
]


urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

It's a social website and I'm trying to render the posts and user's profiles on a page.

My views

from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.http.response import JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.core import serializers

from .models import User, Post, Profile

def index(request):
    qs = Post.objects.all()
    context = {
        'hello': 'hello world!',
        'qs':qs,
    }
    return render(request, "network/index.html", context)


def post_view_json(request):
    qs = Post.objects.all()
    data = serializers.serialize('json', qs)
    return JsonResponse({'data': data})

def profile_test_view(request):
    profile = Profile.objects.get(user=request.user)
    return render(request, "network/profile_test.html", {'profile':profile})

Upvotes: 0

Views: 933

Answers (1)

dacx
dacx

Reputation: 844

You have a trailing slash in urls.py but not in the URL you are calling.

Either change urls.py like this:

...
urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("test", profile_test_view, name="test"), # <-- NOTE: I removed the slash here

    # endpoints
    path("posts-json/", post_view_json, name="posts-view-json")
]
...

or call http://127.0.0.1:8000/test/ <-- NOTE: slash added

Upvotes: 1

Related Questions