Home > Software engineering >  Django not using the form tag
Django not using the form tag

Time:07-17

In my Django template I don't wanna use {{form]] tag. Is there any way I can save the HTML form to my models without using {{form}} tag?

myform.html

<form id="firstform" action="{% url 'saveview' %}" method="POST">
  {% csrf_token %}

  <input type="text" name="brand" id="brand">
  <input type="text" name="series" id="series">
  <input type="text" name="model" id="model">

  <input type="submit"  id="save-button" name="save-button" value="SAVE">

 </form>

views.py

def saveview(request):

   return render(request,'myform.html')

CodePudding user response:

Yes, you can use Django forms without rendering {{ form }} at all - you just need to make sure that your input names match the field names that the form expects. If you need more control over your form rendering then you can loop through each field like so:

{% for field in form %}
<!-- access field properties, value, etc. -->
{% endfor %}

Edit Is that all your views.py file contains? You need to create the form and pass request.POST to it for the form to do anything.

CodePudding user response:

You can Create HTML page only contains the Form and by using the include tag you can reuse it in many HTML pages

form.html

  <input type="text" name="brand" id="brand">
  <input type="text" name="series" id="series">
  <input type="text" name="model" id="model">

home.html

...
{% include "form.html" %}
...
  • Related