简体   繁体   English

使用HTML模板的Django表单

[英]Django form using HTML template

I'm new to Django and I have been trying to get this form to POST. 我是Django的新手,我一直在尝试将此表单发送到POST。 I have read the documentation on forms but i'm a bit confused by it as I am not sure how to implement the Jquery date picker or drop down menu if i create a forms.py file. 我已经阅读了关于表单的文档,但是我对此感到有些困惑,因为如果我创建一个forms.py文件,我不确定如何实现Jquery日期选择器或下拉菜单。

I have created the template and i can access it and it formats exactly how i want, however I can't workout how to get it to POST. 我已经创建了模板,我可以访问它,并且它的格式完全符合我的要求,但是我无法锻炼如何将其发送到POST。 The idea is to take the data in the form and insert it into a Postgres table. 想法是采用表格中的数据并将其插入Postgres表中。

Template submit.py 模板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 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')

Here is the error I am getting: 这是我得到的错误:

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

What am I doing incorrectly? 我做错了什么?

EDIT 编辑

Here are the URL files: 以下是URL文件:

mysite url.py 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')),
]

submit url.py 提交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')),
]

Check that your urls.py file has something like 检查您的urls.py文件是否具有类似内容

from django.conf.urls import patterns, url

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

datepicker is not related to the issue. datepicker与该问题无关。 Most likely you post to another view. 您最有可能发布到另一个视图。 Check all urls and view names. 检查所有网址和视图名称。

You are using ListView for this url. 您正在为此列表使用ListView。 But ListView allowed only GET request. 但是ListView只允许GET请求。 That's why when it get request with POST method. 这就是为什么当它使用POST方法获取请求时的原因。 It is giving error METHOD NOT ALLOWED. 它给出了错误的方法不允许。 Create a view for this url in your views.py and define get and post method. 在您的views.py中为此URL创建一个视图,并定义get和post方法。

You don't have a view mapped to your submit URL. 您没有映射到提交URL的视图。 Your only URL under /submit is a ListView. / submit下唯一的URL是ListView。

As suggested creating a view and not using ListView fixed the issue. 如建议的那样,创建视图而不使用ListView解决了该问题。

Updated submit views.py 更新了提交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)

Updated template submit.html 更新了模板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 %}

Webserver results: 网络服务器结果:

[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

I used Django Tutorial part 3 for help with creating the context. 我使用Django教程第3部分获得有关创建上下文的帮助。 Thanks for pointing me in the correct direction! 感谢您指出正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM