【问题标题】:Getting _wrapped_view() takes at least 1 argument (0 given) on creation of a model在创建模型时获取 _wrapped_view() 至少需要 1 个参数(给定 0)
【发布时间】:2013-03-01 02:52:51
【问题描述】:

我有一个注册表单,提交后我收到以下错误。

_wrapped_view() 至少需要 1 个参数(给定 0)

当我在我的 view.py 中实例化一个新的 UserProfile 对象时会发生这种情况,即 AppUserRegistration 函数。

我正用头撞墙。那里的错误消息根本没有多大帮助。

models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    birthday = models.DateField(null=True, blank=True)
    profession = models.CharField(max_length=100, null=True, blank=True)
    created = models.DateTimeField()
    modified = models.DateTimeField()

views.py

from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from AutoDIY.forms import RegistrationForm, LoginForm, UserProfileAboutForm
from AutoDIY.models import UserProfile
from django.contrib.auth import authenticate, login, logout 

def AppUserRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/')
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            first_name = form.cleaned_data.get('first_name')
            last_name = form.cleaned_data.get('last_name')
            username = form.cleaned_data.get('username')
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            user = User.objects.create_user(username=username,
                                            email=email,
                                            password=password)
            user.first_name = first_name
            user.last_name = last_name
            user.save()
            user_profile = UserProfile(user=user) # fails right here
            ...

register.html

<form class="form-login form-wrapper form-medium" method="POST">
    {% csrf_token %}
    <h3 class="title-divider"><span>Sign Up</span>
         <small>Already signed up? <a href="{% url login %}">Login here</a>.</small>
    </h3>

    {% if form.errors %}

        <div class="alert alert-error">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <strong>Please correct the following fields:</strong>
            <ul>
                {% if form.first_name.errors %}<li>First name</li>{% endif %}
                {% if form.last_name.errors %}<li>Last name</li>{% endif %}
                {% if form.username.errors %}<li>Username</li>{% endif %}
                {% if form.email.errors %}<li>Email address</li>{% endif %}
                {% if form.password.errors %}<li>Password</li>{% endif %}
                {% if form.password1.errors %}<li>Password Verification</li>{% endif %}
            </ul>
        </div>

    {% endif %}

    <h5>Account Information</h5>
    {{ form.first_name }}
    {{ form.last_name }}
    {{ form.username }}
    {{ form.email }}
    {{ form.password }}
    {{ form.password1 }}
    <label class="checkbox">
        <input type="checkbox" value="term">
        I agree with the Terms and Conditions.
    </label>
    <button class="btn btn-primary" type="submit">Sign up</button>
</form>

【问题讨论】:

    标签: django django-models django-views


    【解决方案1】:

    从您发布的错误来看,实际上 UserProfile 不是模型类,而是一些函数(可能已装饰)。检查您的代码库并确保您没有定义名为 UserProfile 的函数。也许您在 views.py 的下面某处有名为 UserProfile 的视图函数?

    【讨论】:

    • 就是这样!我的views.py 中有一个与我的模型同名的函数。哦,多么尴尬! :) 我怎么没看到。谢谢你的帮助。
    【解决方案2】:
    if form.is_valid():
        first_name = form.cleaned_data['first_name']
        last_name = form.cleaned_data['last_name']
        username = form.cleaned_data['username']
        email = form.cleaned_data['email']
        password = form.cleaned_data['password']
    
        reg = User.objects.create_user(
            username=username,
            password=password, 
            email=email
            ) 
    
        reg.is_active = True
        reg.first_name = first_name
        reg.last_name = last_name
        new_user = reg.save()
    
        // This will give the system seconds (time) to generate new id 
        // before giving to userprofile
        messages.info(request, "User successfully registered. Creating profile...")
    
        UserProfile.objects.create(user_id=new_user.id, other_field='')
    
        ............
    

    【讨论】:

    • 我试过了,但没有我在实例化行出现以下错误:'function' object has no attribute 'objects'
    • 我添加了我的模板 (HTML) 的表单部分。
    • 我尝试根据您的建议重构我的代码,但仍然出现同样的错误:'function' object has no attribute 'objects'。顺便说一句,框架会自动激活用户,这似乎是在创建/持久性期间。
    • @DanielMartin 将您的整个项目发送到我的 gmail 中。我会修复它,我会追踪你为什么会收到这个错误。如果你喜欢
    • @DanielMartin 我不喜欢在这里长时间交谈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2011-12-25
    • 2018-07-17
    相关资源
    最近更新 更多