Django:HTML表单操作使用2个参数定向到视图(或URL?) [英] Django : HTML form action directing to view (or url?) with 2 arguments
问题描述
大约一周前开始学习django,然后碰壁.非常感谢您的启发...
Started learning django about a week ago and ran into a wall. Would really appreciate any enlightenment...
models.py
models.py
class data(models.Model):
course = models.CharField(max_length = 250)
def __str__(self):
return self.course
html
将models.course中的对象转换为schlist
Converted the objects in models.course to schlist
<link rel="stylesheet" type="text/css" href="{% static '/chosen/chosen.css' %}" />
<form action={% views.process %} method="GET">
<div>
<h4 style="font-family:verdana;">First Course: </h4>
<select data-placeholder="Course" style="width:350px;" class="chosen-select" tabindex="7">
<option value=""></option>
{% for item in schlist %}
<option> {{ item }} </option>
{% endfor %}
</select>
</div>
</br>
<div>
<h4 style="font-family:verdana;">Second Course:</h4>
<select data-placeholder="Course" style="width:350px;" class="chosen-select" tabindex="7">
<option value=""></option>
{% for item in schlist %}
<option> {{ item }} </option>
{% endfor %}
</select>
</div>
</br>
<input type="submit" value="Compare!" />
</form>
urls.py(我怀疑是否可行..
urls.py (having my doubts if this works..)
urlpatterns = [
url(r'^(\d+)/(\d+)$',views.process, name = 'process'),
]
view.py
def process(request,q1 ,q2):
obj1= get_object_or_404(Schdata, course = q1)
obj2= get_object_or_404(Schdata, course = q2)
........
想知道表单动作是否有可能将动作定向到
Was wondering if it is possible for the form action to direct the action to
选择了2个参数的(1)view.py或(2)url.py(最终到view.py)?
(1) view.py or (2) url.py (and eventually to a view.py) with 2 arguments selected?
如果是这样,表单动作应该如何?{{view?}}或{{url?}}.我是否错过了HTML中参数的定义?
If so how should the form action be? {{view ?}} or {{url ?}}. Am I missing out the definition of my arguments in my HTML?
指向views.py:
Directing to views.py:
用户输入为 CharField
,可以使用 get_object_or_404
来获取模型pk.但是,在定义我的urls.py时,由于我的url参数是主键,因此会出现Noreverse错误.
User input is CharField
, could use get_object_or_404
to get the model pk. However when defining my urls.py I would get a Noreverse error as my url arguments is the primary key.
指向urls.py:
URL参数是主键.从我的角度来看,我需要先将用户输入的 Charfield
转换为pk,然后再将其传递给urls.py
Url arguments is primary key. From the way I see it, I need to magically convert my User input Charfield
to a pk before passing it to urls.py
在Django中是否有用于 get()
的(或)函数?例如 get_object_or_404(pk = q1或course = q1)
?
Is there a (or) function for get()
in django? E.g get_object_or_404(pk = q1 or course = q1)
?
非常感谢您的任何建议.盯着这个看了好几个小时.
Would really appreciate any advice. Been staring at this for hours.
推荐答案
您正尝试在Django中使用URL的反向解析.
You are trying to use the reverse resolution of urls in Django.
在您的html文件中,正确的表单操作网址应为以下内容,方法应为POST:
In your html file correct form action url to the following and method should be POST:
<form action={% url 'process' %} method="POST">
如果您尝试传递参数,请使用以下参数:
In case you are trying to pass parameters along then use this:
<form action={% url 'process' request.user.id 4 %} method="POST">
参考: https://docs.djangoproject.com/en/1.10/topics/http/网址/
这篇关于Django:HTML表单操作使用2个参数定向到视图(或URL?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!