I am having challenges whenever i try to update the form is not working , it jumps to else branch try all measure to check for error
view part:
def clients_edit(request, pk):
client = Client.objects.filter(created_by=request.user).get(pk=pk)
if request.method == 'POST':
form = AddClientForm(request.POST, instance=client)
if form.is_valid():
form.save()
messages.success(request, "The changes were saved.")
return redirect('clients_list')
else:
form = AddClientForm(instance=client)
return render(request, 'clients/clients_edit.html', {'form': form})
url : path('<int:pk>/edit', views.clients_edit, name='clients_edit'),
html:
<form method="POST" action=".">
{% csrf_token %}
{{ form.as_p }}
<button class="py-4 px-8 bg-teal-500 text-white rounded-xl">Submit</button>
</form>
form.py :
class AddClientForm(forms.ModelForm):
class Meta:
model = Client
fields = ('name', 'email' , 'desription',)
Side note: When posting code here, please surround each file’s code between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. This forces the forum software to keep your code properly formatted. (I have taken the liberty of editing this post for you.)
Have you verified that the instance of Client you are trying to edit was actually created by the user currently logged on?
Are you getting any error messages on the console where you’re running runserver?
Can you post your Client
model?
You might want to add an else
clause on the if form.is_valid()
conditional to print the errors generated within the form. That might help identify the issue.
no error message displayed
— from django.db import models
Create your models here.
from django.contrib.auth.models import User
from django.db import models
from team.models import Team
Create your models here.
class Client(models.Model):
name = models.CharField(max_length=120)
email = models.EmailField()
desription = models.TextField(blank=True, null=True)
created_by = models.ForeignKey(User, related_name=‘clients’, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
def __str__(self):
return self.name