Django注册无效

时间:2018-08-19 11:23:16

标签: python django authentication django-urls user-registration

我正在尝试添加用户注册页面,但显示页面

NoReverseMatch at /register/

这是我的项目urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('pages.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/', admin.site.urls),
]

页面urls.py

from django.urls import path
from .views import *

from django.contrib.auth import views

app_name = 'pages'
urlpatterns = [
    path('', home_view, name='home'),
    path('register/', register_user, name='register'),
]

页面views.py

def home_view(request, *args, **kwargs):
    return render(request, 'home.html', {})

def register_user(request, *args, **kwargs):
    if request.method=='POST':
        form = UserCreationForm(request.POST)

        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('home')

    else:
        form = UserCreationForm()

    context = {'form': form}
    return render(request, 'register.html', context)

register.html

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<h2>Register here</h2>
<body>
    <form method="post" action="{% url 'register' %}">
        {% csrf_token %}

        {% if form.errors %}
        <p>Invalid details</p>
        {% endif %}

        {{ form }}

        <input type="submit" value="Register">
    </form>
</body>
</html>

一切正常,但仍然显示错误

NoReverseMatch at /register/
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.

1 个答案:

答案 0 :(得分:1)

urls.py文件中,您指定了一个名称空间:

from django.urls import path
from .views import *

from django.contrib.auth import views

app_name = 'pages'

urlpatterns = [
    path('', home_view, name='home'),
    path('register/', register_user, name='register'),
]

这意味着要引用这种命名视图,您需要在其前面加上名称空间。因此,这意味着在模板中,您需要像这样编写它:

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<h2>Register here</h2>
<body>
    <form method="post" action="{% url 'pages:register' %}">
        {% csrf_token %}

        {% if form.errors %}
        <p>Invalid details</p>
        {% endif %}

        {{ form }}

        <input type="submit" value="Register">
    </form>
</body>
</html>

此外,redirect(..)应该以相同的方式重写:

def register_user(request, *args, **kwargs):
    if request.method=='POST':
        form = UserCreationForm(request.POST)

        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('pages:home')

    else:
        form = UserCreationForm()

    context = {'form': form}
    return render(request, 'register.html', context)