I’m just starting my adventure with Django. I wrote a small application in which, of course, there is a form that collects data from the user. This form in the standard version of Django doesn’t look nice. I found information about crispy-form on the Internet and added it to my project.
I managed to create a form that I like, but there is a problem. I cannot save the data retrieved from the form to the database. Previously, when I had a Django form, in the views.py file, the function handling this form saved the retrieved data to the database. Now that it’s crispy-form I don’t want to save.
My knowledge is too little to be able to do it, hence my request to you for help.
Can you take a look at the codes below and tell me what I’m doing wrong?
forms.py
from django.forms import ModelForm
from crispy_forms.helper import FormHelper
from .models import Firma, DaneKsiegowe
from django import forms
from django.urls import reverse
from crispy_forms.bootstrap import Field, TabHolder, Tab
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Fieldset
class FormNowaFirma(forms.Form):
nazwa = forms.CharField(required=True, max_length=120)
typ_ksiegowosc = forms.ChoiceField(
choices=(("ryczałt ewidencjonowany","ryczałt ewidencjonowany"),("ryczałt ewidencjonowany bez VAT","ryczałt ewidencjonowany bez VAT"),
("KPiR skala podatkowa","KPiR skala podatkowa"),("KPiR skala podatkowa bez VAT","KPiR skala podatkowa bez VAT"),
("KPiR podatek liniowy","KPiR podatek liniowy"),("KPiR podatek liniowy bez VAT","KPiR podatek liniowy bez VAT"),
("pełna księgowość","pełna księgowość"),("pełna księgowość bez VAT","pełna księgowość bez VAT")),
widget=forms.Select,
initial='2',
required=True
)
nip = forms.CharField(required=True, max_length=20)
imie = forms.CharField(max_length=30)
nazwisko = forms.CharField(max_length=50)
pesel = forms.CharField(max_length=11)
ulica = forms.CharField(required=True, max_length=50)
nr_budynek = forms.CharField(required=True, max_length=4)
nr_lokal = forms.CharField(max_length=4)
miasto = forms.CharField(required=True, max_length=50)
kod_p = forms.CharField(required=True, max_length=6)
gmina = forms.CharField(required=True, max_length=50)
powiat = forms.CharField(required=True, max_length=50)
wojewodztwo = forms.ChoiceField(
choices=(("Dolnośląskie","Dolnośląskie"), ("Kujawsko-pomorskie","Kujawsko-pomorskie"), ("Lubelskie","Lubelskie"),
("Lubuskie","Lubuskie"),("Łódzkie","Łódzkie"),("Małopolskie","Małopolskie"),("Mazowieckie","Mazowieckie"),
("Opolskie","Opolskie"),("Podkarpackie","Podkarpackie"),("Podlaskie","Podlaskie"),("Pomorskie","Pomorskie"),
("Śląskie","Śląskie"),("Świętokrzyskie","Świętokrzyskie"),("Warmińsko-mazurskie","Warmińsko-mazurskie"),
("Wielkopolskie","Wielkopolskie"),("Zachodniopomorskie","Zachodniopomorskie")),
widget=forms.Select,
initial='0',
required=True
)
segregator = forms.CharField(required=True, max_length=3)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id_nowa_firma'
self.helper.form_method = 'post'
self.helper.form_action = reverse('nowa_firma')
self.helper.add_input(Submit('submit','Zapisz firmę', css_class='btn-success'))
self.helper.form_class = 'form-horizontal'
self.helper.layout = Layout(
TabHolder(Tab('Dane firmy',
Fieldset('Dane firmy',
Field('typ_ksiegowosc', placeholder='Wybierz rodzaj księgowości',css_class="form-control"),
Field('nazwa', placeholder='Wpisz nazwę firmy',css_class="form-control"),
Field('segregator', placeholder='Wpisz nr segregatora',css_class="form-control"),
Field('nip', placeholder='Wpisz NIP firmy',css_class="form-control"),
Field('imie', placeholder='Wpisz imię włąściciela firmy',css_class="form-control"),
Field('nazwisko', placeholder='Wpisz nazwisko właściciela firmy',css_class="form-control"),
Field('pesel', placeholder='Wpisz PESEL właściciela firmy',css_class="form-control"))),
Tab('Adres',
Fieldset('Adres firmy',
Field('ulica', placeholder='Wpisz ulicę firmy',css_class="some-class"),
Field('nr_budynek', placeholder='Wpisz nr budynku firmy',css_class="some-class"),
Field('nr_lokal', placeholder='Wpisz nr lokalu firmy',css_class="some-class"),
Field('miasto', placeholder='Wpisz miasto firmy',css_class="some-class"),
Field('kod_p', placeholder='Wpisz kod pocztowy firmy',css_class="some-class"),
Field('gmina', placeholder='Wpisz gminę firmy',css_class="some-class"),
Field('powiat', placeholder='Wpisz powiat firmy',css_class="some-class"),
Field('wojewodztwo', placeholder='Wpisz województwo firmy',css_class="some-class")))
)
)
views.py
from django.shortcuts import render, redirect, get_object_or_404
from .forms import FormDane, FormNowaFirma
from .models import Firma, DaneKsiegowe
from django.template import loader
from django.template.context_processors import csrf
from crispy_forms.utils import render_crispy_form
def nowa_firma(request):
form_dane = FormNowaFirma(request.POST or None)
if form_dane.is_valid():
form_dane.save()
return redirect(index)
return render(request, 'form_nowa_firma.html', {'form_nowa_firma':form_dane})
Bug report
AttributeError at /nowa_firma/
‘FormNowaFirma’ object has no attribute ‘save’
Request Method: POST
Request URL: http://127.0.0.1:8000/nowa_firma/
Django Version: 5.0.4
Exception Type: AttributeError
Exception Value:
‘FormNowaFirma’ object has no attribute ‘save’
Exception Location: C:UsersMarcindjangobiurobiuroTaxpulpitBiuroviews.py, line 41, in nowa_firma
Raised during: pulpitBiuro.views.nowa_firma
Python Executable: C:UsersMarcindjangobiurovenvScriptspython.exe
Python Version: 3.12.2
Python Path:
[‘C:UsersMarcindjangobiurobiuroTax’,
‘C:UsersMarcinAppDataLocalProgramsPythonPython312python312.zip’,
‘C:UsersMarcinAppDataLocalProgramsPythonPython312DLLs’,
‘C:UsersMarcinAppDataLocalProgramsPythonPython312Lib’,
‘C:UsersMarcinAppDataLocalProgramsPythonPython312’,
‘C:UsersMarcindjangobiurovenv’,
‘C:UsersMarcindjangobiurovenvLibsite-packages’]
Server time: Sun, 28 Apr 2024 05:36:45 +0000
My knowledge is too little to be able to do it, hence my request to you for help. Can you take a look at the codes below and tell me what I’m doing wrong?
Marcin Lipiński is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.