0

After a user fills out a form on my site, I user render(request, html, context). I'd like to return the user to the same part of the page after they register. I am not using any front end frameworks (like angular - thats my next project). How would I go about doing this?

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 = 'xx@gmail.com'
        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)

and a made up html to show what I mean:

<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>

In the above I want to return the user to the div class="second".

Thanks you.

Robby
  • 183
  • 2
  • 2
  • 13

1 Answers1

0

To do this, you need to differentiate the default GET request to access the page and the POST of the form.

E.g. You could do:

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

and pass form_submit in the context.

Then, in HTML:

{% if form_submit %}
   <div class="second"></div>
{% else %}
   <div class="first"></div>
{% endif %}
zubhav
  • 1,519
  • 1
  • 13
  • 19
  • where do I put that html? You reversed my html, is this something that I put at the bottom? – Robby Oct 03 '16 at 23:51
  • Its just a template that you need to follow.. when the form submit variable is true, you will show second div.. if not then you will show first div. You shouldnt put it at the bottom, instead you should replace the content .. please refer to Django context variables – zubhav Oct 04 '16 at 16:08