Reputation: 519
I want to create something where the current logged user can create 'posts'. Here's my codes:
models.py
from django.contrib.auth.models import User
# Posts
class Post(models.Model):
user = models.ForeignKey(User)
date = models.DateField(auto_now_add=True)
content = models.TextField(max_length=500, blank=False)
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = models.Post
labels = {
'content': ''
}
widgets = {
'user': forms.HiddenInput()
}
fields = '__all__'
# exclude = ('user',)
views.py
def my_profile(request):
user = request.user
profile_info = request.user.userprofileinfo
if request.method == 'POST':
post = forms.PostForm(data=request.POST)
if post.is_valid():
post.save(commit=False)
post.user = user
post.save(commit=True)
return HttpResponseRedirect(reverse('td_app:index'))
else:
print(post.errors)
return HttpResponseRedirect(reverse('td_app:my_profile'))
else:
form = forms.PostForm
data = {
'user': user,
'profile_info': profile_info,
'form': form
}
return render(request, 'td_app/my_profile.html', context=data)
I have declared the 'user' field to be 'hidden' since we don't want users pretending to be someone else when they post - the 'user' field should automatically register the current logged user. However it keeps giving me this error:
<ul class="errorlist"><li>user<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
When I do this instead:
class PostForm(forms.ModelForm):
class Meta:
model = models.Post
labels = {
'content': ''
}
# widgets = {
# 'user': forms.HiddenInput()
# }
fields = '__all__'
exclude = ('user',)
It will now give me a null error:
The above exception (null value in column "user_id" violates not-null constraint
I'm relatively new to Django so forgive my naiveness. How can fix this? Thanks a ton!
Upvotes: 0
Views: 56
Reputation: 9359
However it keeps giving me this error:
This field is required.
that's because you have the user
field in form (even though it's a hidden field), but it does not have any value (and I think all fields are mandatory by default), so the form validation complaining about missing field value is expected.
My suggestions would be to remove the user
field from form (using exclude = ('user',)
you have there commented out already), and then inject the request.user
before calling save()
. Setting post.user = user
doesn't do anything (or at least not the thing you think it does), because your variable post
isn't actually an instance of Post
model, but instance of PostForm
. The instance of model which will be saved is available in post.instance
. So what you want to do is this
form = forms.PostForm(data=request.POST)
form.instance.user = request.user
form.save()
As for the reason why your form doesn't have any value in the user
field is because you dont fill the form with any data. To achieve that you have to use initial parameter of the form:
else:
# now the form will contain `user` value
form = forms.PostForm(initial={'user': user})
but I rather would recommend hiding the user
field in form anyway.
Upvotes: 1