Django、urls.py、include 似乎不起作用

发布于 2024-12-09 08:22:38 字数 1826 浏览 1 评论 0原文

我试图在我的主网址中包含一个额外的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一片旧的回忆 2024-12-16 08:22:38

问题似乎出在 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

半仙 2024-12-16 08:22:38
from django.contrib import admin,include
admin.autodiscover()

urlpatterns = patterns('',
        (r'^pnasser/',include('pnasser.urls')),
        (r'^admin/',include(admin.site.urls)),
        (r'^',include('pnasser.urls')),
)

也许你错过了第一行中的“include”

from django.contrib import admin,include
admin.autodiscover()

urlpatterns = patterns('',
        (r'^pnasser/',include('pnasser.urls')),
        (r'^admin/',include(admin.site.urls)),
        (r'^',include('pnasser.urls')),
)

maybe you missed "include" in the first line

囚我心虐我身 2024-12-16 08:22:38

问题是您使用空正则表达式("^" 将匹配任何内容,包括空 url)来处理 include 指令。如果您这样做,它总是会在您的请求路径处附加第一个斜杠。考虑到您的 pnasser.urls 上不包含“/”的正则表达式,因此与 mysite.com 上的请求不匹配。

如果您希望 mysite.com 或 mysite.com/ 带您进入 pnasser“索引”视图,您需要有类似的内容:

from django.contrib import admin

from pnasser.views import index

admin.autodiscover()


urlpatterns = patterns('',
    (r'^/?

所以,您有:

  • mysite.com => pnasser.views.index
  • mysite.com/ => pnasser.views.index
  • mysite.com/admin =>管理页面
  • mysite.com/pnasser/ => pnasser.views.index
  • mysite.com/pnasser/home => pnasser.views.home

如果这不起作用,请确保您有 安装了 Django 的 CommonMiddleware,并让你有 APPEND_SLASH = True (这是默认设置,所以如果你在您的 settings.py 文件中找不到它)。

, index), (r'^pnasser/',include('pnasser.urls')), (r'^admin/',include(admin.site.urls)), )

所以,您有:

  • mysite.com => pnasser.views.index
  • mysite.com/ => pnasser.views.index
  • mysite.com/admin =>管理页面
  • mysite.com/pnasser/ => pnasser.views.index
  • mysite.com/pnasser/home => pnasser.views.home

如果这不起作用,请确保您有 安装了 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:

from django.contrib import admin

from pnasser.views import index

admin.autodiscover()


urlpatterns = patterns('',
    (r'^/?

So, you have:

  • mysite.com => pnasser.views.index
  • mysite.com/ => pnasser.views.index
  • mysite.com/admin => admin page
  • mysite.com/pnasser/ => pnasser.views.index
  • mysite.com/pnasser/home => pnasser.views.home

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).

, index), (r'^pnasser/',include('pnasser.urls')), (r'^admin/',include(admin.site.urls)), )

So, you have:

  • mysite.com => pnasser.views.index
  • mysite.com/ => pnasser.views.index
  • mysite.com/admin => admin page
  • mysite.com/pnasser/ => pnasser.views.index
  • mysite.com/pnasser/home => pnasser.views.home

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).

灰色世界里的红玫瑰 2024-12-16 08:22:38
Using the URLconf defined in mysite.urls, Django tried these
URL patterns, in this order:

此错误消息应列出所有可能的 URL,包括 pnasser 应用程序中的“扩展”URL。由于您仅从主 urls.py 获取网址,因此表明您尚未在 settings.py 的 INSTALLED_APPS 中正确启用 pnasser 应用。

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

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's INSTALLED_APPS.

記柔刀 2024-12-16 08:22:38

在 URL 文件中,您可以编写如下内容。 += 显然允许我们向“urlpatterns”变量添加额外的“模式”,这也意味着我们可以将它们包装在“if”语句中。

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
)

urlpatterns += patterns('',
    (r'^api/', include('api.urls')),
)

if settings.SHOP_TOGGLE:
    urlpatterns += patterns('',
        (r'^shop/', include('shop.urls')),)
    )

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.

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
)

urlpatterns += patterns('',
    (r'^api/', include('api.urls')),
)

if settings.SHOP_TOGGLE:
    urlpatterns += patterns('',
        (r'^shop/', include('shop.urls')),)
    )
话少心凉 2024-12-16 08:22:38

以下对我有用:

如果您的网址无法正常工作,您可能需要添加此行
到位置/:

fastcgi_split_path_info ^()(.*)$;

来自: https://code.djangoproject.com/wiki/DjangoAndNginx

The following works for me:

If your urls are not working correctly, you may need to add this line
to location /:

fastcgi_split_path_info ^()(.*)$;

From: https://code.djangoproject.com/wiki/DjangoAndNginx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文