[英]In Django, how do I access template values in the views.py?
I'm fairly new to Django and I'm working on a page that takes in user information. 我是Django的新手,正在开发一个包含用户信息的页面。 If all of the information is correct, it will proceed to the next page.
如果所有信息都是正确的,它将进入下一页。 However, if the user does not provide all given info, it will to refresh the page.
但是,如果用户未提供所有给定的信息,它将刷新页面。 My problem is that there are quite a bit of fields the user has to fill out and if the person misses any fields, I don't want them to have to re-type everything out.
我的问题是用户必须填写很多字段,如果此人错过任何字段,我不希望他们必须重新键入所有内容。 So my workaround for it is that in the views.py I created a dictionary and it populates it with the input names in the template.
因此,我的解决方法是在views.py中创建一个字典,并使用模板中的输入名称填充该字典。 However, when I go to run the code, it gives me an error saying that the values in my dictionary do not exist.
但是,当我去运行代码时,它给我一个错误,指出我的词典中的值不存在。 I'm now thinking that my dictionary is not actually accessing any of the template values.
我现在在想我的字典实际上并没有访问任何模板值。
Here is my template: 这是我的模板:
<!DOCTYPE html>
{% extends "Checklist/base.html" %}
{% block main_content %}
{% load static %}
<html>
<body>
<form action="{% url 'Checklist:signin_check' %}" method="post">
{% csrf_token %}
<ul style="list-style-type:none">
<li>
<label for="driver_first_name">Driver First Name:</label>
<input type="text" name="driver_first_name" value="" id="driver_first_name">
</li>
<li>
<label for="driver_last_name">Driver Last Name:</label>
<input type="text" name="driver_last_name" value="" id="driver_last_name">
</li>
<li>
<label for="driver_wwid">Driver WWID:</label>
<input type="text" name="driver_WWID" value="" id="driver_WWID" maxlength="8"
onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57">
</li>
<li>
<label for="co_driver_first_name">CO-Driver First Name:</label>
<input type="text" name="co_driver_first_name" value="" id="co_driver_first_name">
</li>
<li>
<label for="co_driver_last_name">CO-Driver Last Name:</label>
<input type="text" name="co_driver_last_name" value="" id="co_driver_last_name">
</li>
<li>
<label for="co_driver_wwid">CO-Driver WWID:</label>
<input type="text" name="co_driver_WWID" value="" id="co_driver_WWID" maxlength="8"
onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57">
</li>
<li>
<input type="submit" value="Continue">
</li>
</ul>
</form>
</body>
</html>
{% endblock %}
Here is the views.py: 这是views.py:
def signin_check(request):
driver_info_model = Driver()
if request.method == "POST":
driver_info_form = Driver_Form(request.POST)
c = {'driver_first_name':driver_first_name, 'driver_last_name':driver_last_name,
'driver_WWID':driver_WWID, 'co_driver_first_name':co_driver_first_name,
'co_driver_last_name':co_driver_last_name, 'co_driver_WWID': co_driver_WWID,}
if driver_info_form.is_valid():
driver_info_form.save()
return render(request, 'Checklist/checklist.html')
template = loader.get_template('Checklist/signin.html')
return HttpResponse(template.render(c, request))
any feedback would be greatly appreciated. 任何反馈将不胜感激。 Thanks!
谢谢!
Im not 100% sure as i'm fairly new to Django myself, but from what i've done previously you can get the POST data from the request that is passed in, like this: 我不是100%肯定的,因为我对Django相当陌生,但是从我之前所做的事情中,您可以从传入的请求中获取POST数据,如下所示:
request.POST['driver_first_name']
which raises an error if no data is present or from 如果没有数据或没有数据,则会引发错误
request.POST.get('driver_first_name', 'optionaldefaultvalue')
which returns None if no data is present in the specified field, or an optional default. 如果指定字段中没有数据,则返回None,或者返回一个可选的默认值。
It might also be easier to do what you are after with django's inbuilt forms 使用django的内置表单做您想做的事可能也更容易
However, when I go to run the code, it gives me an error saying that the values in my dictionary do not exist.
但是,当我去运行代码时,它给我一个错误,指出我的词典中的值不存在。 I'm now thinking that my dictionary is not actually accessing any of the template values.
我现在在想我的字典实际上并没有访问任何模板值。
From your views.py alone I'm guessing the exception you're running into is that you're assigning dictionary values that aren't defined. 仅从您的views.py,我猜测您遇到的例外情况是您正在分配未定义的字典值。 For example, in
'driver_first_name':driver_first_name
, Python is looking for a variable named driver_first_name
but you haven't defined it. 例如,在
'driver_first_name':driver_first_name
,Python正在寻找一个名为driver_first_name
的变量,但您尚未定义它。 The data you're looking for, as Justin alluded to, can be found in requests.POST
. 您正在寻找的数据,如贾斯汀提到,可以发现
requests.POST
。
One solution, while more verbose, illustrates what needs to be done: 一种解决方案虽然较为详细,但却说明了需要做的事情:
def signin_check(request):
driver_info_model = Driver()
if request.method == "POST":
driver_info_form = Driver_Form(request.POST)
driver_first_name = request.POST.get('driver_first_name', '')
driver_last_name = request.POST.get('driver_last_name', '')
driver_WWID = request.POST.get('driver_WWID', '')
co_driver_first_name = request.POST.get('co_driver_first_name', '')
co_driver_last_name = request.POST.get('co_driver_last_name', '')
co_driver_WWID = request.POST.get('co_driver_WWID', '')
c = {'driver_first_name': driver_first_name,
'driver_last_name': driver_last_name,
'driver_WWID': driver_WWID,
'co_driver_first_name': co_driver_first_name,
'co_driver_last_name': co_driver_last_name,
'co_driver_WWID': co_driver_WWID, }
if driver_info_form.is_valid():
driver_info_form.save()
return render(request, 'Checklist/checklist.html')
template = loader.get_template('Checklist/signin.html')
return HttpResponse(template.render(c, request))
My problem is that there are quite a bit of fields the user has to fill out and if the person misses any fields, I don't want them to have to re-type everything out.
我的问题是用户必须填写很多字段,如果此人错过任何字段,我不希望他们必须重新键入所有内容。
To address your second concern you'll need to deal with your HTML template. 为了解决您的第二个问题,您需要处理HTML模板。 Your input fields have a value of
""
, so any value you pass through your context is not going to reach any of them. 您的输入字段的值为
""
,因此您通过上下文传递的任何值都不会到达它们中的任何一个。 Luckily you're on the right path and you're quite close, so all you need to do is fill those values in. For example: 幸运的是,您处在正确的道路上并且非常接近,因此您所要做的就是填写这些值。例如:
<li>
<label for="driver_first_name">Driver First Name:</label>
<input type="text" name="driver_first_name" value="{{ driver_first_name }}" id="driver_first_name">
</li>
Note that {{ driver_first_name }}
is referencing the driver_first_name
that's being passed into the context. 请注意,
{{ driver_first_name }}
引用了要传递到上下文中的driver_first_name
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.