我希望处理我在django中使用HTML创建的表单,但我找不到从输入中获取值的正确方法。
这是我的HTML表单:
<form action="" class="form-inline" method="post">
{% csrf_token %}
<!-- malfunction description -->
<label class="requiredField" for="malfunctionDescription">תאור
התקלה</label>
<br/>
<textarea class="form-control" style="width: 100%; margin-bottom: 3px"
id="malfunctionDescription"
name="malfunctionDescription"
rows="5">
</textarea>
</form>
这是我的view.py,很遗憾是空的:
def index(request):
error = ''
if request.method == 'POST':
status = request.POST['status']
rank = request.POST['rank']
opener = request.POST['MalfunctionOpener']
handler = request.POST['malfunctionHandler']
system = request.POST['system']
unit = request.POST['unit']
opening_date = request.POST['openingdate']
closing_date = request.POST['closingdate']
description = request.POST['malfunctionDescription']
solution = request.POST['malfunctionSolution']
summary = request.POST['malfunctionSummary']
find_description = Malfunction.objects.filter(description=description)
if find_description:
error = 'This malfunction is already stored in the data base'
else:
Malfunction.objects.create(status=status, rank=rank, opener=opener, handler=handler, system=system,
unit=unit, openingDate=opening_date, closingDate=closing_date, solution=solution,
summary=summary, description=description)
else:
error = 'Something went wrong'
return render(request, 'resources-management/home.html')
我的主要目标是从表单中获取此信息并创建一个新对象,将其推送到数据库。 如果我理解正确创建这个对象,我需要做类似的事情:
Object_name.object.create(information=information)
但我不知道如何获取信息,我需要像烧瓶中的 request.form [&#39; name&#39;]
谢谢!
----编辑1 - url.py ----
这是主要的url.py
urlpatterns = [
url(r'^', include('elbit_ils.urls')),
url(r'^admin/', admin.site.urls),
url(r'^resources-management/', include('resources_management.urls')),
url(r'^registration-login/', include('registration_login.urls')),
url(r'^contact/', include('contact.urls')),
这是应用程序url.py
urlpatterns = [
url(r'^$', IndexView.as_view(), name="my_list"),
url(r'^(?P<pk>\d+)$', DetailView.as_view(model=Malfunction, template_name="resources-management/malfunction.html")),
]
- 编辑2:indexview -
类IndexView(CreateView):
context_object_name = 'my_list'
template_name = 'resources-management/home.html'
queryset = Malfunction.objects.all()
fields = '__all__'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['contacts'] = Contact.objects.all().order_by("firstname")
context['malfunctions'] = Malfunction.objects.all().order_by("-openingDate")
context['systems'] = System.objects.all().order_by("systemname")
context['units'] = Unit.objects.all().order_by("unitname")
# And so on for more models
return context
答案 0 :(得分:1)
如果你有一个像
这样的表格<form action="" method="post">
<input type="text" name="username" />
<input type="submit" />
</form>
请注意name
元素中的input
属性。然后
在django
中,您将获得request.POST
中的数据。所以您可以
request.POST['username']
答案 1 :(得分:1)
您应该能够以下列方式访问数据
def index(request):
# All the post data is stored in `request.POST`
desc = request.POST["malfunctionDescription"]
您可以查看如何在文档中使用Forms,以了解验证所收到数据的最佳方法。