Data from form is not being saved in DB

39 views
Skip to first unread message

Герман Свердлов

unread,
Mar 24, 2021, 10:23:33 AM3/24/21
to Django users
Hi all!

I'm newbie in Django, still trying to figure out how everything works and this is my first back end framework, that I'm using. Currently building an app like booking.com but for commercial property.

Among others I have models that represent office centers and office spaces this is one-to-many relationship (1 office, many spaces). I've made a form that adds spaces to a particular office, but this form doesn't seem to save data.

I'm stuck on the following:
- Am I right that I need to point an action attribute in the form to specify view which will save form data into DB? If yes, I'm struggling how to do that, because I'm getting errors ReverseMatch
- If this is not about action attr, what am I missing?

Thank you in advance
German

***
models.py
from django.db import models
from django.urls import reverse

class Office(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
image = models.ImageField(null=True, blank=True, upload_to='offices/')
description = models.TextField(max_length=400, blank=True)
address = models.CharField(max_length=200, blank=True)
minprice = models.DecimalField(max_digits=6, blank=True, decimal_places=1)
size = models.IntegerField(blank=True)
worktime = models.CharField(max_length=200, blank=True)

class Meta:
verbose_name_plural = 'offices'

def get_absolute_url(self):
return reverse("office:office", kwags={'id': self.id})

def __str__(self):
return self.name

class Space(models.Model):
office = models.ForeignKey(
Office,
on_delete=models.CASCADE,
to_field='slug'
)
name = models.CharField(max_length=200, blank=True)
size = models.IntegerField(blank=True)
term = models.CharField(max_length=100, blank=True)
floor = models.IntegerField(blank=True)
window = models.BooleanField(blank=True)
price = models.DecimalField(max_digits=100, blank=True, decimal_places=2)

class Meta:
verbose_name_plural = 'spaces'

def get_absolute_url(self):
return reverse("office:space", kwags={'id': self.id})

def __str__(self):
return self.name

views.py
def space_edit_view(request, slug):
     office = Office.objects.get(slug=slug)
     form = UpdateSpaceForm(initial={'office': office.slug})
     if request.method == "POST":
         if form.is_valid():
         form.save()
         return redirect('office:office', slug=slug)

     context = {
         "form": form
     }

return render(request, "space_edit.html", context)

urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings # new
from django.conf.urls.static import static # new
from .views import OfficeDetailView, OfficeEditView, space_edit_view

app_name = 'office'

urlpatterns=[
     path('offices/<slug:slug>', OfficeDetailView.as_view(), name='office'),
     path('offices/<slug:slug>/edit', OfficeEditView.as_view(), name='office_edit'),
     path('offices/<slug:slug>/spaces/edit', space_edit_view, name='space_edit'),
]

if settings.DEBUG: # new
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

space_edit.html
{% include 'header.html' %}
<section class="grid-container margin-top-3">
<div class="grid-x grid-padding-x">
<h1 class="cell">Add new office space</h1>
<form action="{% url "space_edit" slug %}" class="cell margin-top-3" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input class='button' type="submit" value='Submit'>
</form>
</div>
</section>
{% include 'footer.html' %}

Prashanjeet Halder

unread,
Mar 24, 2021, 10:29:31 PM3/24/21
to Django users
Hello,
Answer to your first question: You don't have to explicitly mention action attr. without that too it is possible to achieve the same operation.
you can check "practice04" in this Repository :  

For reverse match error:
specifying app_name under urls.py file helps to debug such errors.

Ryan Nowakowski

unread,
Mar 28, 2021, 12:06:45 PM3/28/21
to django...@googlegroups.com
Please post the full error including the traceback and any additional information.

Kunal Solanke

unread,
Mar 28, 2021, 12:13:05 PM3/28/21
to Django users
To use name urls, you should use this format
app_name:name
Where name is the name your assigned to the path while defining urls

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/DE3510B0-58D9-4675-80B4-4297C38B0B21%40fattuba.com.
Reply all
Reply to author
Forward
0 new messages