Wednesday, March 24, 2021

Re: Data from form is not being saved in DB

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.
On Wednesday, March 24, 2021 at 7:53:33 PM UTC+5:30 german....@gmail.com wrote:
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' %}

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/26022f75-aa53-4e2e-b12a-a6d32ff77612n%40googlegroups.com.

No comments:

Post a Comment