Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-从POST请求中获取值_Python_Django_Post - Fatal编程技术网

Python Django-从POST请求中获取值

Python Django-从POST请求中获取值,python,django,post,Python,Django,Post,我有以下django模板(http://IP/admin/start/ 分配给一个称为“视图”的假设视图: {sources%%中的源的% {{source}} {%csrf_令牌%} {%endfor%} sources是视图中引用的Django模型的对象。每当单击“开始”提交输入时,我希望“开始”视图在返回呈现页面之前在函数中使用{{source.title}数据。如何将发布的信息(在本例中为隐藏输入)收集到Python变量中?如果需要在前端执行某些操作,可以响应表单的onsubmit事件

我有以下django模板(http://IP/admin/start/ 分配给一个称为“视图”的假设视图:

{sources%%中的源的%
{{source}}
{%csrf_令牌%}
{%endfor%}

sources
是视图中引用的Django模型的
对象。每当单击“开始”提交输入时,我希望“开始”视图在返回呈现页面之前在函数中使用
{{source.title}
数据。如何将发布的信息(在本例中为隐藏输入)收集到Python变量中?

如果需要在前端执行某些操作,可以响应表单的onsubmit事件。如果您只是发布到admin/start,您可以通过request对象访问视图中的post变量。request.POST是POST变量的字典

了解您的视图收到的请求对象:

此外,隐藏字段还需要可靠的名称和值:

<input type="hidden" name="title" value="{{ source.title }}">

对于django表单,您可以这样做

form = UserLoginForm(data=request.POST) #getting the whole data from the user.
user = form.save() #saving the details obtained from the user.
username = user.cleaned_data.get("username") #where "username" in parenthesis is the name of the Charfield (the variale name i.e, username = forms.Charfield(max_length=64))

另一个答案帮助我解决了一个问题,我在问这个问题之前要问你(名称/值html选择器的定义),所以我将继续并标记这个答案为已接受。谢谢你的帮助。:)我使用Django表单,其中没有独立的
输入
,只有
{{form}}
,而我的表单只有一个文本输入(charfield)。如何访问其数据?@Nitwit
name
属性将是您分配给charfield的属性的名称。如果不是100%,请在加载表单的浏览器中按F12,然后选择输入元素以查看Django为name属性提供了什么。答案中的UserLoginForm是表单的名称
request.POST.get("title", "")
form = UserLoginForm(data=request.POST) #getting the whole data from the user.
user = form.save() #saving the details obtained from the user.
username = user.cleaned_data.get("username") #where "username" in parenthesis is the name of the Charfield (the variale name i.e, username = forms.Charfield(max_length=64))