Django、urls.py、include 似乎不起作用
我试图在我的主网址中包含一个额外的 urls.py - 但它似乎不起作用。我已经进行了大量搜索,但似乎无法弄清楚
主 urls.py 文件 - 管理工作正常
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^pnasser/',include('pnasser.urls')),
(r'^admin/',include(admin.site.urls)),
(r'^',include('pnasser.urls')),
)
,然后我有一个文件夹 pnasser,其中文件 urls.py 包含以下内容:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('pnasser.views',
(r'^$','index'),
(r'^login/$','login'),
(r'^signup/$','signup'),
(r'^insertaccount/$','insertaccount'),
(r'^home/$','home'),
(r'^update/(?P<accid>\d+)','update'),
(r'^history/(?P<accid>\d+)','account_history'),
(r'^logout/(?P<accid>\d+)','logout'),
)
我不确定如果我可能在配置中遗漏了其他东西。如果我访问 mysite.com/admin 它会正确加载管理,如果我转到 mysite 或视图中的任何其他 url,我会收到 404 页面未找到:
使用 mysite.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,按以下顺序: 1.^pnasser/ 2. ^管理/
当前 URL 与其中任何一个都不匹配。
编辑 settings.py 安装的应用程序:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'pnasser',
)
更新 2
因此,我还尝试通过开发服务器运行我的网站:python manage.py runserver 0.0.0.0:8000< /code> 这有效。我假设使用 mod_wsgi 与 apache 集成的某个地方有问题。但是,我不确定问题出在哪里
I'm trying to include an additional urls.py inside my main urls - however it doesn't seem to be working. I've done a bunch of searching and I can't seem to figure it out
main urls.py file - the admin works fine
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^pnasser/',include('pnasser.urls')),
(r'^admin/',include(admin.site.urls)),
(r'^',include('pnasser.urls')),
)
I then have a folder pnasser, with the file urls.py with the following:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('pnasser.views',
(r'^
I'm not sure if I'm maybe missing something else in the configuration. if I visit mysite.com/admin it loads the admin correctly, if I goto mysite or any other url in the views I get 404 page not found:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1. ^pnasser/
2. ^admin/
The current URL, , didn't match any of these.
edit settings.py installed apps:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'pnasser',
)
Update 2
So, I also tried running my site via the dev server: python manage.py runserver 0.0.0.0:8000
this works. I'm assuming somewhere in my integration with apache using mod_wsgi is the problem. However, I'm not sure where the problem would be
,'index'),
(r'^login/
I'm not sure if I'm maybe missing something else in the configuration. if I visit mysite.com/admin it loads the admin correctly, if I goto mysite or any other url in the views I get 404 page not found:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1. ^pnasser/
2. ^admin/
The current URL, , didn't match any of these.
edit settings.py installed apps:
Update 2
So, I also tried running my site via the dev server: python manage.py runserver 0.0.0.0:8000
this works. I'm assuming somewhere in my integration with apache using mod_wsgi is the problem. However, I'm not sure where the problem would be
,'login'),
(r'^signup/
I'm not sure if I'm maybe missing something else in the configuration. if I visit mysite.com/admin it loads the admin correctly, if I goto mysite or any other url in the views I get 404 page not found:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1. ^pnasser/
2. ^admin/
The current URL, , didn't match any of these.
edit settings.py installed apps:
Update 2
So, I also tried running my site via the dev server: python manage.py runserver 0.0.0.0:8000
this works. I'm assuming somewhere in my integration with apache using mod_wsgi is the problem. However, I'm not sure where the problem would be
,'signup'),
(r'^insertaccount/
I'm not sure if I'm maybe missing something else in the configuration. if I visit mysite.com/admin it loads the admin correctly, if I goto mysite or any other url in the views I get 404 page not found:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1. ^pnasser/
2. ^admin/
The current URL, , didn't match any of these.
edit settings.py installed apps:
Update 2
So, I also tried running my site via the dev server: python manage.py runserver 0.0.0.0:8000
this works. I'm assuming somewhere in my integration with apache using mod_wsgi is the problem. However, I'm not sure where the problem would be
,'insertaccount'),
(r'^home/
I'm not sure if I'm maybe missing something else in the configuration. if I visit mysite.com/admin it loads the admin correctly, if I goto mysite or any other url in the views I get 404 page not found:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1. ^pnasser/
2. ^admin/
The current URL, , didn't match any of these.
edit settings.py installed apps:
Update 2
So, I also tried running my site via the dev server: python manage.py runserver 0.0.0.0:8000
this works. I'm assuming somewhere in my integration with apache using mod_wsgi is the problem. However, I'm not sure where the problem would be
,'home'),
(r'^update/(?P<accid>\d+)','update'),
(r'^history/(?P<accid>\d+)','account_history'),
(r'^logout/(?P<accid>\d+)','logout'),
)
I'm not sure if I'm maybe missing something else in the configuration. if I visit mysite.com/admin it loads the admin correctly, if I goto mysite or any other url in the views I get 404 page not found:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1. ^pnasser/
2. ^admin/The current URL, , didn't match any of these.
edit settings.py installed apps:
Update 2
So, I also tried running my site via the dev server: python manage.py runserver 0.0.0.0:8000
this works. I'm assuming somewhere in my integration with apache using mod_wsgi is the problem. However, I'm not sure where the problem would be
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题似乎出在 django.wsgi 文件中,以及标准 django.wsgi 文件加载 python 站点的方式与开发服务器加载站点的方式的差异。我想这是一个众所周知的问题,但我不知道。谢谢大家的建议。
替代的 django.wsgi 文件在这里找到: http ://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
The problem seemed to be in the django.wsgi file - and the differences in how the standard django.wsgi file loads a python site vs how the development server loads the site. I guess it's a well known issue, that I was unaware of. Thanks everyone for the suggestions.
Alternative django.wsgi file found here: http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
也许你错过了第一行中的“include”
maybe you missed "include" in the first line
问题是您使用空正则表达式(
"^"
将匹配任何内容,包括空 url)来处理 include 指令。如果您这样做,它总是会在您的请求路径处附加第一个斜杠。考虑到您的 pnasser.urls 上不包含“/”的正则表达式,因此与 mysite.com 上的请求不匹配。如果您希望 mysite.com 或 mysite.com/ 带您进入 pnasser“索引”视图,您需要有类似的内容:
所以,您有:
如果这不起作用,请确保您有 安装了 Django 的 CommonMiddleware,并让你有
APPEND_SLASH = True
(这是默认设置,所以如果你在您的 settings.py 文件中找不到它)。The problem is that you are using an empty regex (
"^"
will match anything, including an empty url) to handle an include directive. If you do that, it will always append a first slash at your request path. Considering that on your pnasser.urls does not contain a regex for "/", there is no match for a request on mysite.com.If you want mysite.com or mysite.com/ to take you to pnasser "index" view, you need to have something like:
So, you have:
If this doesn't work, make sure that you have Django's CommonMiddleware installed, and make you have
APPEND_SLASH = True
(it is the default, so you shouldn't need to mess with this if you don't find it in your settings.py file).此错误消息应列出所有可能的 URL,包括 pnasser 应用程序中的“扩展”URL。由于您仅从主 urls.py 获取网址,因此表明您尚未在 settings.py 的
INSTALLED_APPS
中正确启用pnasser
应用。This error message should list all possible URLs, including the 'expanded' urls from your pnasser app. Since you're only getting the URLs from your main urls.py, it suggests you haven't properly enabled the
pnasser
app in settings.py'sINSTALLED_APPS
.在 URL 文件中,您可以编写如下内容。 += 显然允许我们向“urlpatterns”变量添加额外的“模式”,这也意味着我们可以将它们包装在“if”语句中。
Within your URL's file you can write something like the following below. += obviously allows us to add additional 'patterns' to our 'urlpatterns' variable, this also means we can wrap these in an 'if' statement.
以下对我有用:
来自: https://code.djangoproject.com/wiki/DjangoAndNginx
The following works for me:
From: https://code.djangoproject.com/wiki/DjangoAndNginx