【问题标题】:Django Invalid FormDjango 无效表单
【发布时间】:2021-12-28 07:07:34
【问题描述】:

我有一个模态框,里面有一个表单。问题是在无效表单上它确实返回表单错误,但它也通过再次呈现主页来关闭模式对话框。它也是首页,所以登录需要返回渲染。

如果帖子失败,我将如何返回屏幕。

def index(request):
    context = {}
    if request.method == "POST":
        print(request.POST)
        form = UserProfileForm(request.POST or None, request.FILES or None,instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('home') 
    else:
        form = UserProfileForm()
    context['form']= form
    return render(request, "home.html", context)

模态

{% block content %}
 
    <div class="modal fade" id="editProfile" tabindex="-1" role="dialog" aria-labelledby="editProfilelCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editProfileLongTtitle">Edit Profile</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    {% include 'form.html' %}
                </div>
            </div>
        </div>
    </div>

{% endblock %} 

form.html

{% block content %}

    <form method = "POST" action='.' enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Upload</button>
    </form>

{% endblock %}

home.html

{% extends 'base.html' %}

{% block title %}Home{% endblock %}

{% block content %}
  {% if user.is_authenticated %}
  <div class="container"> 
    {% include 'editProfileModal.html' %} 
    <div class='row'>  
      {% include 'sidebar.html' %}
    </div> 
  </div> 
  {% else %}
    <p>You are not logged in</p>
    <a href="{% url 'login' %}">Log In</a>
    <a href="{% url 'signup' %}">Sign up</a>
    
  {% endif %}

{% endblock %}

【问题讨论】:

  • 你的意思是当form.is_valid() 返回 False 时?
  • 是的,当我在表单上输入错误时。
  • 可以加form.html代码
  • 肯定用表单编辑过
  • home.html 模板中是否有引导模式?

标签: django


【解决方案1】:

您需要在发送 POST 请求时显示模式并且视图不发送重定向。您可以尝试下一个糟糕的解决方案。它很丑但可以工作:


def index(request):
    context = {'is_post': False}
    if request.method == "POST":
        context['is_post'] = True
        form = UserProfileForm(request.POST or None, request.FILES or None,instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('home') 
    else:
        form = UserProfileForm()
    context['form']= form
    return render(request, "home.html", context)

{% block content %}
 
    <div class="modal fade" id="editProfile" tabindex="-1" role="dialog" ... >
        <!-- your content ... -->
    </div>
    <!-- Do this after jQuery loaded -->
    {% if is_post %}
    <script type="text/javascript">
        $(window).on('load', function() {
            $('#myModal').modal('show');
        });
    </script>
    {% endif %}
{% endblock %}

【讨论】:

    【解决方案2】:

    您可以简单地返回 HttpResponse 并显示表单错误。

    from django.http import HttpResponse
    
    if form.is_valid():
       ...
    else:
       return HttpResponse(form.errors.as_json())
    

    【讨论】:

    • 发回错误但页面现在只是错误。应该留在有表单错误的页面。
    • 页面返回什么错误?
    • 你好 @ArundeepChohan 在这种情况下你可以使用 ajax 和 django 形式这些帖子将帮助你 post1,post2,post3
    • 它只是根据不正确的部分发送表单错误。
    • 不确定这部分如何使用 ajax。
    【解决方案3】:

    试试这个:

    def index(request):
    context = {}
    if request.method == "POST":
        print(request.POST)
        form = UserProfileForm(request.POST or None, request.FILES or None,instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('home') 
    context['form']= form
    return render(request, "home.html", context)
    

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 2020-04-21
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2017-04-27
      • 2011-12-14
      • 2021-07-10
      相关资源
      最近更新 更多