我确定我做的事情显然是错的,但是我无法看到它。
我为Django应用程序制作了一个简单的表单,但它只返回了csrf令牌,而不是字段值。表格提交正常,但没有“事件标题”。 QueryDict中的键/值对。
准确地说,当我记录QueryDict时,它看起来像这样:
<QueryDict: {u'csrfmiddlewaretoken': [u'dpXmMHTE3WmQvdvrAUD4oFer2WfKEjWd']}>
create_event.html:
{% extends "basic-layout.html" %}
{% block maincontent %}
<h1>Create Event</h1>
{% if error_message %}<p>{{ error_message }}</p>{% endif %}
<form action="/create-event" method="post">{% csrf_token %}
<label for="event-title">Event title</label>
<input type="text" title="event-title" id="event-title" required/>
<input type="submit" value="create event"/>
</form>
{% endblock %}
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.decorators.csrf import csrf_exempt
import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.listEvents),
url(r'^create-event', csrf_exempt(views.createEvent))
]
views.py
def createEvent(request):
if request.method == 'GET':
template = loader.get_template('create_event.html')
context = RequestContext(request, {})
return HttpResponse(template.render(context))
if request.method == 'POST':
logger = logging.getLogger('degub')
logger.info(request.POST)
event_title = request.POST.get('event-title', '')
if event_title:
event = Event(event_title)
c = {}
c.update(csrf(request))
template = loader.get_template('list_events.html')
context = RequestContext(request, c)
return HttpResponse(template.render(context))
else:
template = loader.get_template('create_event.html')
template_values = {"error_message": "Nope, didn't work"}
context = RequestContext(request, template_values)
return HttpResponse(template.render(context))
答案 0 :(得分:2)
尝试在输入标记中添加name
属性。
<input type="text" name="event-title" title="event-title" id="event-title" required/>