django 如何传递位置参数PK跳转页面(NoReverseMatch at / Reverse for 'post.views.blog' not found. 等问题解决)

问题一:NoReverseMatch at /

Reverse for 'post.views.blog' not found. 'post.views.blog' is not a valid view function or pattern name

问题二:NoReverseMatch at /

NameError at /detail/post-1/ name 'post' is not defined

 

一、主app(Mysite)下

url.py

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


urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'', include('Post.urls',namespace="Post")),#主要注意这个
]

二、自己建立的app(Post)

url.py

from django.conf.urls import *
from . import views



app_name = 'Post'   # 这里是为了url反向解析用


urlpatterns = [
    url(r'^$', views.index, name="index"),
    url(r'^detail/post-(?P[0-9]+)/$', views.detail,name="detail"),#注意views,name后面跟的数值
]

views.py

from django.shortcuts import render, get_object_or_404



def detail(request, pk):
    """
    :param request:
    :param pk: 接收到的文章的主键id
    :return:
    """
    return render(request, 'Post/blog.html',{'post':detail}) #注意{'post':detail}中冒号后边的值

html中的跳转的链接(注意url后面跟的'Post:detail'),Post是app名称detail是views.py中的detail函数

Read more 

 

你可能感兴趣的:(django框架,django之搭建博客,url页面跳转)