为什么我的表格无效?

时间:2018-06-24 21:34:36

标签: django-views

def rate_me(request, pk):
    tech = get_object_or_404(Technician, pk=pk)
    print(tech)
    if request.method == "POST":
        form = RatingForm(request.POST)
        rate = int(request.POST['selected_rating'])
        rate = rate
        rated = get_object_or_404(Rating_Option, pk=rate)
        if form.is_valid():
            tech = form.save(commit=False)
            tech.technicians = Technician.objects.filter(id = pk)[0]
            rated = form.save(commit=False)
            rated.rating = Rating_Option.objects.filter(id= 5)[0]
            form.save()
            return HttpResponseRedirect('thank_you')
        else:
            print(rate)
            message = "Form not valid"
            return redirect('home')
    else:
        print(request.POST)
        form = RatingForm()
        tech_name = Technician.objects.values_list('first_name', flat=True).filter(id = pk)[0]
        ratings = Rating_Option.objects.all().order_by('options')
        return render(request, 'techs/rate_me.html', {'form': form, 'ratings':ratings, 'tech_name':tech_name})

0 个答案:

没有答案