简体   繁体   中英

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

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)

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)

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM