简体   繁体   English

Django NoReverseMatch URL 重定向

[英]Django NoReverseMatch URL redirect

I am trying to customize the signup redirect url using allauth in django 3. I am getting this error, instead:我正在尝试使用 django 3 中的 allauth 自定义注册重定向 url。我收到此错误,而不是:

NoReverseMatch at /accounts/candidate-signup/

Reverse for 'candidate_profile' with no arguments not found. 1 pattern(s) tried: ['accounts/profile/(?P<username>[^/]+)/$']

views.py视图.py

from django.shortcuts import render
from django.views import View

from .models import CandidateProfile
from .forms import CandidateProfileForm

from allauth.account import app_settings
from allauth.account.views import SignupView
from allauth.account.utils import complete_signup
from allauth.exceptions import ImmediateHttpResponse


class CandidateSignUpView(SignupView, View):
    def get(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        return render(request, self.template_name, {'form': form})

    def form_valid(self, form):
        self.user = form.save(self.request)
        self.user.is_candidate = True
        self.user.save()
        success_url = f'profile/{self.user.username}/'
        try:
            return complete_signup(
                self.request,
                self.user,
                app_settings.EMAIL_VERIFICATION,
                self.success_url,
            )
        except ImmediateHttpResponse as e:
            return e.response

urls.py网址.py

from django.urls import path

from . import views

urlpatterns = [
    path('candidate-signup/', views.CandidateSignUpView.as_view(), name='candidate_signup'),
    path('company-signup/', views.CompanySignUpView.as_view(), name='company_signup'),
    path('profile/<str:username>/', views.CandidateProfileView.as_view(), name='candidate_profile'),
]

django/conf/global_settings.py django/conf/global_settings.py

LOGIN_REDIRECT_URL = 'candidate_profile'

html html

                <form class="signup" id="signup_form" method="post" action="{% url 'candidate_signup' %}">
                    {% csrf_token %}
                    {{ form.as_p }}
                    {% if redirect_field_value %}
                        <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
                    {% endif %}

A working solution would redirect from the signup page to /accounts/profile/<str:username>/一个可行的解决方案将从注册页面重定向到/accounts/profile/<str:username>/

You have two issues in CandidateSignUpView :您在CandidateSignUpView有两个问题:

  1. You're passing self.success_url to complete_signup , instead of passing success_url , which is defined locally in form_valid method.您将self.success_url传递给complete_signup ,而不是传递在success_url方法中本地定义的form_valid As self.success_url is None (it is defined in SignupView and inherited in your CandidateSignUpView ), complete_signup is receiving None for success_url parameter, so it uses default success_url after login.由于self.success_urlNone (它在SignupView中定义并在您的CandidateSignUpView中继承), complete_signup正在接收Nonesuccess_url参数,因此它在登录后使用默认的success_url
  2. You should better not construct urls manually.您最好不要手动构建 url。 For this you'd better use reverse and pass view name and according arguments, so instead of success_url = f'profile/{self.user.username}/' you'd better use success_url = reverse('candidate_profile',kwargs={'username':self.user.username}) .为此,您最好使用reverse并传递视图名称并根据 arguments,因此您最好使用success_url = reverse('candidate_profile',kwargs={'username':self.user.username})而不是success_url = f'profile/{self.user.username}/' success_url = reverse('candidate_profile',kwargs={'username':self.user.username}) See more about reversehere . 在此处查看有关reverse的更多信息。

So, your CandidateSignUpView should be something like this:因此,您的CandidateSignUpView应该是这样的:

from django.shortcuts import render
from django.views import View

from .models import CandidateProfile
from .forms import CandidateProfileForm

from allauth.account.views import SignupView
from allauth.account.utils import complete_signup
from allauth.exceptions import ImmediateHttpResponse
from django.http.response import HttpResponse
from django.urls.base import reverse


class CandidateSignUpView(SignupView, View):
    def get(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        return render(request, self.template_name, {'form': form})

    def form_valid(self, form):
        self.user = form.save(self.request)
        self.user.is_candidate = True
        self.user.save()
        # success_url = f'profile/{self.user.username}/'
        success_url = reverse('candidate_profile',kwargs={'username':self.user.username})
        
        try:
            return complete_signup(
                self.request,
                self.user,
                app_settings.EMAIL_VERIFICATION,
                # self.success_url,
                success_url
            )
        except ImmediateHttpResponse as e:
            return e.response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM