django:404(main.urls不包含在myproject / urls.py中?)

时间:2015-05-10 22:18:16

标签: python django django-cms

我有以下问题:

我做了一个django(1.7.8)项目(名为 djangocmstest )来测试django-cms(但它可能与django有关) ,我不确定这个。)

我尝试访问 localhost:8000 / one 并遇到以下问题:

firstDirective

我已经制作了以下文件:

djangocmstest / urls.py:

我按照建议here添加了link: { pre: function prelink(scope){ console.log('first directive') console.log(scope) scope.numbers = scope.dataFirst.numbers; } }

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/one/

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

    ^en/
    ^media\/(?P<path>.*)$

The current URL, one/, didn't match any of these.

main / urls.py:

url(r'^/', include('cms.urls')),

出于某种原因,主要/ urls.py 文件内容似乎未包含在 djangocmstest / urls.py 中。

我该如何解决这个问题?

编辑: 使用浏览器中的正确URL(由@catavaran建议)(“... / en / one”),我得到以下内容:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static

urlpatterns = i18n_patterns('',
                       url(r'^admin/', include(admin.site.urls)),
                       # Please note that I'm not sure how to handle
                       # the order of the two following lines.
                       url(r'^/', include('main.urls')),
                       url(r'^/', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

编辑2(已修复): 我现在有这些文件(它可以工作:)):

main / urls.py:

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = patterns('main.views',
    url(r'^one/$', 'template_one'),
)

djangocmstest / urls.py:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/en/one

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

    ^en/ ^admin/
    ^en/ ^/
    ^en/ ^/
    ^media\/(?P<path>.*)$

The current URL, en/one, didn't match any of these.

3 个答案:

答案 0 :(得分:2)

在项目urls.py中,您使用的是i18n_patterns而不是简单的patterns,因此您网页的网址应为:

/en/one/

更新:从项目urls.py中的正则表达式中删除尾部斜杠。因此正则表达式应为r'^',而不是r'^/'

urlpatterns = i18n_patterns('',
                       url(r'^admin/', include(admin.site.urls)),
                       url(r'^', include('main.urls')),
                       url(r'^', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

答案 1 :(得分:1)

main.urlscms.urls的正则表达式不应包含转发斜杠:

urlpatterns = i18n_patterns('',
                       url(r'^admin/', include(admin.site.urls)),
                       # Please note that I'm not sure how to handle
                       # the order of the two following lines.
                       url(r'^', include('main.urls')),
                       url(r'^', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

此外,当您执行查询时,请确保它是

http://127.0.0.1:8000/en/one/

因为main.urls文件中有一个尾部斜杠。

答案 2 :(得分:0)

您正在使用具有相同正则表达式的两个包含。这样,包括赢了工作。试试这个:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static

urlpatterns = i18n_patterns('',
                       url(r'^admin/', include(admin.site.urls)),
                       # Please note that I'm not sure how to handle
                       # the order of the two following lines.
                       url(r'^cms/', include('cms.urls')),
                       url(r'^/', include('main.urls')),

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)