Django can't find URL pattern

空扰寡人 提交于 2019-11-28 08:15:44

问题


I can't figure out why Django is unable to find the requested URL in my application.

Here is the error code I get:

 Using the URLconf defined in littlelogsms.urls, Django tried these URL patterns, in this order:
^admin/
^$
The current URL, success/, didn't match any of these.

Here is my sms.urls.py file:

from django.conf.urls import url

from sms import views

urlpatterns = [
    url(r'^success/$', views.success, name='success'),
    url(r'^$', views.index, name='index')
]

Here is the application urls.py file:

from django.conf.urls import include, url
from django.contrib import admin


urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', include('sms.urls')),
]

I can't find the mistake I'm making. Any ideas?


回答1:


This problem is the dollar sign in this url pattern.

url(r'^$', include('sms.urls')),

The caret ^ matches the beginning of the string, and the dollar $ matches the end if the string, so ^$ only matches the index URL /.

You should remove the dollar and change it to:

url(r'^', include('sms.urls')),



回答2:


you should try with the base url,like

Here is the application urls.py file:

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^sms', include('sms.urls')),   //the 'sms' is your defined name
]

Here is my sms.urls.py file:

from django.conf.urls import url

from sms import views

urlpatterns = [
    url(r'^success/$', views.success, name='success'),
    url(r'^$', views.index, name='index')
]

you should try accessing your url's like in your views as

sms/ //for accessing tht index function in view sms/success //for accessing the success function in view

or you can use the labels in the form's action tag in the templates to call the urls,

{% url 'sms:index or success' %} //index,success is the name space given in the sms.urls



来源:https://stackoverflow.com/questions/32458541/django-cant-find-url-pattern

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