gpt4 book ai didi

python - 在项目和应用程序之间配置 Django url

转载 作者:行者123 更新时间:2023-11-30 23:18:53 27 4
gpt4 key购买 nike

我想将 Django 教程中的帐户应用程序添加到 mysite/polls 项目中。当我添加“帐户”应用程序来管理用户身份验证时,我无法正确设置网址。

我的根 URLconf 是:

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

urlpatterns = patterns(
'',
# Examples:
# url(r'^$', 'uniVote.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^elections/', include('elections.urls', namespace='elections')),
url(r'^admin/', include(admin.site.urls)),
# ex: accounts/...
url(r'^accounts/', include('accounts.urls')),
)

我的帐户应用程序中的 URLconf 是:

from django.conf.urls import patterns, url

urlpatterns = patterns(
'',
# ex: /accounts/login/
url(r'^accounts/login/$', 'accounts.views.user_login'),
# ex: /accounts/logout/
url(r'^accounts/logout/$', 'accounts.views.user_logout'),
# ex: /accounts/register/
url(r'^accounts/register/$', 'accounts.views.user_register'),
)

另外,这是我的帐户views.py:

from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render_to_response
from django.core.context_processors import csrf

#Import a user registration form
from accounts.forms import UserRegisterForm


# User Login View
def user_login(request):
if request.user.is_anonymous():
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
#This authenticates the user
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
#This logs him in
login(request, user)
else:
return HttpResponse("Not active")
else:
return HttpResponse("Wrong username/password")
return HttpResponseRedirect("/")


# User Logout View
def user_logout(request):
logout(request)
return HttpResponseRedirect('/')


# User Register View
def user_register(request):
if request.user.is_anonymous():
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid:
form.save()
return HttpResponse('User created succcessfully.')
else:
form = UserRegisterForm()
context = {}
context.update(csrf(request))
context['form'] = form
#Pass the context to a template
return render_to_response('accounts/register.html', context)
else:
return HttpResponseRedirect('/')

每当我将浏览器指向:0.0.0.0:8080/accounts/register 时,我都会收到 404 错误和以下输出:

使用 uniVote.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

  1. ^选举/
  2. ^管理员/
  3. ^账户/^账户/登录/$
  4. ^账户/^账户/注销/$
  5. ^账户/^账户/注册/$

当前 URL、accounts/register/与其中任何一个都不匹配。

<小时/>

从教程中,我相信 addr:port/accounts/register 将加载根 URLconf 并查看“accounts”并加载accounts/urls.py 并传递“register”并查看第三个 url 模式并加载帐户。 View .user_register。

我是否缺少某些内容来访问帐户/...网址?

最佳答案

当前您的应用已配置为在 accounts/accounts/register/ 网址中查看。

从您的代码中,URL 的正则表达式重复了“accounts”。

基本上你应该尝试:

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

然后从帐户网址中删除“帐户”。

urlpatterns = patterns(
'',
# ex: /accounts/login/
url(r'^login/$', 'accounts.views.user_login'),
# ex: /accounts/logout/
url(r'^logout/$', 'accounts.views.user_logout'),
# ex: /accounts/register/
url(r'^register/$', 'accounts.views.user_register'),
)

关于python - 在项目和应用程序之间配置 Django url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26443370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com