[Answer]-Django – Link on submit

1👍

You can use HttpResponseRedirect() in the view like this:

from django.shortcuts import render_to_response
from ezmapping.models import *
from django.forms.models import modelformset_factory

def setName(request):
    ezAppFormSet = modelformset_factory(ezApp, extra=1, fields=('name'))
    formset = ezAppFormSet(queryset=ezApp.objects.none())
    if request.method == 'POST':
        formset = ezAppFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            next_link = u"/next/"
            return HttpResponseRedirect(next_link)

    return render_to_response("project/manage_new.html", {'formset': formset, 'title': "New"}, context_instance=RequestContext(request))

0👍

Sure. Just return a render_to_response inside of your conditional for request.method == 'POST', or optionally, just set up the context and template as variables to hand into render_to_response as such:

Paraphrased:

def foo(request):
    template = 'template1.html'
    # form and formsets here set into `context` as a dictionary

    if request.method == 'POST':
        template = 'template2.html'

    return render_to_response(template, context)

[EDIT]

When I re-read your question, if you want to redirect to a different view if your form is valid, then return an HttpResponseRedirect instead.

0👍

If I understand your question correctly.

Your view:

from django.shortcuts import render_to_response
from ezmapping.models import *
from django.forms.models import modelformset_factory

def setName(request):
    ezAppFormSet = modelformset_factory(ezApp, extra=1, fields=('name'))
    formset = ezAppFormSet(queryset=ezApp.objects.none())
    if request.method == 'POST':
        formset = ezAppFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            submit_link = True # 2*
        else:
            submit_link = False
    return render_to_response("project/manage_new.html", {'submit_link': submit_link, 'formset': formset, 'title': "New"}, context_instance=RequestContext(request))

Your template:

{% extends "basemap.html" %}
 {% block content %}
 <table border="1">
   <tr>
   <td>
    <h1>Define new App options</h1>
    {% if formset.errors %}
        <p style="color: red;">
            Please correct the error{{ formset.errors|pluralize }} below.
        </p>
    {% endif %}
    <form method="post" action="." encrypt="multipart/form-data">{% csrf_token %}
            {{ formset.as_p }}
        <input type="submit" value="Submit">
    </form>

    {% if submit_link %} 
      <a href='/next/'>Data is saved, let's continue.</a>
    {% endif %}
</td>
</tr>
</table>
{% endblock %}

Update.

And yes, if you want to redirect (not to show the next link) just place:

from django.shortcuts import redirect
...
return redirect( 'you-url' )

Instead of 2*.

Leave a comment