Answer the question
In order to leave comments, you need to log in
How to add a user to CreateView?
There is a User model inherited from AbstractUser and supplemented with:
class User(AbstractUser):
email = models.EmailField(unique=True)
bio = models.TextField(blank=True, max_length=1000)
date_of_birth = models.DateField(blank=True, null=True)
class Post(models.Model):
theme = models.CharField(max_length=50)
content = models.TextField()
photo = models.ImageField(upload_to='media/')
status = models.BooleanField(default=True)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['theme', 'content', 'photo', 'user']
widgets = {
'theme': forms.TextInput(attrs={'class': 'form-control'}),
'content': forms.Textarea(attrs={'class': 'form-control', 'rows': 5}),
}
class CreatePost(LoginRequiredMixin, CreateView):
form_class = PostForm
template_name = '/add_post.html'
raise_exception = True
fields = ['theme', 'content', 'photo', 'user']
then the Post simply does not go, does not find the user, and gives an error.) Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question