Django 1.6:在页面之间临时保存表单数据

时间:2015-12-18 10:25:58

标签: python django forms

我正在创建一个用户评论表单,患者可以在其中对医生进行评估。我只希望登录用户撰写评论,每位医生每位用户只需1次评论。无论用户是否已登录,都会向用户显示评论表单。如果未登录,则在单击“提交”按钮后,页面将重定向到登录表单,并且在成功验证后,它将重定向回医生档案。但是重定向用户时表单数据会丢失。

如何暂时保存表单数据,以便在用户成功进行身份验证后返回并且可以提交表单时保留表单数据?我已经在某个地方读到了你可以使用会话但是一切都是如此令人难以理解,让我无法理解。

docprofile.html

{% if reviewed == False %}
<form action="" method="post" id="user_uploader" > {% csrf_token %}
<div class="revbox">
<textarea type="text" class="form-control" id="comment" placeholder="How was your experience?" name="comment" rows="6"></textarea>

<div class="subbox">
<div class="like-dis-check">
<p id="wouldrec"> Would you recommmend this doctor to someone else?</p>
<input type="radio" id="radio1" name="Like" value="Like">
<label id="r1" for="radio1">Yes</label>
<input type="radio" id="radio2" name="Like" value="Dislike">
<label id="r2" for="radio2">No</label>
</div>

<div class="drop1">
<input type="hidden" name="user" value="{{ user.id }}" />
<input type="hidden" name="doctor" value="{{ doctor.id }}" />
</div>

{% if not user.is_authenticated %}

<div class="subbutton">
<a class="btn btn-primary" href="{% url 'login' %}?next={{ request.path }}" type="" name="submit" id="ss-submit">Submit</a>
</div>

{% else %}

<div class="subbutton">
<button class="btn btn-primary" type="submit" name="submit"id="ss-submit">Submit</button>
</div>

{% endif %} 


<div class="errorbox">
{% for field in form.visible_fields %}
{{ field.errors }}
{% endfor %}
</div>
</div>
</div>
</form>
{% elif user.is_authenticated and reviewed == True  %}
<h4><a class="writebtn">Already Reviewed!</a></h4>
{% else %}
<a class="btn btn-primary writebtn" onclick = " ga('send','event', 'button' , 'Write a Review', 'Dr. {{doctor.name}}', 7);" href="{% url 'login' %}?next={{ request.path }}">Write a Review</a>  
{% endif %}

view.py

def showDocProfile(request, slug):

    doctor = get_object_or_404(Doctor, slug=slug)

    d = getVariables(request,dictionary={'page_name': "Dr. " + doctor.name +"  "+ doctor.specialization.name +"})

    if request.user.is_authenticated():
        user = request.user

    if request.method == "POST":
        form = UserContentForm(request.POST)

        if form.is_valid():
            comment = form.cleaned_data['comment']


            if request.POST.get('Like') == 'Like':
                con = UserContent(comment = comment, liked = True, disliked = False, doctor_id = doctor.id, user_id = request.user.id)
                doctor.likes += 1
                doctor.netlikes = doctor.likes - doctor.dislikes
                doctor.save()
                con.save()

            elif request.POST.get('Like') == 'Dislike':
                con = UserContent(comment = comment, liked = False, disliked = True,  doctor_id = doctor.id, user_id = request.user.id)
                doctor.dislikes +=1
                doctor.netlikes = doctor.likes - doctor.dislikes
                doctor.save()
                con.save()

            url = '/docprofile/%s' % str(doctor.id)
            return HttpResponseRedirect(url)
    else:
        form = UserContentForm()

    UGC = UserContent.objects.filter(doctor_id=doctor.id).order_by('-submitted_on') # Reviews on Meddy


    d.update({'doctor': doctor, 'UGC': UGC,'form': form,  'doctors': Doctor.objects.all().order_by('-rating')})
    return render(request, 'm1/docprofile.html', d)

1 个答案:

答案 0 :(得分:0)

在模板中进行重定向是错误的地方。如果你这样做,就没有机会将它保存在会话中。相反,您应该始终提交到showDocProfile视图,并在必要时进行重定向:

if request.user.is_authenticated():
    user = request.user
else:
    request.session['doctor_data'] = request.POST
    return redirect("{}?{}".format(reverse('login'), request.path))
if request.method == "POST":
...
else:
    data = request.session.pop('doctor_data', None)
    form = UserContentForm(data)
...