Reputation: 629
I'm new to Django and I have been trying to get this form to 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.
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. The idea is to take the data in the form and insert it into a Postgres table.
Template 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')
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:
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
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')),
]
Upvotes: 1
Views: 947
Reputation: 629
As suggested creating a view and not using ListView fixed the issue.
Updated submit 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
{% 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. Thanks for pointing me in the correct direction!
Upvotes: 1
Reputation: 2984
You are using ListView for this url. But ListView allowed only GET request. That's why when it get request with POST method. It is giving error METHOD NOT ALLOWED. Create a view for this url in your views.py and define get and post method.
Upvotes: 2
Reputation: 599926
You don't have a view mapped to your submit URL. Your only URL under /submit is a ListView.
Upvotes: 1
Reputation: 436
Check that your urls.py file has something like
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^submit/$', 'yourapp.views.submit'),
)
datepicker is not related to the issue. Most likely you post to another view. Check all urls and view names.
Upvotes: 2