Django:HTML 表单操作指向带有 2 个参数的视图(或 url?) [英] Django : HTML form action directing to view (or url?) with 2 arguments

查看:24
本文介绍了Django:HTML 表单操作指向带有 2 个参数的视图(或 url?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约一周前开始学习 django 并撞到了一堵墙.真的很感激任何启示...

models.py

类数据(models.Model):course = models.CharField(max_length = 250)def __str__(self):返回自我课程

html

将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;">第一道菜:</h4><select data-placeholder="课程" style="width:350px;"class="chosen-select" tabindex="7"><option value=""></option>{% 用于 schlist %}<选项>{{ item }} </option>{% 结束为 %}</选择>

</br><div><h4 style="font-family:verdana;">第二课:</h4><select data-placeholder="课程" style="width:350px;"class="chosen-select" tabindex="7"><option value=""></option>{% 用于 schlist %}<选项>{{ item }} </option>{% 结束为 %}</选择>

</br><input type="submit" value="比较!"/></表单>

urls.py(我怀疑这是否有效..)

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)…………

想知道表单操作是否可以将操作定向到

(1) view.py 或 (2) url.py(最终到 view.py)并选择了 2 个参数?

如果是这样,表单动作应该如何?{{view ?}} 或 {{url ?}}.我是否遗漏了 HTML 中参数的定义?

指向 views.py:

用户输入是CharField,可以使用get_object_or_404来获取模型pk.但是,在定义我的 urls.py 时,我会收到一个 Noreverse 错误,因为我的 url 参数是主键.

指向 urls.py:

Url 参数是主键.从我看来,我需要神奇地将我的用户输入 Charfield 转换为 pk,然后再将其传递给 urls.py

django 中是否有用于 get() 的(或)函数?例如 get_object_or_404(pk = q1 or course = q1)?

非常感谢任何建议.盯着这个看了几个小时.

解决方案

您正试图在 Django 中使用反向解析网址.

在您的 html 文件中,正确的表单操作 URL 和方法应为 POST:

如果您尝试传递参数,请使用以下命令:

参考:https://docs.djangoproject.com/en/1.10/topics/http/网址/

Started learning django about a week ago and ran into a wall. Would really appreciate any enlightenment...

models.py

class data(models.Model):
    course = models.CharField(max_length = 250)

    def __str__(self):
        return self.course

html

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 (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

(1) view.py or (2) url.py (and eventually to a view.py) with 2 arguments selected?

If so how should the form action be? {{view ?}} or {{url ?}}. Am I missing out the definition of my arguments in my HTML?

Directing to 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.

Directing to 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

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.

解决方案

You are trying to use the reverse resolution of urls in Django.

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">

Reference: https://docs.djangoproject.com/en/1.10/topics/http/urls/

这篇关于Django:HTML 表单操作指向带有 2 个参数的视图(或 url?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