django : 404 (main.urls not included in myproject/urls.py?)

a 夏天 提交于 2020-01-06 19:30:27

问题


I have the following problem :

I've made a little django (1.7.8) project (named djangocmstest) to test django-cms (but it could be related with just django, I'm not sure about this).

I try to access localhost:8000/one and get the following issue :

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.

I've made the following files :

djangocmstest/urls.py :

I added url(r'^/', include('cms.urls')), as suggested here.

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)

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'),
)

For some reason, the main/urls.py file content doesn't seem to be included into djangocmstest/urls.py.

How could I fix this?

EDIT : With the correct URL (as suggested by @catavaran ) in the browser (".../en/one") I get the following :

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.

EDIT 2 (FIXED) : I now have these files (and it works :) ) :

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 :

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)),
                       url(r'^', include('main.urls')),
                       url(r'^', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

回答1:


In the project's urls.py you use the i18n_patterns instead of simple patterns so url for your page should be:

/en/one/

UPDATE: Remove the trailing slashes from the regexes in the project's urls.py. So regex should be r'^' but not the 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)



回答2:


Your regular expressions for main.urls and cms.urls should not include a forwards slash:

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)

Also, when you perform your query, make sure it is for

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

since you do have a trailing slash in your main.urls file.




回答3:


You are using two includes with the same regex. This way the include won't work. Try this:

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)


来源:https://stackoverflow.com/questions/30157231/django-404-main-urls-not-included-in-myproject-urls-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!