【发布时间】:2014-02-15 08:16:16
【问题描述】:
我目前正在学习 Django,我正在尝试添加表单来注册用户。
现在我的问题很早就开始了,因为每当我尝试访问我的注册页面时都会收到 404。
我的3个文件如下:
views.py : 只是一个基本显示的 hello world
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
def registration(request):
return HttpResponse("Registration page")
urls.py(在我的项目中):
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
(r'^$', include('myapp.urls')), # index with login/registration
(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
(r'^admin/', include(admin.site.urls)), # admin site
)
最后是 urls.py(在我的应用中):
from django.conf.urls import patterns, url
from myapp import views
urlpatterns = patterns('',
url(r'^$',views.index, name='index'),
url(r'^registration/$',views.registration, name='registration'),
)
当我转到我的正常索引 (localhost:8000) 时,它会显示我在 views.py 文件中编写的 hello world。但是,当我尝试进入注册页面时,它只会返回一个 404(localhost:8000/registration)。
现在正如我所说,我仍在学习这整件事,但我不明白为什么它不能正常工作。据我了解,项目文件中的 r'^$' 指向 localhost:8000 并且应用程序文件中的相同正则表达式告诉它在此位置加载索引页面。现在程序仍在 localhost:8000 中,r'^registration/' 应该正在加载 localhost:8000/registration 中的另一个页面,对吗?
如果你能向我解释为什么这不起作用以及我在哪里做错了,那就太好了。
这是我得到的错误:
当我服用
(r'^randomstringhere/', include('myapp.urls')), # index with login/registration
而不是
(r'^$', include('myapp.urls')), # index with login/registration
我可以通过 localhost:8000/randomstringhere/registration 进入注册页面。但是站点意味着有一个选择,您可以在 localhost:8000(或未来的域 www.randomdomainhere.com)上登录或注册,并在 localhost:8000/login、localhost:8000/registration(www .randomdomainhere.com/registration 等)
【问题讨论】:
-
最后使用斜线:localhost:8000/registration/。至少这就是你的正则表达式对 URL conf 所说的。有关如何解决它,请参阅 Odif Yltsaeb 的答案。
标签: python django http-status-code-404 django-urls