AttributeError at /accounts/regist_save/ 'User' object has no attribute 'user' error happens.I wrote in views.py I wrote in views.py
def regist(request):
regist_form = RegisterForm(request.POST or None)
profile_form = ProfileForm(request.POST or None)
context = {
'regist_form': regist_form,
'profile_form': profile_form,
}
return render(request, 'registration/regist.html', context)
@require_POST
def regist_save(request):
regist_form = RegisterForm(request.POST or None)
profile_form = ProfileForm(request.POST or None)
if request.method == "POST" and regist_form.is_valid() and profile_form.is_valid():
regist = regist_form.save(commit=False)
regist.is_staff = True
regist.save()
profile = profile_form.save(commit=False)
profile.user = regist.user
return render(request, 'registration/detail.html')
in regist.html
<div class="form-group-lg">
<label for="id_username">Username</label>
{{ regist_form.username }}
</div>
<div class="form-group-lg">
<label for="id_email">Email</label>
{{ regist_form.email }}
</div>
<div class="form-group-lg">
<label for="id_password">Password</label>
{{ regist_form.password1 }}
</div>
<div class="form-group-lg">
<label for="id_password">Password2</label>
{{ regist_form.password2 }}
<p class="help-block">{{ regist_form.password2.help_text }}</p>
</div>
{% load static %}
<div class="form-group-lg">
<label for="sex">SEX</label>
<select id="sex" class="form-control sex" name="sex">
<option value="">--</option>
<option value="male">male</option>
<option value="female">female</option>
</select>
</div>
<script src="{% static 'accounts/register.js' %}"></script>
<button type="submit" class="btn-lg regist">REGIST</button>
<input name="next" type="hidden" />
{% csrf_token %}
</form>
in models.py
from django.db import models
from django.contrib.auth.models import User
class NewUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birthday = models.CharField(max_length=100,null=True, blank=True, default=None)
sex = models.CharField(max_length=100,null=True, blank=True, default=None)
I really cannot understand why this error happens because I think profile.user = regist.user
can be gotten the user.I tried to write profile.user = request.user
but IntegrityError happens.How can I get the user?Why can't i get it by using this code?Am I wrong to write User model 's column name?What should i do to fix this?
I think regist
itself is user object.
just try:
profile.user = regist
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.