简体   繁体   English

如何在没有AJAX的Django中提交“ POST”方法表单?

[英]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.

相关问题 如何在Django中使用一个提交按钮提交多个表单? - How do I submit multiple forms with a single submit button in django? 如何在没有方法发布和 forms 的情况下将数据保存在 django 中? - How to save data in django without method post and forms? 如何在Django中使用jQuery / Ajax进行POST? - How do I POST with jQuery/Ajax in Django? django:我如何在没有 django 表单的情况下在 django 中保存表单? - django: How do i save form in django without the django forms? 如何自定义Django表单的提交和验证方法? - How to customize the submit and verification method of Django forms? 如何使用 Ajax 在不重新加载页面的情况下提交表单 - How do I submit a form without page reload using Ajax 如何使用一个提交按钮在Django中提交多个脆皮表格? - How do i submit mutiple crispy forms in django using one submit button? django2-每个帖子都有一个喜欢的按钮。 Ajax提交类似按钮的表单,但是我有许多表单具有相同的ID - django2 - A like button per post. Ajax to submit the like button form but I have many forms with the same ID 如何在Django中通过Ajax发布数据? - How do i post data via ajax in django? 我如何处理不带表单的Django POST方法。 收到错误-CSRF验证失败。 请求中止 - How do i process the Django POST method without form. Getting error - CSRF verification failed. Request aborted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM