I'm currently creating a Social platform with Django. Right now, I'm developing their Dashboard and want endusers to be able to publish their own posts. This is my Post model:
class Post(models.Model):
user = models.ForeignKey(User)
posted = models.DateTimeField(auto_now_add=True)
content = models.CharField(max_length=150)
Likes = models.IntegerField(default=0)
def __str__(self):
return self.user.username
My Form for the Post model:
class Post_form(forms.ModelForm):
class Meta:
model = Post
fields = (
'content',
]
def __init__(self, *args, **kwargs):
super(Post_form, self).__init__(*args, **kwargs)
self.fields['content'].label = ""
self.fields['content'].widget.attrs={
'id': 'id_content',
'class': 'myCustomClass',
'name': 'name_content',
'placeholder': 'What are you thinking about?',
}
When the form gets submitted, it correctly generates a POST request with the value of the content field. When I save my form in the view, no new post gets generated. What am I doing wrong?
My views.py:
def dashboard_view(request):
if request.POST:
form = Post_form(data=request.POST, instance=request.user)
if form.is_valid():
form.save() // <- no new post gets generated after saving
return redirect(reverse('dashboard'))
else:
return redirect(reverse('settings'))
else:
form = Post_form
gebruikers = User.objects.all()
all_posts = Post.objects.all().order_by('-posted')
return render(request, 'Dashboard/index.html', {'gebruiker':gebruikers, 'posts':all_posts, 'form': form},)
You can't pass the user as the instance parameter to a Post form. You should omit that argument and assign the user on the actual Post instance returned from save.
form = Post_form(data=request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
Related
I want to get the current logged in user to this CreateForm form. Below Im giving my current code, here request.user is not giving me error
ValueError at /create-post/
Cannot assign "<SimpleLazyObject: <User: testuser>>": "Post.author" must be a "Author" instance.
models.py
class Author(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
rate = models.IntegerField(default=0)
class Post(models.Model):
title = models.CharField(max_length= 50)
overview = models.TextField()
body_text = RichTextUploadingField(null = True)
time_upload = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(Author, related_name='author', on_delete=models.CASCADE)
thumbnail = models.ImageField(upload_to = 'thumbnails')
publish = models.BooleanField()
categories = models.ManyToManyField(Categories)
read = models.IntegerField(default=0)
slug = models.SlugField(null= True, blank= True)
Forms.py
class CreateForm(ModelForm):
class Meta:
model = Post
fields = [
'title',
'overview',
'body_text',
'thumbnail',
'categories',
'publish',
]
Views.py
def create_post(request):
if request.method == 'POST':
form = CreateForm(request.POST, request.FILES)
if form.is_valid:
post = form.save(commit=False)
post.author= request.user
post.save()
return render(request, 'index.html')
else:
form = CreateForm()
return render(request, 'create_post.html', {'form': form})
As the error says, post.author expects an Author object, not the user. You thus retrieve this with:
from django.contrib.auth.decorators import login_required
#login_required
def create_post(request):
if request.method == 'POST':
form = CreateForm(request.POST, request.FILES)
if form.is_valid():
form.instance.author= request.user.author
form.save()
return render(request, 'index.html')
else:
form = CreateForm()
return render(request, 'create_post.html', {'form': form})
You should also call the is_valid() method [Django-doc], so form.is_valid().
Note: You can limit views to a view to authenticated users with the
#login_required decorator [Django-doc].
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
The author field on your Post model is a foreign key to Author model, and not user. You could replace post.author = request.user with:
if request.user.author:
post.author = request.user.author
else:
post.author = Author.objects.create(user=request.user)
The answer from Willem works if an Author instance was already created for the User, but will throw an exception if there's no Author object associated with that user.
In one of my views I have a form where when a user logs in and submits the form, it works fine.
However, when an anonymous user submits the form I get the following error:
Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x1052fd3a0>>": "User_Inquiries.user" must be a "CustomUser" instance.
This form needs to be submitted whether a user is anonymous or logged in.
What do I need to do in order to resolve this issue?
Code below.
Any help is gladly appreciated. Thanks!
views.py
def account_view_contact(request):
form = ContactUsForm(request.POST or None, request.FILES or None,)
user_profile = User_Inquiries.objects.all()
user_profile = User_Info.objects.all()
user = request.user
if request.method == "POST": # checking if request is POST or Not
# if its a post request, then its checking if the form is valid or not
if form.is_valid():
contact_instance = form.save(commit=False) # "this will return the 'Listing' instance"
contact_instance.user = user # assign 'user' instance
contact_instance.save() # calling 'save()' method of model
return redirect("home")
context = {
'form': form, 'user_profile': user_profile
}
return render(request, 'contact.html', context)
models.py
class User_Inquiries(models.Model):
email = models.EmailField(max_length=100, null=True)
name = models.CharField(max_length=100, null=True)
subject = models.CharField(max_length=100, null=True)
message = models.CharField(max_length=100, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=False, on_delete=models.CASCADE)
date_submitted = models.DateTimeField(auto_now_add=True, null=True)
class Meta:
verbose_name_plural = "User Inquiries"
#property
def user_message(self):
return truncatechars(self.message, 30)
What you can do is :
First, add a user field with null=True
class User_Inquiries(models.Model):
# ....
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
# ...
Second, in your view, you need to provide the user field only if the user is authenticated. Because AnonymousUser can be saved in the database.
def account_view_contact(request):
user_profile = User_Info.objects.all()
# If it's a post request
if request.method == "POST":
form = ContactUsForm(request.POST, request.FILES or None,)
if form.is_valid():
if request.user is authenticated:
# Only authenticated user can be assigned
contact_instance = form.save(commit=False)
contact_instance.user = request.user
contact_instance.save()
return redirect("home")
else:
# Save the form without user because no user is logged in
form.save()
# Handle no POST request
else:
form = ContactUsForm()
context = {
'form': form, 'user_profile': user_profile
}
return render(request, 'contact.html', context)
the User_Inquiries model user should be blank = True and null = True. In the view check if the user is logged in before setting the contact_instance.user to user.
models.py
class User_Inquiries(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
views.py
def account_view_contact(request):
form = ContactUsForm(request.POST or None, request.FILES or None,)
user_profile = User_Inquiries.objects.all()
user_profile = User_Info.objects.all()
# check if the user is authenticated
if request.user.is_authenticated:
user = request.user
if request.method == "POST": # checking if request is POST or Not
# if its a post request, then its checking if the form is valid or not
if form.is_valid():
contact_instance = form.save(commit=False) # "this will return the 'Listing' instance"
contact_instance.user = user # assign 'user' instance
contact_instance.save() # calling 'save()' method of model
return redirect("home")
context = {
'form': form, 'user_profile': user_profile
}
return render(request, 'contact.html', context)
check if user is authenticated in django
set null and blank = True
I tried the above solutions and it worked. I also used CreateView to figure this out and it worked flawlessly. Code below:
class account_view_contact(CreateView):
model = User_Inquiries
form_class = ContactUsForm
template_name = "contact.html"
success_url = reverse_lazy("home")
def form_valid(self, form):
if self.request.user.is_authenticated:
self.object = form.save(commit=False)
self.object.created_by = self.request.user
self.object.save()
else:
self.object = form.save(commit=False)
self.object.save()
return super().form_valid(form)
Thanks Everyone!
I have faced a problem in my Django project where my form is not being saved as a new listing in my model(listing) and is not even showing on Django's admin page.
my models.py :
class listing(models.Model):
title = models.CharField(max_length=64)
describtion = models.CharField(max_length=300)
bid = models.FloatField()
category = models.ForeignKey(categories, default=1, verbose_name="Category",
on_delete=models.SET_DEFAULT)
user = models.ForeignKey(User,default='', verbose_name="User", on_delete=models.SET_DEFAULT)
image = models.CharField(max_length=400)
def __str__(self):
return f"{self.title} "
create a new listing form :
class create(ModelForm):
class Meta:
model = listing
fields = [ 'title', 'describtion','bid','category','image']
views.py :
def CreateListing(request):
user = request.user
if request.method == "POST":
form = create(request.POST, instance=user)
if form.is_valid():
new_listing = form.save()
new_listing.user = request.user
new_listing.save()
return render(request, "auctions/listing.html")
else:
return render(request, "auctions/Create.html",{
"form": create
})
Ps: I have no problem with my urls.py
You need to set the user before you can save this to the database:
def CreateListing(request):
user = request.user
if request.method == "POST":
form = create(request.POST, instance=user)
if form.is_valid():
form.instance.user = user
form.save()
return redirect('%name-of-some-view')
else:
form = create(instance=user)
return render(request, "auctions/Create.html",{
'form': form
})
Note: You can limit views to a view to authenticated users with the
#login_required decorator [Django-doc].
Note: In case of a successful POST request, you should make a redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
I am trying to create an endpoint to edit both the user model and custom profile model below.
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500)
location = models.CharField(max_length=50)
image = models.ImageField(default='default.jpg', upload_to='profile')
In the regular django I would do:
views.py
def edit_profile(request):
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user)
extended_profile_form = ProfileForm(request.POST,
request.FILES,
instance=request.user.profile)
if form.is_valid() and extended_profile_form.is_valid():
form.save()
extended_profile_form.save()
return redirect('accounts:profile')
else:
form = EditProfileForm(instance=request.user)
extended_profile_form = ProfileForm(instance=request.user.profile)
context = {
'form':form,
'extended_profile_form':extended_profile_form
}
return render(request, 'accounts/edit-profile.html', context)
what is the equivalent for django rest framework?
I have tried:
views.py (Django Rest Framework)
#api_view(['GET','PUT'])
def profile(request):
if request.method == 'GET':
user = User.objects.filter(username=request.user)
profile_user = Profile.objects.filter(user=request.user)
serializer_user = UserSerializer(user, many=True)
serializer_profile_user = ProfileSerializer(profile_user, many=True)
result = {'serializer_user': serializer_user.data, 'serializer_profile_user': serializer_profile_user.data}
return Response(result)
elif request.method == 'PUT':
user = User.objects.filter(username=request.user)
profile_user = Profile.objects.filter(user=request.user)
serializer_user = UserSerializer(user, data=request.data)
serializer_profile_user = ProfileSerializer(profile_user, data=request.data)
if serializer_user.is_valid() and serializer_profile_user.is_valid():
serializer_user.save()
serializer_profile_user.save()
result = {'serializer_user': serializer_user.data, 'serializer_profile_user': serializer_profile_user.data}
return Response(result)
result = {'serializer_user': serializer_user.data, 'serializer_profile_user': serializer_profile_user.data}
return Response(result.errors, status=status.HTTP_400_BAD_REQUEST)
When I am browsing the endpoint, it does display serializer_user and serializer_profile_user data but I am unable to edit any of those data using the DRF browsable API.
Am I right thinking the codes above is the equivalent of the codes from the codes from the normal django to edit the profile of the user?
It looks fine to me, but you need to replace this:
if request.method == 'GET':
user = User.objects.filter(username=request.user)
with this:
if request.method == 'GET':
try:
user = User.objects.get(id=request.user.id)
except User.DoesNotExist:
return Response(data='no such user!', status=status.HTTP_400_BAD_REQUEST)
# you need to use objects.get because objects.filter returns a queryset not an abject
Because, request.user is an instance of User model, you cannot compare it to an attribute of user (in your case username)
PS: same goes with your PUT method as well.
Hope this helps!
Look. You can make it easier. Let's take Post model (for example):
class Post(models.Model):
author = models.ForeignKey(base.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
text = models.TextField()
likes = models.ManyToManyField(base.AUTH_USER_MODEL, blank=True, related_name='post_likes')
created_date = models.DateTimeField(default=timezone.now)
And that You should describe it in your serializer (serializer is something similar to DTO. It converts data into a service-friendly JSON view):
class PostCreateUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'title', 'text']
And the last part - Endpoint:
class PostUpdateView(UpdateAPIView):
serializer_class = PostCreateUpdateSerializer
def get_queryset(self):
return Post.objects.filter(author=self.request.user)
It will be more comfortable to use CBV for Django and DRF
And One more thing. You shouldn't create one more table for your user model. This is due to the extension of the BaseUser model. Link for help
I am developing a django application which has a form for creating ingredients. The form contains a dropdown for selecting Recipes. When a user creates an ingredient, in the dropdown, I want that only those recipes should appear that are created by the same user.
Here is my code:
#forms.py
class IngredientForm(forms.ModelForm):
primal = forms.BooleanField()
class Meta:
model = Ingredient
fields = ('recipe_id', 'title', 'instructions', 'rules')
#models.py
class Recipe(models.Model):
user = models.ForeignKey('auth.User')
title = models.CharField(max_length=500)
description = models.TextField(max_length=500)
rules = models.TextField(max_length=500,blank=True)
def __str__(self):
return self.title
class Ingredient(models.Model):
user = models.ForeignKey('auth.User')
recipe_id = models.ForeignKey(Recipe, on_delete=models.CASCADE)
title = models.CharField(max_length=500)
instructions = models.CharField(max_length=500)
rules = models.TextField(max_length=500,blank=True)
primal = models.CharField(default='0',max_length=500,blank=True)
def __str__(self):
return self.title
#views.py
def create_ingredient(request):
if request.method == 'POST':
form = IngredientForm(request.POST)
if form.is_valid():
current_user = request.user
data = form.cleaned_data
ingredient_data=Ingredient.objects.create(user=current_user, recipe_id=data['recipe_id'],title=data['title'], primal=data['primal'], instructions=data['instructions'], rules=data['rules'])
ingredient_data.save()
ingredient = Ingredient.objects.get(pk = ingredient_data.pk)
return redirect('ingredient_detail', pk=ingredient.pk)
else:
messages.error(request, "Error")
return render(request, 'create_ingredient.html', {'form': IngredientForm })
The problem is that right now, when the user tries to select a recipe, the recipes created by all users of the site appear in the 'recipe_id' dropdown. He should only be able to see recipes in the dropdown that are created by himself. Any ideas how to do it?
UPDATE FROM ANSWER:
If I use this:
...
if request.method == 'POST':
form = IngredientForm(current_user=request.user, request.POST)
if form.is_valid():
...
it gives me this syntax error: non-keyword arg after keyword arg in this line form = IngredientForm(current_user=request.user, request.POST)
UPDATE#2:
If I use:
...
if request.method == 'POST':
form = IngredientForm( request.POST,current_user=request.user)
if form.is_valid():
...
It gives me error: __init__() got multiple values of argument 'current.user'
If I use:
...
if request.method == 'POST':
form = IngredientForm( request.POST)
if form.is_valid():
...
It gives me error: 'QueryDict' object has no attribute 'id'
UPDATE # 3:
After implementing the latest update from answer. It gives me error name 'current_user' is not defined
in the following piece of code:
def create_ingredient(request):
form = IngredientForm(current_user=request.user)
In the model form you can do this:
class IngredientForm(ModelForm):
primal = forms.BooleanField()
class Meta:
model = Ingredient
fields = ('recipe_id', 'title', 'instructions', 'rules')
def __init__(self, current_user, *args, **kwargs):
super(IngredientForm, self).__init__(*args, **kwargs)
self.fields['recipe_id'].queryset = self.fields['recipe_id'].queryset.filter(user=current_user.id)
then instantiate the form like so
form = IngredientForm(current_user=request.user)
EDIT #1:
Passing in the user to the POST request form:
if request.method == "POST":
form = IngredientForm(request.POST, current_user=request.user)
if form.is_valid():
....
EDIT #2:
Try changing the init decleration to what is below and pop the user from the kwargs:
def __init__(self, *args, **kwargs):
current_user = kwargs.pop('current_user', None)
super(IngredientForm, self).__init__(*args, **kwargs)
if current_user:
self.fields['recipe_id'].queryset = self.fields['recipe_id'].queryset.filter(user=current_user.id)
I think this might solve your problems, leave the rest of the code the same as my answer above (where you create the forms)