Django:HTML 表单操作指向带有 2 个参数的视图(或 url?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42440962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django : HTML form action directing to view (or url?) with 2 arguments
提问by Lolerla
Started learning django about a week ago and ran into a wall. Would really appreciate any enlightenment...
大约一周前开始学习 django 并撞到了一堵墙。真的很感激任何启示......
models.py
模型.py
class data(models.Model):
course = models.CharField(max_length = 250)
def __str__(self):
return self.course
html
html
Converted the objects in models.course to schlist
将 models.course 中的对象转换为 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 (having my doubts if this works..)
urls.py(我怀疑这是否有效..)
urlpatterns = [
url(r'^(\d+)/(\d+)$',views.process, name = 'process'),
]
view.py
查看.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
想知道表单操作是否可以将操作定向到
(1) view.py or (2) url.py (and eventually to a view.py) with 2 arguments selected?
(1) view.py 或 (2) url.py(并最终到 view.py)选择了 2 个参数?
If so how should the form action be? {{view ?}} or {{url ?}}. Am I missing out the definition of my arguments in my HTML?
如果是这样,表单操作应该如何?{{view ?}} 或 {{url ?}}。我是否遗漏了 HTML 中参数的定义?
Directing to views.py:
指向 views.py:
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.
用户输入为CharField
,可用于get_object_or_404
获取模型 pk。但是,在定义我的 urls.py 时,我会收到一个 Noreverse 错误,因为我的 url 参数是主键。
Directing to urls.py:
指向 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
Url 参数是主键。从我看来,我需要在将用户输入Charfield
传递给 urls.py 之前神奇地将其转换为 pk
Is there a (or) function for get()
in django? E.g get_object_or_404(pk = q1 or course = q1)
?
get()
django 中是否有(或)函数?例如get_object_or_404(pk = q1 or course = q1)
?
Would really appreciate any advice. Been staring at this for hours.
真的很感激任何建议。盯着这个看了几个小时。
回答by Abijith Mg
You are trying to use the reverse resolution of urls in Django.
您正在尝试在 Django 中使用反向解析 url。
In your html file correct form action url to the following and method should be POST:
在您的 html 文件中,正确的表单操作 url 以下和方法应该是 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">
Reference: https://docs.djangoproject.com/en/1.10/topics/http/urls/
参考:https: //docs.djangoproject.com/en/1.10/topics/http/urls/
回答by Muhammad Faizan Fareed
Yes i'm late but it can help others for better understanding how Django processes the request.
是的,我迟到了,但它可以帮助其他人更好地理解 Django 如何处理请求。
Django 3.0 pattern
Django 3.0 模式
How Django processes the request
Django 如何处理请求
Basic :
基本的 :
- First Django check the matching URL.
- If URL is matched then calling the defined view to process the request. (Success)
- If URL not matched/found the Django invokes error Page Not Found
- 首先 Django 检查匹配的 URL。
- 如果 URL 匹配,则调用定义的视图来处理请求。(成功)
- 如果 URL 不匹配/未找到,Django 会调用错误页面未找到
In detail reading :
详细阅读:
Official Django Documentations How Django processes a request
These are your URL patterns :
这些是您的 URL 模式:
urlpatterns = [ path('profile/edit/<int:pk>/',views.editprofile, name='editprofile'),]
Third argument in urlpatterns is for if you want to change the url pattern from current to this :
urlpatterns 中的第三个参数用于将 url 模式从 current 更改为 this :
urlpatterns = [ url('profile/edit/user/id/<int:pk>',views.editprofile, name = 'editprofile'),]
You don't need to redefine url pattern in all Templates where you using url name.
您不需要在使用 url 名称的所有模板中重新定义 url 模式。
For Example :
例如 :
This is my template profile.htmlwhere i used the url name instead of hard coded url.
这是我的模板profile.html,其中我使用了 url 名称而不是硬编码的 url。
<a class="item" href="{% url 'editprofile' user.id %}" >Edit profile </a>
Solution of your problem :
您的问题的解决方案:
.html
.html
Only use url name instead of hard coded url in your templates and pass arguments.
仅在模板中使用 url 名称而不是硬编码的 url 并传递参数。
<form action={% process no_of_arguments %} method="POST">
views.py
视图.py
Here you can process your request
在这里您可以处理您的请求
def process(request,no_of_arguments):
Become good django developer
成为优秀的 Django 开发者
You can also use Django ModelForms for your model. Using model forms or simple form you can do multiple things
您还可以将 Django ModelForms 用于您的模型。使用模型表单或简单表单您可以做多件事
- Modular approach
- Write server side validation in related form instead of doing in views.py
- Readable code - Clean code
- 模块化方法
- 以相关形式编写服务器端验证,而不是在 views.py 中进行
- 可读代码 - 干净的代码