Update object with model form inside the view - django

I need to update the object with its model form and passed the instance as described in the django doc. However I am having these problems when saving the form:
When I change the title and submit the form, it gives me an error that the image field is required and image field gets blank. However when the image is changed, the title field is not blank and it does not give any error.
If everything works and submits the form, it creates a new snap object instead of updating the instance object.
In the views I tried using both the obj.save() and obj.update(), but nothing helped. Please help me how to solve this problem. I will really appreciate your help Thank you.
form:
class SnapForm(forms.ModelForm):
class Meta:
model = Snap
fields = ['title', 'description', 'image', 'upload_date']
view:
def admin_snap_settings(request, snap_id):
if not request.user.is_admin:
return render(request, 'admin_login_invalid.html')
else:
instance = Snap.objects.get(id=snap_id)
if request.user == instance.user:
if request.method == "POST":
form = SnapForm(request.POST, request.FILES, instance=instance)
if form.is_valid():
form.save()
return HttpResponseRedirect('/custom123user/admin/snapview')
else:
form = SnapForm(instance=instance)
return render(request, 'admin_snap_settings.html', {
'form': form
})
else:
return render(request, 'wrong_user.html')

After hours of debugging and scratching head... I realized that the url of the action for the form was pointing to admin_snap_add() view and not to the admin_snap_settings() view.
Hope this will help someone so dumb as me. If you guys want me to delete this question please inform me. Thank you for your time.

Related

How to update data in Django when getting unique field error

first thing first I'm sorry for my bad english. I hope so u can understand me easily.
I'm trying to make a blog with django but there's a place I can't solve. I used unique slug field instead of id for url, whenever I want to update the data I get the UNIQUE constraint failed: post_post.url_text error (url_text is slugfield variable name). Here is the my model,
and the Form looks like this,
At first I followed a way to update the data here:
#login_required(login_url='login')
def post_update(request, url_text=None):
post = get_object_or_404(Post, url_text=url_text)
form = PostWrite(request.POST or None, instance=post)
if form.is_valid():
post_author = request.user
post_title = form.cleaned_data.get('post_title')
post_content = form.cleaned_data.get('post_content')
post_tags = form.cleaned_data.get('tags')
post = Post(post_author=post_author, post_title=post_title, post_content=post_content, tags=post_tags)
post.save()
context = {
'post': post,
'form': form,
}
return render(request, 'post/re_write.html', context)
and I got the error I mentioned at the beginning of the post. Then I found a solution like this in the forums,
if form.is_valid():
form.save()
This time it does not give any error but does not update the data. Despite hours of research, for some reason I could not find a tangible solution. I wanted to ask you esteemed coders as a last resort and I look forward to your help.
The issue is that you're creating a new post with the following code while this view appears to be an update:
post = Post(post_author=post_author, post_title=post_title, post_content=post_content, tags=post_tags)
post.save()
Instead, you should utilize the modelform you're already using to save the changes to the instance:
if form.is_valid():
post = form.save()
I'm guessing maybe because you weren't capturing the post from form.save() the rendered template appeared to not have the data updated because the instance passed into the template was from before the changes.
Other issue:
You're overriding the save method, but not always calling super().save. This means that you're only saving the post when the url_text property is not set. Instead always call super().save
def save(self, *args, **kwargs):
if ...
# other stuff
super().save(*args, **kwargs)

Problems when obtaining an id, sent by a form

