3

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

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

它发生在我在view.py中实例化一个新的UserProfile对象时,即AppUserRegistration函数。

我正在把头撞在墙上。那里的错误信息根本没有多大帮助。

模型.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()

视图.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
            ...

注册.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>
4

2 回答 2

5

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

于 2013-03-01T16:46:44.703 回答
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='')

    ............
于 2013-03-01T02:57:44.913 回答