2016-10-03 180 views
0

用戶在我的網站上填寫表單後,我使用render(request,html,context)。我想在註冊後將用戶返回到頁面的同一部分。我沒有使用任何前端框架(如角度 - 這是我的下一個項目)。我會如何去做這件事?django 1.10渲染模板html類

views.py:

def homepage(request): 
    countries = Country.objects.count() 
    cities = City.objects.count() 

    start_trip_date = date(xxxx, x, x) 
    today = date.today() 
    total_days = today - start_trip_date 

    queryset_list = Post.objects.active()[:6] 
    query = request.GET.get("q") 
    if query: 
     queryset_list = queryset_list.filter(
      Q(title__icontains=query) | 
      Q(content__icontains=query) | 
      Q(user__first_name__icontains=query) | 
      Q(user__last_name__icontains=query) 
     ).distinct() 

    contact_form = EmailUpdatesForm(request.POST or None) 
    if contact_form.is_valid(): 
     contact = contact_form.save(commit=False) 
     contact.email = request.POST['email'] 
     contact.first_name = request.POST['first_name'] 
     contact.save() 
     profile_data = { 
      'email': contact.email, 
      'first_name': contact.first_name, 
     } 

     plaintext = get_template('email/frontpage_registered_email/email_text.txt') 
     htmly = get_template('email/frontpage_registered_email/email_template.html') 
     text_content = plaintext.render(profile_data) 
     html_content = htmly.render(profile_data) 

     subject = "{0}, thank you for registering with xx!".format(contact.first_name) 
     from_email = '[email protected]' 
     to_email = contact.email 

     msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email]) 
     msg.attach_alternative(html_content, "text/html") 
     msg.send() 

     return render(request, "homepage/homepage.html", {}) 
    else: 
     print contact_form.errors, 

    context = { 
     'object_list': queryset_list, 
     'countries': countries, 
     'cities': cities, 
     'days_traveling': total_days.days, 
     'contact_form': contact_form, 
    } 

    return render(request, "homepage/homepage.html", context) 

和由HTML來說明我的意思:

<body> 
<div class="first">content</div> 
<div class="second"> 
    <form id="contact_form" method="POST" action="." enctype="multipart/form-data" novalidate> 
     <fieldset> 
      {% csrf_toke %} 
      {{ contact_form|crispy }} 
      <input class="btn..." type="submit" name="submit" value="Register" /> 
     </fieldset> 
    </form> 
</div> 
</body> 

在上面我想給用戶返回到div類= 「第二」 。

謝謝你。

回答

0

爲此,您需要區分默認的GET請求以訪問頁面和表單的POST。

E.g.你可以這樣做:

contact_form = EmailUpdatesForm() 

if request.method == 'POST': 
    contact_form = EmailUpdatesForm(request.POST) 
    if contact_form.is_valid(): 
     contact = contact_form.save(commit=False) 
     contact.email = request.POST['email'] 
     .... 
     .... 
     form_submit = True 

並在上下文中傳遞form_submit。

然後,在HTML:

{% if form_submit %} 
    <div class="second"></div> 
{% else %} 
    <div class="first"></div> 
{% endif %} 
+0

在哪裏我把HTML?你扭轉了我的HTML,這是我放在底部的東西? – Robby

+0

它只是一個模板,你需要遵循..當表單提交變量爲true時,你將顯示第二個div ..如果不是,那麼你會顯示第一個div。你不應該把它放在底部,而應該替換內容..請參考Django上下文變量 – zubhav