Good evening, I am trying to get the id of my model Note that is sent by means of a form, but when I put form.id it tells me that id is not defined, try to get it through the user session but it says that it was not found.
def add_book(request):
template_name = 'books/create_note.html'
book = get_or_create_book(request)
form = NoteForm(request.POST)
if request.method == 'POST' and form.is_valid():
note = Note.objects.get(pk=form.pk)
book.notes.add(note)
form.save()
return redirect('books:book')
return render(request, template_name, {
'form': form,
})
and this is the form
class NoteForm(ModelForm):
class Meta:
model = Note
fields = (
'title', 'nota'
)
labels = {
'title': 'Titulo',
'nota': 'Nota',
}
try creating an instance of my Note model but when it comes time to create it tells me it is empty.
I'm new to Django, but I had similar problems that frustrate me. not sure if I have the hang of it yet, but I think what might be happening is that when you first go to the page there is a GET request, so your if statement misses it. It then it reaches the last line and goes to template_name without the form being assigned so the form never gets a Post requests. In the terminal you can see the POST and GET requests. I ended up also printing out request.method a lot before and after if statements just to help trace what was going on.
else:
form=NoteForm()
Then your return render(request,....
making sure it goes back to the correct html page.
The thing that worked for me eventually was something like
def Search_Page(request):
if request.method=='POST':
form = Search_Page_Form(request.POST)
if form.is_valid():
do some stuff and save the change to the model
return(redirect('mainapp:Evaluate_Page'))
else:
form=Search_Page_Form()
return render(request, 'mainapp/Search_Page.html', {'form': form})

How to add a value to an existing model record with the use of Django Forms?

I'm trying to make a form that adds points to a user's account. This form chooses which user and it should add it based on the value added. I don't know what I did wrong. No hints in the terminal to show what I did wrong. I just doesn't submit. The only indication that I screwed up is it shows that !form.is_valid based on the error message that I set.
Here is my forms.py:
class addpointForm(forms.ModelForm):
add_point_field = forms.IntegerField(widget=forms.NumberInput)
class Meta:
model = Points
fields = ['user']
Here is my views.py:
def pointform(request):
if request.method=='POST':
form = addpointForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
messages.success(request, 'Success! Points has been added!')
instance.points += addpointForm.add_point_field
instance.save()
else:
messages.error(request, 'Oh no! Points has an error!')
form = addpointForm()
return render (request,'users/addpoints.html',{'form':form})
I just some help to point me in the right direction. Any help is appreciated thank you.

Why won't my text posts save, it worked before?

My text posts were working before adding a notification feature but now they aren't, I have a debug print but instead of seeing the post object, I'm seeing the user that posted it. I haven't changed anything other than getting the post id, timesince and the notify function, I've used the exact same method with my picture and video posts and they work fine, so it doesn't make sense to me. I've got a debug print in the notification function to make sure the post type and id have been passed correctly and those indeed show the id and type
views.py
#login_required()
def text_post(request):
if request.method == "POST":
form = TextPostForm(request.POST)
if form.is_valid():
t_post = form.save(commit=False)
t_post.author = request.user
t_post.save()
id = t_post.id
time = timesince
print(t_post) # t_post shows the user instead of the post
followers = list(request.user.followers.all())
notify(followers,
request.user,
f"{request.user.username} posted {time}",
id,
'text'
)
print(t_post)
#redirect to last page
return redirect('home')
else:
form = TextPostForm()
post_type = 'text'
return render(request, 'create_post.html', {'form': form, 'post_type': post_type})
forms.py
class TextPostForm(forms.ModelForm):
class Meta:
model = TextPost
fields = ('description','privacy', 'categories')

How to edit/add objects using the same django form?

Hey, I've searched around to do this (particularly this Q: Django edit form based on add form?) but I just can't get it to work.
The problem I'm having is that the form always creates a new object, instead of modifying the existing one.
This is my code:
def new_task(request, task_id=None):
if task_id is not None:
task = Task.objects.get(pk=task_id)
else:
task = Task()
if request.method == 'POST': # If the form has been submitted...
form = TaskForm(request.POST, instance=task)
if form.is_valid():
form.save();
return tasks(request, 'Task #%s created successfully.' % (task.id))
else:
form = TaskForm(instance=task)
return custom_render('user/new_task.html',
{'form': form},
request);
Any clues on what I'm missing? Thanks
Edit: added form definitions.
class TaskForm(ModelForm):
description = CharField(max_length = 1500,
widget= forms.Textarea(attrs={'class':'task-description'}),
required=True)
class Meta:
model = Task
Ok, after a nice night of debugging i found out what the problem was. Quite stupid actually.
The problem was that on submit, task_id was None.
I worked it out by doing:
On the form I added: <form action="{% url App.myapp.views.new_task **task_id** %}"
On my views I added:
return custom_render('user/new_task.html',
{'form': form, 'submit': submit, 'task_id':task.id},
request)
That was it. A newbie mistake. If someone out there knows a nicer way I'm open to suggestions.