我正在尝试创建一个addform来接受用户的输入并将其添加到我的模型中,但是该表单未显示在页面上,有人可以告诉我我做错了什么吗?这是forms.py,views.py和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>
答案 0 :(得分:1)
我认为问题在于您如何到达那里。
您的html中包含以下内容
dic_A = {"A": a, "B": b}
dic_C = {"E": e, "F": f}
这将提取html页面,但您希望dic_A = {"A": a, "B": b, "C": {"E": e, "F": f}}
会提取Django URL。
答案 1 :(得分:-1)
代替dict使用上下文
def add(request):
addform = forms.AddForm()
context= {'addform': addform}
return render(request, 'add.html', context)
请阅读此内容。 context