2015-12-18 92 views
0

我正在創建一個用戶查看錶單,患者可以在其中留下對其醫生的評論。我只希望登錄的用戶撰寫評論,並且每位醫生每個用戶只需評論1次。無論她是否登錄,用戶都會看到一個評論表單。如果沒有登錄,點擊提交按鈕後頁面會被重定向到登錄表單,並且在成功驗證後,它會被重定向回醫生檔案。但是當用戶重定向時,表單數據會丟失。Django 1.6:在頁面之間臨時保存表單數據

如何暫時保存表單數據,以便當用戶在認證成功後回滾並可以提交表單時,它保持在那裏?我已經在某個可以使用會話的地方讀過它,但是一切都如此令人難以置信,讓我難以理解。

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) 
+0

會議確實是你會怎麼做。但是如果用戶沒有登錄,我沒有看到任何代碼重定向。發生在哪裏? –

+0

在docprofile中。 {%如果不是user.is_authenticated%}。當用戶點擊提交按鈕時,它被重定向到登錄頁面。 –

回答

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) 
... 
+0

但這種方式用戶甚至沒有登錄就看不到醫生檔案,更不用說嘗試撰寫評論。我想在提交表單時提示登錄。 –

+0

好的,請在'request.method == POST'檢查中移動該邏輯。 –