L
L
Lolo Penguin2019-11-23 15:11:55
Django
Lolo Penguin, 2019-11-23 15:11:55

How to sync a django form with a database?

I recently started to figure out what djanga is, so I ask you to explain it as simply as possible)
In Django 2.2, I create a form that, after filling, should insert all the entered data into the database.
But this is not happening!
For many hours I can not find a solution, I beg you to help.
models.py

from django.db import models
from django.forms import ModelForm

class compani_information(models.Model):
    name = models.CharField('Название', max_length = 40)
    email = models.URLField('Сайт', blank = True, null = True, help_text = 'не обязат.')
    site = models.EmailField('Элекстронная почта')
    tel = models.CharField('Телефон', max_length = 25)
    tel2 = models.CharField('Телефон (доп.)', max_length = 25, blank = True, null = True, help_text = 'не обязат.')
    inn = models.CharField('ИНН организации', max_length = 16)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'компания'
        verbose_name_plural = 'компании'

views.py
from .models import compani_information
from .forms import compani_information_form
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

def compani_reg(request):
    if request.method == "POST":
        form = compani_information_form(request.POST)

        if form.is_valid():
            # form.save()
            new_hydrant = form.save()
            # return HttpResponseRedirect(reverse('reg.html', args=(new_hydrant.id,)))
            return HttpResponseRedirect('reg.html')
            # return render(request, 'lk.html')
    else:
        return render(request, 'reg.html', {"form": compani_information_form()})


# def compani_reg_save_post(request):
#    name = request.POST["name"]
#    email = request.POST["email"]
#    site = request.POST["site"]
#    tel = request.POST["tel"]
#    tel2 = request.POST["tel2"]
#    img = request.POST["img"]

forms.py
from django.forms import ModelForm
from .models import compani_information

# Create the form class.
class compani_information_form(ModelForm):

    class Meta:
        model = compani_information
        # инпут и их последовательность, которые будут выводиться
        fields = '__all__'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2019-11-23
@ledo02

https://tutorial.djangogirls.org/ru/
closer to the last article

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question