Django,  HTML,  Python

Django – Error in CreateView, the form does nothing, it does not save to the database

Yay! New post…

Yep, I came back to this, my abandoned blog, to tell you about how I solved an error I was having with Django and was driving me crazy for a some days…

But first, Django? What is Django ?. Well, I am not going to go into much detail, but I will tell you that it is a web development framework that I discovered not so long ago, but I liked it a lot. Complete websites with great functionality are obtained with very little effort. It is based on python, which is a plus, since I recommend everyone to learn this programming language that is leading the list of favorite programming languages ​​🙂

Among the building blocks of django, there are generic views, which are an easy way of creating listing, detail, edit, create and delete pages (among others) for entities in the database.

Well, I was testing a creation view (CreateView) and I saw that no matter how much I hit the “Create” button in my form, the new object that I had just created was not saved in the database. The page was reloaded, and in my django console the creation HTTP request was output, but nothing else. No error messages, no database updates. I tried everything, I googled and found no solution to my problem (neither in English nor in Spanish…)

My views.py file:

class RegionCreate(CreateView):
  template_name = 'obaju/region_form.html'
  model = Region
  fields = "__all__"

My urls.py file:

urlpatterns = [
...
  path('region/crear/', views.RegionCreate.as_view(), name="region_crear"),
...
]

My models.py file:

class Region(models.Model):
  nombre = models.CharField(max_length=50)
 
  def get_absolute_url(self):
    return reverse("region", kwargs={"pk": self.pk})
 
  def __str__(self):
    return "[" + str(self.id) + "] " + self.nombre

My region_form.html file:

<div class="col-md-9">
	<div class="box" id="crear">
		<h1>Crear región</h1>

		<form action "" method="" enctype="multipart/form-data">
{% csrf_token %}
			<div class="row">
{% include "obaju/form.html" %}

				<div class="col-sm-12 text-center">
					<button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i> Crear</button>
				</div>
			</div>
			<!−− /.row −−>
		</form>
	</div>

My form.html file:

<div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
	<span class="text-danger small">{{ field.error }}</span>
  </div>
  <label class="control-label col-sm-2">{{ field.label_tag }}</label>
  <div class="col-sm-10">{{ field }}</div>
</div>

Everything looked fine to me, or that’s what I thought. What could happen? A typo declaring the view? An error with the Django version I was using? A simple error in the form HTML source? Yes, exactly that… I was missing the method attribute value, in this case, POST:

<form action "" method="post" enctype="multipart/form-data">
{% csrf_token %}
	<div class="row">
{% include "obaju/form.html" %}

		<div class="col-sm-12 text-center">
			<button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i> Crear</button>
		</div>
	</div>
	<!−− /.row −−>
</form>

All that time checking and checking all of the python code, thinking the error should be there, since it was the language I had the least amount of experience on, but turns out the error was in the old and well-known HTML…

Sometimes, the errors are just in front of you, and you don’t see them. Sometimes you trust the things you do since a lot of time ago, thinking that you already mastered it, but these kind of things happen 🙂.

Hope this post helps someone who’s having the same problem that I did 😉.

See you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Esta web utiliza cookies propias y de terceros para su correcto funcionamiento y para fines analíticos. Al hacer clic en el botón Aceptar, acepta el uso de estas tecnologías y el procesamiento de tus datos para estos propósitos. Ver
Privacidad