0

I am currently developing a blogging webapp and I tried to extend my Django User Framework With a One To One Field, everything is working fine, but when I'm trying to register a new user, it's throwing a NameError. It is also worth noting that the user is being created and stored(I checked it from the admin page). It is saying that this statement profile.user = user in my views.py is creating the problem. Can anyone please help me with this?

my views.py

from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth.decorators import login_required from .forms import UserRegisterForm, ProfileForm def register(request): if request.method == 'POST': form = UserRegisterForm(request.POST) profile_form = ProfileForm(request.POST) if form.is_valid() and profile_form.is_valid(): form.save() profile = profile_form.save(commit=False) profile.user = user profile.save() username = form.cleaned_data.get('username') messages.success(request, f'Your account has been created! You are now able to log in') return redirect('login') else: form = UserRegisterForm() profile_form = ProfileForm return render(request, 'users/register.html', {'form': form, 'profile_form': profile_form}) @login_required def profile(request): return render(request, 'users/profile.html') 

my models.py

from django.db import models from django.contrib.auth.models import User class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) image = models.ImageField(default='default.jpg', upload_to='profile_pics') CHOICES = ( ('AB+', 'AB+'), ('AB-', 'AB-'), ('A+', 'A+'), ('A-', 'A-'), ('B+', 'B+'), ('B-', 'B-'), ('O+', 'O+'), ('O-', 'O-'), ) bloodgroup = models.CharField(max_length=100, choices= CHOICES) bio = models.TextField(max_length=300) def __str__(self): return f'{self.user.username} Profile' 

my forms.py

from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm from .models import Profile class UserRegisterForm(UserCreationForm): email = forms.EmailField() class Meta: model = User fields = ['username', 'email', 'password1', 'password2'] def save(self, commit=True): user = super().save(commit=False) if commit: user.save() return user class ProfileForm(forms.ModelForm): class Meta: model = Profile fields = ('image', 'bloodgroup', 'bio') 
1
  • What do you expect user to be in profile.user = user? You never define a variable with that name. Commented Mar 19, 2020 at 17:45

2 Answers 2

1

profile.user = user change to profile.user = request.user

Sign up to request clarification or add additional context in comments.

5 Comments

Now it says : Profile.user" must be a "User" instance.
Now it says : 'AnonymousUser' object is not iterable
you should logged in to handle this error, you can use a login required decorator in this view
Where should I add login required?
no, it is wrong, try to assign from User.object.get(user=request.user), otherwise you have to use pre save signal instead of this way
0

Just add

from django.contrib.auth.models import User 

instead of

from django.contrib.auth import User 

Comments