NoReverseMatch与参数'()'和关键字参数{}没有找到。0模式(s)尝试:[]

时间:2022-06-02 23:20:22

I am trying understand why I get the NoReverseMatch error when I use the User register form that I've created:

我正在尝试理解为什么当我使用我创建的用户注册表时,会出现NoReverseMatch错误:

According to my situation, I reference the relevant files/information:

根据我的情况,我参考了相关的文件/信息:

I have the main urls.py file named neurorehab/urls.py

我有主要的url。py文件名为neurorehab / urls . py

from django.conf.urls import include, url, patterns
from django.conf import settings
from django.contrib import admin
from .views import home, home_files

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', home, name='home'),

    url(r'^', include('userprofiles.urls')),
    #Call the userprofiles/urls.py

    url(r'^(?P<filename>(robots.txt)|(humans.txt))$', home_files, name='home-files'),


]

# Response the media files only in development environment
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT,}),
)

I have the module/application named userprofiles, in which I have the userprofiles/urls.py file of this way:

我有一个名为userprofiles的模块/应用程序,其中有用户配置文件/url。这种方式的py文件:

from django.conf.urls import include, url, patterns
from .views import (ProfileView, LogoutView,
                AccountRegistrationView, PasswordRecoveryView,
                SettingsView)
from userprofiles.forms import CustomAuthenticationForm

urlpatterns = [
    url(r'^accounts/profile/$', ProfileView.as_view(), name='profile/'),

    # Url that I am using for this case
    url(r'^register/$', AccountRegistrationView.as_view(), name='register'),

    url(r'^login/$','django.contrib.auth.views.login', {
                    'authentication_form': CustomAuthenticationForm,
                    }, name='login',
),
    url(r'^logout/$', LogoutView.as_view(), name='logout'),
]

The url register call to the CBV AccountRegistrationView located in the userprofiles/urls.py which is so:

url寄存器调用位于userprofiles/url中的CBV AccountRegistrationView。py是如此:

from django.shortcuts import render
from django.contrib.auth import login, logout, get_user, authenticate
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader

# Importing classes for LoginView form
from django.views.generic import FormView, TemplateView, RedirectView
from django.contrib.auth.forms import AuthenticationForm
from django.core.urlresolvers import reverse, reverse_lazy

from .mixins import LoginRequiredMixin
from .forms import  UserCreateForm

class AccountRegistrationView(FormView):
    template_name = 'signup.html'
    form_class = UserCreateForm

    # Is here in the success_url in where I use reverse_lazy and I get
    # the NoReverseMatch
    success_url = reverse_lazy('accounts/profile')
    #success_url = '/accounts/profile'

    # Override the form_valid method
    def form_valid(self, form):
        # get our saved user with form.save()
        saved_user = form.save()
        user = authenticate(username = saved_user.username,
                            password = form.cleaned_data['password1'])

        # Login the user, then we authenticate it
        login(self.request,user)

        # redirect the user to the url home or profile
        # Is here in the self.get_success_url in where I  get
        # the NoReverseMatch
        return HttpResponseRedirect(self.get_success_url())

My form class UserCreateForm in which I made the registration form is located in userprofiles/forms.py file and it's so:

我的表单类UserCreateForm,我在其中创建了注册表单,它位于userprofiles/表单中。py文件,它是:

from django import forms
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class UserCreateForm(UserCreationForm):

    def __init__(self, *args, **kwargs):
        super(UserCreateForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.add_input(Submit('submit', u'Save'))

    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ('username','email','password1','password2',)

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data['email']

        if commit:
            user.save()
        return user

And my template is the userprofiles/templates/signup.html file:

我的模板是用户配置文件/模板/注册。html文件:

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %}Register{% endblock %}
{% block content %}

<div>
    {% crispy form %}
    {% csrf_token %}

</div>
{% endblock %}

When I go to my register user form, and I pressed submit this save my user and I have it for that try redirect to profile of the user recent created, but I get this error

当我进入到我的注册用户表单时,我按下提交这个保存我的用户,我有它尝试重定向到最近创建的用户的配置文件,但是我得到这个错误。

NoReverseMatch与参数'()'和关键字参数{}没有找到。0模式(s)尝试:[]

What may be happening to me in this case. Seem that the reverse_lazy does not work?

在这种情况下,我可能会发生什么。似乎reverse_lazy不工作?

Any help will be appreciated :)

如有任何帮助,我们将不胜感激:

1 个解决方案

#1


1  

The reverse_lazy() function either takes view function or url name to resolve it not the url path. So you need to call it as

reverse_lazy()函数可以使用视图函数或url名称来解析它,而不是解析url路径。所以你需要把它叫做。

success_url = reverse_lazy('profile/')
#---------------------------^ use url name

However, I'm not sure if '/' character works in url name.

但是,我不确定“/”字符是否在url名称中工作。

If you have to use path to resolve to an url, use resolve() function.

如果必须使用路径来解析url,请使用resolve()函数。

#1


1  

The reverse_lazy() function either takes view function or url name to resolve it not the url path. So you need to call it as

reverse_lazy()函数可以使用视图函数或url名称来解析它,而不是解析url路径。所以你需要把它叫做。

success_url = reverse_lazy('profile/')
#---------------------------^ use url name

However, I'm not sure if '/' character works in url name.

但是,我不确定“/”字符是否在url名称中工作。

If you have to use path to resolve to an url, use resolve() function.

如果必须使用路径来解析url,请使用resolve()函数。