【问题标题】:get_success_url() takes exactly 3 arguments (2 given)get_success_url() 正好需要 3 个参数(给定 2 个)
【发布时间】:2016-09-01 06:06:03
【问题描述】:

每一个,我都在用

django-registration-redux (1.4)

对于我的 django 注册(django 1.8),但是,当我从未注册过网络时,将显示错误

,,但是form_valid中的views.py,第43行是编辑功能,,好像不是关于寄存器的??

views.py

@login_required
def edit_thing(request, slug):
# grab the object...
    thing = ProductsTbl.objects.get(slug=slug)
    if thing.user != request.user:
        raise Http404
# set the form we're using...
    form_class = ProductsTblForm
    if request.method == 'POST':
# grab the data from the submitted form
        form = form_class(data=request.POST,files=request.FILES,instance=thing)#**line 43**
        if form.is_valid():
            # save the new data
            form.save()
            return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
    else:
        form = form_class(instance=thing)
# and render the template
    return render(request, 'things/edit_thing.html', {
        'thing': thing,
        'form': form,
    })

urls.py

from django.conf.urls import patterns, url,include
from django.contrib import admin
from django.views.generic import TemplateView
from designer import views
from designer.backends import MyRegistrationView
from django.conf import settings
from django.contrib.auth.views import (
    password_reset,
    password_reset_done,
    password_reset_confirm,
    password_reset_complete,
)

....
urlpatterns = [
    ....
    url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
    ....
]

registration_form.html

<h1>Registration Form</h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock content %}

,,虽然有这个错误,,,我的数据库还是写了用户名和密码,,,. 谁能告诉我为什么会出现这个错误,非常感谢

后端.py

from registration.backends.simple.views import RegistrationView

class MyRegistrationView(RegistrationView):
    def get_success_url(self, request, user):
# the named URL that we want to redirect to after # successful registration
        return ('home') 

【问题讨论】:

  • 请将完整回溯显示为文本而不是图像。
  • 我只是添加了 backends.py

标签: django django-forms django-views django-registration


【解决方案1】:

在 django-registration-redux RegistrationView 中已将 get_success_url 定义为这个。

def get_success_url(self, user=None):
    """
    Use the new user when constructing success_url.
    """
    return super(RegistrationView, self).get_success_url()

因此,似乎只有两个参数将传递给该函数。然而在你的子类中,如果你有

def get_success_url(self, request, user):
   # the named URL that we want to redirect to after # successful    registration
    return ('home') 

其中有一个您不会收到的额外请求参数。因此错误。

【讨论】:

  • 很高兴能帮上忙。
【解决方案2】:

get_success_url 方法不将请求作为参数。删除它。

class MyRegistrationView(RegistrationView):
    def get_success_url(self, user):
        # the named URL that we want to redirect to after # successful registration
        return ('home') 

在这种情况下,由于您总是重定向到 home 视图,您可以改为设置 success_url

class MyRegistrationView(RegistrationView):
    success_url = 'home'

【讨论】:

  • 谢谢,在我删除请求后,它可以工作
【解决方案3】:

在 1.4 版本之后,get_success_url 方法不再以 request 作为参数:

def get_success_url(self, user=None):

但是,如果您确实需要处理请求对象(例如,您想记住用户决定注册的页面,可以作为 get 或 post 参数传递)django-registration-redux 提供了一个非常方便的信号: registration.signals.user_registered

如下:

def remember_program_for_registration(sender, user, request, **kwargs):
    [do some processing of the request object]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多