[英]How do I submit “POST” method forms in Django without AJAX?
I'm completely new to Django and I'm also developing a very important project in this framework with some friends. 我对Django完全陌生,并且还在与一些朋友一起在此框架中开发一个非常重要的项目。 I'm having problems in submitting a "POST" method form in Django.
我在Django中提交“ POST”方法表单时遇到问题。
I'm having the "403 Forbidden" error
. 我遇到了
"403 Forbidden" error
。 It says that my CSRF token isn't configured correctly
. 它说我的
CSRF token isn't configured correctly
。 I'm pretty sure that I did setup it correctly, though. 我很确定自己确实设置正确。 My form is about updating an django user account in the database (MySQL).
我的表单是有关更新数据库(MySQL)中的django用户帐户的。 I also don't know if my program logic is right in the view.
我也不知道我的程序逻辑是否正确。 I didn't even had the opportunity to test it because of this dumb error.
由于这个愚蠢的错误,我什至没有机会进行测试。 The image and codes below exemplificate my problem.
下面的图像和代码说明了我的问题。
My form: 我的表格:
<form method="POST" action="/validacao/" name="user" class="current2"> {% csrf_token %}
<table>
<tr>
<td>Nome:</td><td>
<input type='text' name='first_name' maxlength='30' value='{{usuario.first_name}}' class="campo2" />
</td>
<td>Permissão: <font style="color: red;">
{% if usuario.is_staff %} Admin {% else %} Comum {% endif %}</font>
</td>
</tr>
<tr>
<td>Sobrenome:</td>
<td><input type='text' name='last_name' maxlength='30' value='{{usuario.last_name}}' class="campo2" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type='text' name='email' maxlength='75' value='{{usuario.email}}' class="campo2"/></td>
</tr>
<tr>
<td>Senha:</td><td> <input type='password' name='password' maxlength='120' class="campo2"/></td>
</tr>
<tr>
<td>Confirmar Senha:</td><td><input type='password' name='password2' maxlength='120' class="campo2"/></td>
</tr>
<tr><td></td><td><input type='submit' name='salvar' value='Salvar' class="botao2"/></td></tr>
</table>
</form>
My view: 我的观点:
def validacao_perfil(request):
if request.POST:
try:
request.user.first_name = request.POST['first_name']
request.user.last_name = request.POST['last_name']
request.user.email = request.POST['email']
request.user.password = request.POST['password']
request.user.save()
validacao=1
except:
validacao=0
variaveis_resposta={ 'usuario':request.user,
'MEDIA_URL':settings.MEDIA_URL,
'height_backgroud':'900',
'rodape':'position:relative; top: 148px;',
'ordem':0,
'validacao':validacao,
'context_instance':RequestContext(request),
}
return render_to_response("perfil_usuario.html", variaveis_resposta)
Obs.: the "urls.py" is set correctly and the bizarre thing is that I can see the csrftoken cookie var using Django Debug Toolbar. 观察:“ urls.py”设置正确,奇怪的是,我可以使用Django Debug Toolbar看到csrftoken cookie var。
只需将@csrf_exempt放在您的def validacao_perfil(request):
,看看这项工作是否可行,还可以尝试阅读django的文档。非常好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.