本文介绍了为什么我的urls.py不能与Django一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,当我用Building_a_blog_in_30_mins_with_Django_Screencast编写我的博客时,我遇到了一些问题。当我点击文章的标题时,它不会出现正确的页面。

 找不到页面(404)
请求方法:GET

使用myblog.urls中定义的URLconf,Django按照以下顺序尝试了这些URL模式:

  ^ $ 
^(?P< pk> \d +)/ $
^ admin /
^ admin / doc /

当前的URL,app / 1与这些中的任何一个都不匹配。



您看到此错误,因为您的Django设置文件中有DEBUG = True。将其更改为False,Django将显示标准的404页面。



这是我的urls.py:

  url(r'^ $',ListView.as_view(queryset = Post.objects.all()。order_by(created [:2] template_name =blog.html)) 
url(r'^(?P< pk> \d +)/ $',DetailView.as_view(model = Post,template_name =post.html)),
url(r' admin /',include(admin.site.urls)),
url(r'^ admin / doc /',include('django.contrib.admindocs.urls'))

我不会知道发生了什么事情。请帮助,谢谢!

解决方案

您需要在您的网址中添加'app /'



Detail $。$($)$($)$($)$($) ,

或者您可能需要在 urls.py 你的应用程序(名称d 应用程序?),并将其包含在网站main urls.py

  url(r'app /',include('app.urls'))


Today when I code my blog with 'Building_a_blog_in_30_mins_with_Django_Screencast',I meet some problems.When I click the title with the article,it can't appear the right page.!

Page not found (404)
Request Method:     GET

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

^$
^(?P<pk>\d+)/$
^admin/
^admin/doc/

The current URL, app/1, 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.

and it's my urls.py:

url(r'^$',ListView.as_view(queryset=Post.objects.all().order_by("created[:2]template_name="                                                                               blog.html")), 
url(r'^(?P<pk>\d+)/$',DetailView.as_view( model=Post, template_name="post.html")),
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/doc/', include('django.contrib.admindocs.urls'))

I doesn't konw what is going on with it.Please help,thanks!

解决方案

You need to add 'app/' in your url.

url(r'^app/(?P<pk>\d+)/$',DetailView.as_view( model=Post, template_name="post.html")),

Or may be you need to define these urls in urls.py of your app (named app ?) and include it in sites main urls.py

url(r'app/', include('app.urls'))

这篇关于为什么我的urls.py不能与Django一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 22:22