In my application I'm trying to update an Object with the Class Based View UpdateView and save it to the database. This is my code:
models.py:
class Toy(models.Model):
#Relation
list = models.ForeignKey(GiftingList, on_delete=models.DO_NOTHING)
donor = models.ForeignKey("auth.User", on_delete=models.DO_NOTHING)
#Attribute
name = models.CharField(max_length=30, blank=False, help_text="Spielzeugnamen eingeben")
description = models.TextField(max_length=200, blank=False, default="", help_text="Spielzeug eingeben")
image = models.ImageField(upload_to="upload/images/", blank=False, help_text="Bild hochladen")
def __str__(self):
return self.name
urls.py:
app_name = 'toys'
urlpatterns = [
...
path("toys/<int:pk>/update/", ToyUpdateView.as_view(), name="ToyUpdate"),
...
]
views.py:
class FormSubmittedInContextMixin(FormMixin):
def form_invalid(self, form):
return self.render_to_response(self.get_context_data(form=form, form_submitted=True))
class ToyUpdateView(UpdateView, FormSubmittedInContextMixin):
model = Toy
form_class = ToyForm
template_name = "main/updatetoy.html"
success_url = reverse_lazy('toys:GiftingListOverview')
forms.py:
class ToyForm(forms.ModelForm):
class Meta:
model = Toy
widgets = {
#'list': forms.ModelChoiceField(queryset=GiftingList.objects.label(), required=True),
'list': forms.TextInput(attrs={'class': 'form-control'}),
'donor': forms.TextInput(attrs={'class': 'form-control'}),
'name': forms.TextInput(attrs={'class': 'form-control'}),
'description': forms.TextInput(attrs={'class': 'form-control'}),
'image': forms.TextInput(attrs={'class': 'form-control'}),
}
fields = ('list', 'donor', 'name', 'description', 'image')
HTML Template:
{% extends "main/base.html" %}
{% block title %}Spielzeug bearbeiten{% endblock %}
{% block content %}
<div class="container">
<h3>Spielzeug {{ toy.name }} bearbeiten</h3>
<form action="{% url 'toys:ToyUpdate' toy.id %}" method="post" enctype='multipart/form-data'>
{% csrf_token %}
{% include 'main/toy_form.html' %}
<a href="{% url 'toys:ToyDetail' toy.id %}" class="btn btn-primary">Zurück<a>
<button type="submit" class="btn btn-success">Speichern</button>
</form>
</div>
{% endblock %}
toy_form.html:
<section class="django-forms">
<div class="form-group">
<b>Liste:</b>
{{ form.list }}
<div class="invalid-feedback">
{{ form.label.errors }}
</div>
</div>
<div class="form-group">
<b>Author:</b>
{{ form.donor }}
<div class="invalid-feedback">
{{ form.user.errors }}
</div>
</div>
<div class="form-group">
<b>Name:</b>
{{ form.name }}
<div class="invalid-feedback">
{{ form.details.errors }}
</div>
</div>
<div class="form-group">
<b>Beschreibung:</b>
{{ form.description }}
<div class="invalid-feedback">
{{ form.label.errors }}
</div>
</div>
<div class="form-group">
<b>Image:</b>
{{ form.image }}
<div class="invalid-feedback">
{{ form.label.errors }}
</div>
</div>
</section>
When I change the title in the form, it immediately also changes the title of the tag accordingly, but it doesn't redirect me, which it should do if the validation was successful. Also, when I manually go back to the overview, the changes aren't saved.
I have another Class, in which this scenario works as expected. I can't figure out why it doesn't work with this class.
The terminal does not show an error. The HTML-Code is 200. When I update and save this other functioning class, it shows the code 302. Do you have any idea?