简体   繁体   English

如何解决 Django url 模式不起作用

[英]how to solve Django url patterns not working

I added path('int:pk/',...) in urls.py and access 'http://127.0.0.1:8000/blog/1'我在urls.py 中添加了 path('int:pk/',...) 并访问了 'http://127.0.0.1:8000/blog/1'

the result was结果是

'page not found(404)' '找不到页面(404)'
Using the URLconf defined in doit.urls, Django tried these URL patterns, in this order:使用 doit.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

  1. blog <int:pk>/博客 <int:pk>/
  2. blog博客
  3. admin/行政/

The current path, blog/1, didn't match any of these.当前路径 blog/1 与其中任何一个都不匹配。

also, I made 3 pk contents另外,我制作了 3 包内容

please help me I suffered for a long time.请帮助我我受苦了很长时间。

urls.py网址.py

from django.urls import path
from . import views

urlpatterns = [
    path('<int:pk>/', views.PostDetail.as_view()),
    path('', views.PostList.as_view()),
] 

models.py模型.py

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=30)
    content = models.TextField()

    created_at = models.DateTimeField()


    def __str__(self):
        return f'[{self.pk}]{self.title}'

views.py视图.py

from django.views.generic import ListView, DetailView
from .models import Post 


class PostList(ListView):
    model = Post
    ordering = '-pk'

class PostDetail(DetailView):
    model = Post

post_list.html post_list.html

<!DOCTYPE html>
<html lang = "ko">
<head>
    <meta charset="UTF-8">
    <title>Blog</title>
</head>
<body>
    
    <h1>Blog</h1>

{% for p in post_list %}
    <hr/>
    <h2> {{ p.title }} </h2>
    <p> {{ p.content }}</p>
    <h4> {{ p.created_at }} </h4> 

{% endfor %}
</body>
</html>

post_detail.html post_detail.html

<!DOCTYPE html>
<html lang="ko">
<head>

    <meta charset = "UTF-8">
    <title> {{ post.title }} - Blog </title>
</head>
<body>
    <nav>
        <a href="/blog/">Blog</a>
    </nav>

    <h1> {{ post.title }} </h1>
    <h4> {{ post.created_at }} </h4>
    <p> {{ post.content }} </p>
    <hr/>

    <h3> ... </h3>
</body>
</html>

Try this way, please请试试这个方法

urlpatterns = [
    path('', views.PostList.as_view()),
    path('post/<int:pk>/', views.PostDetail.as_view()),
] 

I don't know how you architected your project, but I'm assuming that you have created a separate urls.py file inside your app, if you did so, you are doing some things wrong in the code that you provided in this question.我不知道你是如何构建你的项目的,但我假设你已经在你的应用程序中创建了一个单独的 urls.py 文件,如果你这样做了,你在这个问题中提供的代码中做错了一些事情. You are not declaring the app_name in your urls.py file, this makes it really hard for you to create links for your pages later on when you are expanding your project, and also makes it impossible for you to access pages, I'm going to give you some code, but make sure that you read it, and adapt it to your project.你没有在你的 urls.py 文件中声明app_name ,这让你在扩展你的项目时很难为你的页面创建链接,也让你无法访问页面,我要去给你一些代码,但确保你阅读它,并适应你的项目。

This should be your urls.py in the project folder:这应该是项目文件夹中的 urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('your_app_name.urls'))
]

After doing that, you should create an urls.py file inside your app folder, and make it look like this:之后,您应该在您的应用程序文件夹中创建一个 urls.py 文件,并使其看起来像这样:

from django.urls import path
from .views import *

app_name = "your_app_name"

urlpatterns = [
    path('<int:pk>/', PostDetail.as_view(), name='post_detail'),
    path('', PostList.as_view(), name='post_list'),
]

Note that I changed the way that you are importing the views, this way you don't need to write views.PostDetail.as_view() , you can just call it by the class name.请注意,我更改了您导入视图的方式,这样您就不需要编写views.PostDetail.as_view() ,您只需通过类名调用它即可。 I also added a name for each URL pattern, with this you can create links way easier like I mentioned before, you create an a tag in HTML that will look like that:我还为每个 URL 模式添加了一个名称,这样您就可以像我之前提到的那样更轻松地创建链接,您可以在 HTML 中创建一个如下所示的标签:

<a href='{% url 'your_app_name:post_list %}'>Blog</a>

By applying those changes to your code, it will probably work fine, but if it doesn't, feel free to contact me.通过将这些更改应用于您的代码,它可能会正常工作,但如果没有,请随时与我联系。 Also try to learn about function based views in Django, they are better to use, since with class based views, you break the DRY(don't repeat yourself) concept of Django.还尝试了解 Django 中基于函数的视图,它们更好用,因为使用基于类的视图,您打破了 Django 的 DRY(不要重复自己)概念。

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

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