获取_wrapped_view()在创建模型时至少需要1个参数(0给定)

时间:2013-03-01 02:52:51

标签: django django-models django-views

我有一份注册表格,当提交时我收到以下错误。

_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>

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

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='')

    ............