在这个django教程中,我们正在创建一个博客站点,在这里我们正在为用户创建一个登录表单,不幸的是,我得到了一个错误,说“accounts”不是一个注册的命名空间,我如何修复这个问题?
我的urls.py文件的应用程序“帐户”:
from django.conf.urls import url
from.import views
appname= 'accounts'
urlpatterns=[
url(r'^signup/$', views.signup_view, name= "signup"),
url(r'^login/$', views.login_view, name = "login" ),
]
我的views.py的应用程序:
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
def signup_view(request):
if request.method== 'POST':
form= UserCreationForm(request.POST)
if form.is_valid():
form.save()
#log the user in
return redirect('narticle:list')
else:
form=UserCreationForm()
return render (request,'accounts/accounts_signup.html', {'form': form})
def login_view(request):
if request.method == "POST":
form = AuthenticationForm(data= request.POST)
if form.is_valid():
return redirect('narticle:list')
else:
form = AuthenticationForm()
return render(request, 'accounts/login.html',{'form': form})
我的基本布局是:
{% load static from staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>Narticle</title>
<link rel="stylesheet" href="{%static 'styles.css'%}">
</head>
<body>
<div class="wrapper">
<h1> <a href="{% url 'narticle:list' %}">narticle </a> </h1>
{% block content %}
{% endblock %}
</div>
</body>
</html>
登录模板为:
{% extends 'base_layout.html'%}
{%block content%}
<h1> log in</h1>
<form class="site-form" action="{% url 'accounts:login' %}" method="post">
{% csrf_token %}
{{form}}
<input type="submit" name="log_in" value="login">
</form>
{% endblock %}
这些是我的基本URL:
from django.conf.urls import url, include
from django.contrib import admin
from. import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include ('accounts.urls')),
url(r'^narticle/', include ('narticle.urls')),
url(r'^about/$', views.about),
url(r'^$',views.homepage),
]
urlpatterns+= staticfiles_urlpatterns()
urlpatterns+= static(settings.MEDIA_URL, document_root= settings.
MEDIA_ROOT)
3条答案
按热度按时间ca1c2owp1#
相信这是因为你错过了下划线:
ehxuflar2#
您在项目url中遗漏了命名空间。
jrcvhitl3#
你应该在narticle/和accounts/ url上都添加namespace,因为你肯定会有错误,让我们看看如何设置它: