【问题标题】:Why isn't my page redirecting after I submit the form or refresh the page in Django?为什么我在 Django 中提交表单或刷新页面后页面没有重定向?
【发布时间】:2021-12-03 17:43:10
【问题描述】:

我正在开发一个 Django 应用程序,但我没有得到想要的结果。提交表单或刷新整个页面后,create_job 页面未呈现。

这是 create_job_page 视图

def create_job_page(request):
    current_customer = request.user.customer
    
    if not current_customer.stripe_payment_method_id:
        return redirect(reverse('customer:payment_method'))
    # return render(request, 'customer/create_job.html')
    # Filtering
    create_job = Job.objects.filter(customer=current_customer, status=Job.CREATING_STATUS).last()
    
    step1_form = forms.JobCreateStep1Form(instance=create_job)
    
    if request.method == 'POST':
        if request.POST.get('step') == '1': #If it's form one
            step1_form = forms.JobCreateStep1Form(request.POST, request.FILES)
            if step1_form.is_valid():
                creating_job = step1_form.save(commit=False)
                # Adding current customer to the form
                creating_job.customer = current_customer
                creating_job.save()
                return redirect(reverse('customer:create_job'))
            
    
    return render(request, 'customer/create_job.html', {
        "step1_form": step1_form
    })

这是 HTML 代码


<b>Create a Job</b>
            <div class="tab-content" id="pills-Content">
                <div class="tab-pane fade" id="pills-info" role="tabpanel" aria-labelledby="pills-info-tab">
                    <h1>Item Info</h1>
                    <form method="POST" enctype="multipart/form-data">
                        <b class="text-secondary">Item Information</b></br>
                        <div class="card bg-white mt-2 mb-5">
                            <div class="card-body">
                                {% csrf_token %}
                                {% bootstrap_form step1_form %}
                            </div>
                        </div>
                        <input type="hidden" name="step" value="1">
                        <button class="btn btn-primary" type="submit">Save &    Continue</button>
                    </form>
                </div>

【问题讨论】:

  • 当你按下 f12 开发者工具时你会遇到什么错误?
  • @Ahmedaminshahin 客户端没有错误,整个问题是我提交表单后页面没有反向重定向 - return render(request, 'customer/create_job.html', { " step1_form": step1_form })
  • @Ahmedaminshahin 我想通了,问题出在 urls.py
  • 好兄弟 :) 继续努力,你是 python 开发者吗?
  • @Ahmedaminshahin 是的,我是。

标签: django django-rest-framework django-views django-forms


【解决方案1】:

在 django 中我从不使用reverse,我通常只写redirect('app:url_name')

【讨论】:

  • 如何在视图中使用它?
  • 返回重定向('app:url_name')
猜你喜欢
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 2023-03-06
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
相关资源
最近更新 更多