django 在视图和模板中执行布尔逻辑

wj8zmpe1  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(126)

我有两组挑战。首先,我有一个模型,其中的字段提交为布尔字段。我使用模型表单并将其呈现在模板中。有两个选项作为布尔选择,即“是”和“否”选项供用户选择是否保存表单。我希望如果此用户选择“是”,则应保存表单。否则,如果选择“否”,表单不应该保存。我已经用尽了所有我认为可以工作的逻辑,但不能实现这一点。以下是意见
views.py

def submit(request):
    if request.method =='POST':
        form = submitForm(request.POST)
        form.instance.user = request.user
        sub = form.save()
        if sub.sumit ==True:

        if sub:
            messages.info(request, 'You have submitted you form successfully')
            return redirect('home')
         
        messages.info(request, 'Continue editing your data')
        return render(request, 'portal/home/submit.html')
    form =submitForm()
    context = {
        'form':form,
        # 'sub':sub
    }
    return render(request, 'portal/home/submit.html', context)

我的第二个问题是这样的观点.既然我不能用上面的形式达到我想要的,我只是在此user_info中查询它,以便如果用户将提交保存为“是”或“否”,则模板中的某些按钮应该或不应该显示。下面是我的尝试方式,但不完全是我想要的。我只希望选择上述布尔形式为True或“是”(如模板上所示)的用户看到“保存”和“更新”按钮,但任何尚未提交或提交“否”的人都可以看到所有按钮。
检视

def user_info(request):

    # user = request.user
    user =request.user
    try:
        personal = Personalinfo.objects.get(user_id=user)
    except Personalinfo.DoesNotExist:
        messages.error(request, 'Please fill in all data')
        personal =None
    # try:
    sub = SubmitData.objects.filter(user_id = user)
    
    

    # except SubmitData.DoesNotExist:
        # messages.error(request, f'You already submitted you data on, {sub.date_submited}')
        # sub =None

    if request.method == "POST":
        form = usersForm(request.POST, request.FILES, instance =personal)
        if form.is_valid():
            form.instance.user = request.user
            form.save()          
            messages.success(request, 'User data saved successful! Click next to proceed')
            return redirect('user_info')
        
        else:
            messages.error(request, 'Error! Check your data')
            return redirect('user_info')
      
    form = usersForm(instance =personal)
    context={
        "form": form,
        'sub':sub,
        # 'submitted':submitted
        }
    return render(request, 'portal/home/user_info.html', context)

这是模板

{% if sub %}
                      
                      <div class="col-md-4 col-sm-3">
                        <div class="text-center">
                          
                            <a class="btn btn-dark text-decoration-none text-white bg-dark mt-4 mb-0" href="{% url 'academy' %}">Next</a>      
                          
                        </div>
                      </div>
                    </div>
                      {% else %}

                      <div class="row">
                        <div class="col-md-10 col-sm-10 mx-auto ">
                          <button type="submit" name="register" class="text-white btn bg-dark mt-4 mb-0">
                            Save
                          </button>
                          <a class="btn btn-dark text-decoration-none text-white btn bg-dark mt-4 mb-0" href="{% url 'user_update' request.user.pk %}"> Update </a>      
                          
                          <a class="btn btn-dark text-decoration-none text-white bg-dark mt-4 mb-0" href="{% url 'academy' %}">Next</a>    
                        </div>
                        
                      </div>

                    {% endif %}

你的回答会让我进步很多。谢谢

bgtovc5b

bgtovc5b1#

第一个问题:
根据您的问题,我假设该字段名为sumit(如果是打印错误,请道歉:-))
你在哪里

form.instance.user = request.user
    sub = form.save()
    if sub.sumit ==True:

    if sub:
        messages.info(request, 'You have submitted you form successfully')
        return redirect('home')

替换为

#set up sub as a 'falsey' value
    sub = ""
    #create an instance of form
    form = submitForm(request.POST)
    #check that the data is valid
    if form.is_valid():
        #check if the 'submit' checkbox has been checked
        #this is assuming you are using a default boolean checkbox widget, 
        #which only gets submitted if checked
        #with other widgets you might need to check for a particular value
        #eg, if form.cleaned_data.get("sumit") == "Yes"
        if form.cleaned_data.get("sumit"):
            form.instance.user = request.user
            sub = form.save()
        if sub:
            messages.info(request, 'You have submitted you form successfully')
            return redirect('home')

对于第二个问题--如果我没理解错的话,您应该能够通过对sub进行进一步的过滤来完成,这样,只有当用户表示“是”时,查询才会成功:

sub = SubmitData.objects.filter(user_id = user, sumit=True)

相关问题