1

I am using forms which get populated by the user. The save method of the forms.py looks like this:

def save(self, commit=True):
    instance = super(LocationForm, self).save(commit=False)
    if instance.location_id:
       instance.save()
    else:
        new_location=Location.objects.create(name=self.cleaned_data['name'])
        instance.location=new_company
        instance.save()
    return instance 

So when I click on update the data gets saved in the database, however I get an error

No URL to redirect to error

views:

class LandingView(CreateView):
    model = Review
    form_class = LocationForm
    template_name="core/index.html"

models.py - I created a get_absolute_url function

from django.core.urlresolvers import reverse

  def get_absolute_url(self):
     return reverse ('index', args=[str(self.id)])

So in the urls I tried this

url(r'^$', core.views.LandingView.as_view(success_url="success"), name='index'),

However, what do I do if I want it to be redirected to its original page, like 'go back to where I came from' ?

I tried

url(r'^$', core.views.LandingView.as_view(success_url=""), name='index'),

and

url(r'^$', core.views.LandingView.as_view(success_url=reverse('index')), name='index'),

and

url(r'^$', core.views.LandingView.as_view(success_url=reverse("")), name='index'),

But None of these work!

EDIT This url works and I don't need def_get_absolute_url

url(r'^$', core.views.LandingView.as_view(success_url="/"), name='index'),
Tom
  • 2,545
  • 5
  • 31
  • 71

1 Answers1

1

I am sure there is a way to redirect in a Class Based View like yours but here is how I would do it, simply once the form is saved then it redirects. Does your save method work if so I like the way you did that but I'm not sure if editing your save method is the best way to go, instead of getting the instance in your view, to pre populate the form with the data that's already submitted say in an initial creation form. Any other questions let me know.

views.py

 from app.forms import LocationForm

      def Landing_View(request)
        if request.method == 'POST':
          form = LocationForm(request.POST)
          if form.is_valid():
             form.save()
             return HttpResponseRedirect('/your_url/')
          else:
             print form.errors()
        else:
          form = LocationForm()
         return render (request, 'template.html', {'form':form},)

urls.py - The URL for the Landing_View, then I redirect you back to this URL on save

  url(r'^your_url/', app.views.Landing_View, name='your_url'),
  • Hi thanks, ok I understand the question with the views now. My entire form is handeled in the forms.py with the def save method above. Thanks for your help. However I found out how to redirect with the success_url, it's simply: `url(r'^$', core.views.LandingView.as_view(success_url='/'), name='index'),` – Tom Aug 19 '15 at 15:28
  • oh interesting never knew that either so we both learned something. –  Aug 19 '15 at 15:29