简体   繁体   English

无法呈现模板,找不到页面 (404) Django

[英]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:(我似乎找不到错误,网站工作正常但是当我请求http://127.0.0.1:8000/test我得到 404 错误,我觉得很愚蠢,我真的被困在这个问题上,请帮助:(

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})

You have a trailing slash in urls.py but not in the URL you are calling.您在urls.py中有一个斜杠,但在您正在调用的 URL 中没有。

Either change urls.py like this:像这样更改urls.py

...
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或致电http://127.0.0.1:8000/test/ <-- 注意:添加斜线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM