S
S
serik2018-06-01 18:48:32
Django
serik, 2018-06-01 18:48:32

How to fill the model with data through the form on the site, and not through the DJANGO admin panel?

I have several models that need to be filled in by users from the site, please help me sort it out using one example. I made some variant, but I stick on the date, pictures and comboboxes.
Model:

class Company(models.Model):
    user = models.ForeignKey(User, verbose_name=u"Директор", blank=True, null=True, on_delete=models.CASCADE)
    full_name = models.CharField("Полное наименование компании", default='', max_length = 180)
    name = models.CharField("Краткое название компании", default='Тимфорт', max_length = 80)
    adress = models.CharField("Адрес", default='г. ', max_length = 180)
    phone = models.CharField("Телефон", default='', max_length = 180)
    email = models.CharField("Электронная почта", default='', max_length = 180)
    background = models.ImageField("Фон", upload_to="annex/img/%Y/%m/%d", default='')
    date_push = models.DateTimeField("Дата публикации")

Example:
views.py
def createSpecific(request):
    if request.method == "POST":
        tom = Company()
        users = User()
        users.user = request.POST.get("user")
        tom.full_name = request.POST.get("full_name")
        tom.name = request.POST.get("name")
        tom.adress = request.POST.get("adress")
        tom.phone = request.POST.get("phone")
        tom.email = request.POST.get("email")
        tom.background = request.POST.get("background")
        tom.save()
    return HttpResponseRedirect("/")

Form.html:
<form method="POST" action="/create/" enctype="multipart/form-data">
                {% csrf_token %}
                <p>
                    <input type="text" name="user" />
                </p>
                <p>
                    <input type="text" name="full_name" />
                </p>
                <p>
                    <input type="text" name="name" />
                </p>
                <p>
                    <input type="text" name="adress" />
                </p>
                <p>
                    <input type="text" name="phone" />
                </p>
                <p>
                    <input type="text" name="email" />
                </p>
                <p>
                    <input type="file" name="background" />
                </p>
                <input type="submit" value="Сохранить" >
            </form>

But I can’t figure out how to select users through the combobox, select an image and enter data =(
Please help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question