Django表单使用HTML模板

时间:2017-02-08 17:41:54

标签: python django django-forms django-templates

我是Django的新手,我一直在尝试将此表单发布到POST。我已经阅读了关于表单的文档,但我对此有点困惑,因为如果我创建一个forms.py文件,我不确定如何实现Jquery日期选择器或下拉菜单。

我已经创建了模板,我可以访问它,它可以按照我想要的格式进行格式化,但是我无法训练如何将其发送到POST。我们的想法是获取表单中的数据并将其插入Postgres表中。

模板submit.py

{% extends 'website/header.html' %}
{% block content %}
    <p><b>Submit Job</b></p>
    <form action="" method="post">
    {% csrf_token %}
        <b>Select Asset to transcode</b>
        <p>Material ID:
            <input type="text" name="material_id" size="30" value="" id="keyword"/>
        </p>
        <p>Profile:
            <select name="workflow">
                <option value="">Select a transcode Profile</option>
                {%  for i in object_list %}
                    <option value="{{ i.name }}">{{ i.name }}</option>
                {% endfor %}
            </select>
        </p>
        <script>
            $(function() {
                $( "#start_datepicker" ).datepicker({
                    dateFormat: 'dd-mm-yy'
                });
            });
        </script>
        <p>License Start Date: <input type="text" id="start_datepicker" name="start_date"></p>
        <script>
            $(function() {
                $( "#end_datepicker" ).datepicker({
                    dateFormat: 'dd-mm-yy'
                });
            });
        </script>
        <p>License End Date: <input type="text" id="end_datepicker" name="end_date"></p>
        <p>
            <input type="submit" name="submit" />
        </p>
    </form>
{% endblock %}

Views.py

from django.shortcuts import render

def submit(request):
    if request.method == 'POST':
        material_id = request.POST['material_id']
        workflow = request.POST['workflow']
        start_date = request.POST['start_date']
        end_date = request.POST['end_date']
        return render(request, 'submit/submit.html')

    else:
        return render(request, 'submit/submit.html')

以下是我遇到的错误:

Method Not Allowed (POST): /submit/
[08/Feb/2017 17:28:49] "POST /submit/ HTTP/1.1" 405 0

我做错了什么?

修改

以下是网址文件:

mysite url.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('website.urls')),
    url(r'^submit/', include('submit.urls')),
    url(r'^repo/', include('asset_db.urls')),
]

提交url.py

from django.conf.urls import url
from django.views.generic import ListView
from asset_db.models import Profiles

urlpatterns = [
    url(r'^$', ListView.as_view(queryset=Profiles.objects.all().order_by("id"), template_name='submit/submit.html')),
]

4 个答案:

答案 0 :(得分:2)

检查您的urls.py文件是否有类似

的内容
from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^submit/$', 'yourapp.views.submit'),
)

datepicker与此问题无关。很可能你发布到另一个视图。检查所有网址和查看名称。

答案 1 :(得分:2)

您正在使用ListView获取此网址。但ListView仅允许GET请求。这就是它用POST方法获取请求的原因。它给出错误方法不允许。 在views.py中为此网址创建一个视图,并定义get和post方法。

答案 2 :(得分:1)

您没有将视图映射到您的提交网址。您在/ submit下的唯一URL是ListView。

答案 3 :(得分:1)

如建议创建视图而不使用ListView修复了问题。

更新了提交的views.py

from django.shortcuts import render
from asset_db.models import Profiles


def submit(request):
    profiles = Profiles.objects.order_by('-id')
    context = {'profiles': profiles}
    return render(request, 'submit/submit.html', context)


def success(request):
    profiles = Profiles.objects.order_by('-id')
    context = {'profiles': profiles}
    return render(request, 'submit/success.html', context)

更新了模板submit.html

{% extends 'website/header.html' %}
{% block content %}
    <p><b>Submit Job</b></p>
    <form action="{% url 'success' %}" method="post">
    {% csrf_token %}
        <b>Select Asset to transcode</b>
        <p>Material ID:
            <input type="text" name="material_id" size="30" value="" id="keyword"/>
        </p>
        <p>Profile:
            <select name="workflow">
                <option value="">Select a transcode Profile</option>
                {%  for i in profiles %}
                    <option value="{{ i.name }}">{{ i.name }}</option>
                {% endfor %}
            </select>
        </p>
        <script>
            $(function() {
                $( "#start_datepicker" ).datepicker({
                    dateFormat: 'dd-mm-yy'
                });
            });
        </script>
        <p>License Start Date: <input type="text" id="start_datepicker" name="start_date"></p>
        <script>
            $(function() {
                $( "#end_datepicker" ).datepicker({
                    dateFormat: 'dd-mm-yy'
                });
            });
        </script>
        <p>License End Date: <input type="text" id="end_datepicker" name="end_date"></p>
        <p>
            <input type="submit" name="submit" />
        </p>
    </form>
{% endblock %}

网络服务器结果:

[09/Feb/2017 08:55:22] "GET /submit/ HTTP/1.1" 200 3300
[09/Feb/2017 08:55:29] "POST /submit/success/ HTTP/1.1" 200 2043

我使用Django Tutorial part 3来帮助创建上下文。谢谢你指点我正确的方向!