Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不是';django url中捕获的t变量作为字符串传递给视图?_Django_Python 3.x_Django Views_Url Pattern - Fatal编程技术网

为什么不是';django url中捕获的t变量作为字符串传递给视图?

为什么不是';django url中捕获的t变量作为字符串传递给视图?,django,python-3.x,django-views,url-pattern,Django,Python 3.x,Django Views,Url Pattern,我试图在我的url.py文件中捕获url中的变量,然后将其传递给我的视图文件中的函数,在该文件中我希望将其用作字符串: # urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^posts/(?P<title>)', views.blog_ronn_post, name='blog_ronn_post'), ] # views.py from django.h

我试图在我的url.py文件中捕获url中的变量,然后将其传递给我的视图文件中的函数,在该文件中我希望将其用作字符串:

# urls.py
from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^posts/(?P<title>)', views.blog_ronn_post, name='blog_ronn_post'),
]

# views.py
from django.http import HttpResponse

def blog_ronn_post(request, title):
    html = "<html><body>This is the page for the blog titled: {s} </body></html>".format(title)
    return HttpResponse(html)
#url.py
从django.conf.url导入url
从…起导入视图
URL模式=[
url(r'^posts/(?P'),views.blog_ronn_post,name='blog_ronn_post'),
]
#views.py
从django.http导入HttpResponse
def博客文章(请求、标题):
html=“这是标题为:{s}的博客页面”。格式(标题)
返回HttpResponse(html)
然而,这不起作用。在我的开发浏览器中,html将显示,但在最后一个冒号后不显示任何内容。我不认为我的Python 3语法有问题,好像我试图在格式括号中使用字符串,例如:

 html = "<html><body>This is the page for the blog titled: {s} </body></html>".format('Title 1')
html=“这是标题为:{s}的博客页面。格式('Title 1'))

它工作,显示“…标题:标题1”。那么为什么它不通过我的标题字符串呢?我知道title是作为字符串从url传递到视图的,所以应该没有问题。刚到django,如果我犯了一个明显的错误,请道歉

因为你没有给它任何匹配的东西

r'^posts/(?P<title>\w+)$'
r'^posts/(?P\w+)$'

因为你没有给它任何匹配的东西

r'^posts/(?P<title>\w+)$'
r'^posts/(?P\w+)$'