Order form with Python and Django

Asked

Viewed 1,025 times

2

I’m starting my studies with Python and Django. After creating several simple CRUDS, I tried to create a more complex form using several classes, but was unsuccessful.

I want to create a screen for typing orders with customer information (the customer can be registered along with the order), a list with sellers and add products. To add the products I think about creating a modal window that refers to the products or an autocomplete field in the form itself with an add button.

Doubts:

  • It is good practice to create manual forms and persist data manually?

  • What is the best way to create a form that has information from multiple models (Order, Product and Seller)?

I’m using Python 3.5, Django 1.10 on Linux.

Thanks for your help.

  • Hello - your question was not very clear. I suggest you read the class documentation Forms of Django https://docs.djangoproject.com/pt-br/1.10/ref/forms/api/

  • Leonardo, thanks for the feedback. I edited my question, just wanted the path I must follow to create a more complex form. I had already browsed the link you sent, but what is the best option? Thanks.

1 answer

2


I suggest you use the API of Forms django.

I’m not going to get into the merits of how to build a form because it doesn’t seem like the scope of the question, so I’m just going to emphasize some of the benefits that we see:

1. Validators for the form and fields

Let Django take care of validations of data types for you based on the type of field chosen in Model.

2. Various formatting options

You can use the methods to display the form as list or text, in addition to customizing the template tags to further customize your style. You can also iterate field by field to customize your view within the template, for example:

index.html

<html>
<head><title>Meu site</title></head>
<body>
<h1>Aqui vai o meu formulário:</h1>
{% for field in form %}
    <p style="color:red">Campo em vermelho num parágrafo com filter: {{ field|meu_filter_personalizado }}</p>
{% endfor %}
</body>
</html>
  • The manual form idea, I’ve ruled it out. With respect to the second item, your suggestion would be to create a form for each model, can I change the layout or just use {{ form.as_table }} or {{ form.as_p }}? I’m still confused about the standard form and the custom form (template). Thanks for the feedback.

  • Yes, use a form for model. There are several ways to customize the fields, using template tags/Filters (https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/) or even iterating field by field and printing in HTML however you want (https://docs.djangoproject.com/pt-br/1.10/ref/forms/api/#accessing-the-Fields-from-the-form)

  • Thanks Leonardo. I will study the links you sent. If you have any questions, I return.

  • Leonardo, I studied both links. In custom-template-tags, I can create "functions" like converting a string to minuscule or uppercase... Okay, they are useful.

  • In the Forms Apis, I’ve already been able to understand how to unite multiple classes into a single class using multiple inheritance. Perfect, I made the examples in the shell, I understood the concept of bound Forms and Unbound, wonder. Now the main question would be regarding the positioning of the fields in html that is generated automatically. I couldn’t figure out where to place my fields in the formaulary. Ex.: In the first line I want to be displayed the customer name and email information, in the second address, number, neighborhood... I’m sorry if my question is obvious, but I’m inciting...

  • I updated the answer with a field-by-field iteration for maximum customization. If you want to choose a field in particular, can add a if compare if the field has the name you are looking for to customize

  • Could I direct-reference the form’s Fields? Remove for and insert the Fields into Divs? So I could position every field on the page. Once again, I thank you for your great good will and help. Thank you. Ex.: <div>{form.name}</div><div>{form.endereco}}</div>

  • Leonardo, I think I complicated something that was very simple. I managed to use the Fields one by one on the page without it. Thank you very much for your guidance. If you have any questions, I will return.

  • Arrange! If the answer answered your question, please remember to accept it at the check above. If you had something in the comments that was useful to you and want to edit my reply to add, better yet. :)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.