0

enter image description hereI am trying to create an addform to take input from a user and add it to my model, however, the form is not showing on the page, can someone tell me what I am doing wrong? Here is the code for the forms.py, views.py and add.html:

forms.py

class AddForm(forms.Form):
    vehicle = forms.CharField(max_length=10)
    carrier = forms.FloatField()
    location = forms.ChoiceField(choices=[(1, 'Mathura Installation')])
    customer_code = forms.FloatField()
    zone = forms.ChoiceField(choices=[('NW', 'North West'),
                                      ('NCR', 'North Central'),
                                      ('SCR', 'South Central'),
                                      ('S', 'South'), ('N', 'North'),
                                      ('W', 'West'), ('E', 'East')
                                      ])
    quantity = forms.FloatField()
    load = forms.FloatField()
    rtkm = forms.FloatField(label='RTKM')
    rate = forms.ChoiceField(label='Rate ', widget=forms.RadioSelect, choices=[('avg', 'Average Rate'),
                                                                         ('user', 'User Rate')])

views.py

def add(request):
    addform = forms.AddForm()
    dict = {'addform': addform}
    return render(request, 'add.html', dict)

urls.py

from django.contrib import admin
from django.urls import path
from searchapp import views

urlpatterns = [
    path('', views.search, name='search'),
    path('add/', views.add, name='add'),
    path('admin/', admin.site.urls),
]

html - add.html

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
<head>
    <meta charset="UTF-8">
    <title>Transport Portal - Add Page</title>
</head>
<body>
<div class="header">
    <img src="{% static 'images/hpcl_logo.png' %}">
    <h1>Transportation Portal</h1>
</div>
<div class="topnav">
    <ul>
        <li><a href="home.html">Home</a></li>
        <li><a href="homehp.html">Search</a></li>
        <li><a class="active" href="add.html">Add</a></li>
    </ul>
</div>
<div class="add">
    <form method="POST">
        {% csrf_token %}
        {{ addform.as_p }}
    </form>
</div>
</body>
</html>

2 Answers2

1

I think the problem is how you're getting there.

You have the following in your html

<li><a class="active" href="add.html">Add</a></li>

This is going to pick up the html page but you want href="{% url 'add' %}" which will pick up the django url.

HenryM
  • 5,557
  • 7
  • 49
  • 105
  • Add I did like you suggested but couldnt see the form, I dont think there is a problem with the url because when i injected normal text using {{}} it shows there, just not displaying the form for some reason. – Shwetanshu RAj Jun 25 '19 at 08:35
  • I just tried to inject the form again but without the
    tags and it worked. But as soon as I put the {{addform}} in the
    tags it dissappears again. Can you help me with this?
    – Shwetanshu RAj Jun 25 '19 at 08:45
  • I've never seen that and don't know how it can happen – HenryM Jun 25 '19 at 08:50
  • Found the problem, the css file that I was using had form{display: none; } for some javascript thing that I had to do on another page – Shwetanshu RAj Jun 25 '19 at 08:56
-1

Instead of dict use context

def add(request):
    addform = forms.AddForm()
    context= {'addform': addform}
    return render(request, 'add.html', context)

Please read this. context

Talha Murtaza
  • 324
  • 1
  • 2
  • 17