您好我想为当前登录的用户创建一个新任务,因此我正在使用此视图 这是我的观点
@login_required
def ajouter_task(request):
if request.method == 'POST':
form = TaskForm(request.POST)
if form.is_valid():
task = form.save(commit=False)
task.user = request.user
task.save()
return redirect('home')
else:
form = TaskForm()
return render_to_response('ajouter_task.html', {'form': form})
它不起作用我不知道问题出在哪里,请提前帮助并感谢你
答案 0 :(得分:0)
这是我已经包含的模板{%csrf_token%}
<form class="login100-form validate-form" method="POST" action="{% url 'ajouter_task' %}">{% csrf_token %}
<span class="login100-form-title">
ajouter votre Taches :
</span>
<div class="form-group clearfix">
<label for="{{ form.titre.id_for_label }}" class="col-md-4 control-label text-right">Titre<span class="text-red"></span>:</label>
<div class="col-md-6"><input type="text" class="form-control"/>
{{ form.titre }}
</div>
</div>
<div class="form-group clearfix">
<label for="{{ form.date.id_for_label }}" class="col-md-4 control-label text-right">Date:</label>
<div class="col-md-6"><input type="date" class="form-control"/>
{{ form.date }}
</div>
</div>
<div class="form-group clearfix">
<label for="{{ form.objectif.id_for_label }}" class="col-md-4 control-label text-right">Objectif:</label>
<div class="col-md-6"><input type="text" class="form-control"/>
{{ form.objectif }}
</div>
</div>
<div class="form-group clearfix">
<label for="{{ form.theme.id_for_label }}" class="col-md-4 control-label text-right">Theme:</label>
<div class="col-md-6"><input type="text" class="form-control"/>
{{ form.theme }}
</div>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn" href="{% url 'home' %}">
ajouter
</button>
</div>
</form>
我的urls.py是
url(r'^ajouter_task/$', views.ajouter_task, name='ajouter_task'),