Django 1.10中的某些URL没有反向匹配

时间:2017-03-19 17:55:55

标签: python django url django-templates django-views

我正在Django 1.10中创建一个Web应用程序,并且遇到一个令人讨厌的错误,我似乎无法解决。在这个项目中有几个Django应用程序,其URL功能没有问题,似乎与新的帐户应用程序具有类似的设置。但是,这个身份验证应用程序给我带来了麻烦。

此外,account/register URL成功运行并将用户插入数据库。我不确定为什么有些URL工作而其他URL没有用。

这是错误:

NoReverseMatch at /account/login/
Reverse for 'dashboard' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL:    http://localhost:8000/account/login/
Django Version: 1.10.6
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'dashboard' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我有以下目录结构:

enter image description here

Project' url.py:

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^account/', include('account.urls', namespace='account')),
    url(r'^orders/', include('orders.urls', namespace='orders')),
    url(r'^shop/', include('shop.urls', namespace='shop')),
    url(r'^cart/', include('cart.urls', namespace='cart')),

]

if settings.DEBUG:
       urlpatterns += static(settings.MEDIA_URL,
                             document_root=settings.MEDIA_ROOT)

帐户应用urls.py:

urlpatterns = [
    # url(r'^login/$', views.user_login, name='login'),
    url(r'^$', views.dashboard, name='dashboard'),

    url(r'^register/$', views.register, name='register'),
    url(r'^edit/$', views.edit, name='edit'),

    # login / logout urls
    url(r'^login/$', django_views.login, name='login'),
    url(r'^logout/$', django_views.logout, name='logout'),
    url(r'^logout-then-login/$', django_views.logout_then_login, name='logout_then_login'),

    # change password urls
    url(r'^password-change/$', django_views.password_change, name='password_change'),
    url(r'^password-change/done/$', django_views.password_change_done, name='password_change_done'),

    # restore password urls
    url(r'^password-reset/$', django_views.password_reset, name='password_reset'),
    url(r'^password-reset/done/$', django_views.password_reset_done, name='password_reset_done'),
    url(r'^password-reset/confirm/(?P<uidb64>[-\w]+)/(?P<token>[-\w]+)/$', django_views.password_reset_confirm, name='password_reset_confirm'),
    url(r'^password-reset/complete/$', django_views.password_reset_complete, name='password_reset_complete'),
]

登录模板:

{% extends "base.html" %}

{% block title %}Log-in{% endblock %}

{% block content %}
    <h1>Log-in</h1>
    {% if form.errors %}
        <p>Your username and password didn't match. Please try again.</p>
    {% else %}
        <p>Please, use the following form to log-in. If you don't have an account <a href="{% url "account:register" %}">register here</a></p>
    {% endif %}

    <div class="login-form">
        <form action="{% url "account:login" %}" method="post">
            {{ form.as_p }}
            {% csrf_token %}
            <input type="hidden" name="next" value="{{ next }}" />
            <p><input type="submit" value="Log-in"></p>
        </form>
        <p><a href="{% url "account:password_reset" %}">Forgotten your password?</a></p>
    </div>

    <div class="social">

    </div>
{% endblock %}

修改(views.py):

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import LoginForm, UserRegistrationForm, UserEditForm, ProfileEditForm
from .models import Profile


def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(username=cd['username'], password=cd['password'])
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponse('Authenticated successfully')
                else:
                    return HttpResponse('Disabled account')
            else:
                return HttpResponse('Invalid login')
    else:
        form = LoginForm()
    return render(request, 'account/login.html', {'form': form})


def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)

        if user_form.is_valid():
            # Create a new user object but avoid saving it yet
            new_user = user_form.save(commit=False)
            # Set the chosen password
            new_user.set_password(user_form.cleaned_data['password'])
            # Save the User object
            new_user.save()
            # Create the user profile
            profile = Profile.objects.create(user=new_user)
            return render(request,
                          'account/register_done.html',
                          {'new_user': new_user})
    else:
        user_form = UserRegistrationForm()
    return render(request, 'account/register.html', {'user_form': user_form})


@login_required
def edit(request):
    if request.method == 'POST':
        user_form = UserEditForm(instance=request.user,
                                 data=request.POST)
        profile_form = ProfileEditForm(instance=request.user.profile,
                                       data=request.POST,
                                       files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Profile updated successfully')
        else:
            messages.error(request, 'Error updating your profile')
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile)
    return render(request, 'account/edit.html', {'user_form': user_form,
                                                 'profile_form': profile_form})


@login_required
def dashboard(request):
    return render(request, 'account/dashboard.html', {'section': 'dashboard'})

1 个答案:

答案 0 :(得分:0)

某处,在您尚未显示的模板中,您已完成{% url "dashboard" %},但该URL位于帐户命名空间下。所以你需要做{% url "account:dashboard" %}