Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Class Based Generic View - "No URL to redirect to"

I'm using the generic CreateView like:

#urls.py

from django.conf.urls.defaults import *
from django.views.generic import CreateView
from content.models import myModel

urlpatterns = patterns('myApp.views',
    (r'myCreate/$', CreateView.as_view(model=myModel)),
)

With a mymodel_form.html template like:

<form method="post" action="">
{% csrf_token %}
  {{ form.as_p }}
  <input type="submit" value="Submit" />
</form>

When I submit my form, the new object is created but I get the error

ImproperlyConfigured at ...

No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.

How can I specify the url to redirect on success ?

like image 337
Pierre de LESPINAY Avatar asked Jun 07 '11 14:06

Pierre de LESPINAY


3 Answers

Have you tried passing in success_url? e.g.

CreateView.as_view(model=myModel, success_url="/success/")

or if you want to redirect to a named view:

CreateView.as_view(model=myModel, success_url=reverse('success-url'))
like image 107
NickAldwin Avatar answered Nov 03 '22 23:11

NickAldwin


you can also try to define get_absolute_url in your models. For example

class Something(models.Model):
    name = models.CharField(max_length=50, verbose_name='name')

    class Meta:
        pass

    def get_absolute_url(self):
        return u'/some_url/%d' % self.id 
like image 43
zzart Avatar answered Nov 04 '22 00:11

zzart


Add in views:

def form_valid(self, form):

   return self.render_to_response(self.get_context_data(form=form))
like image 36
user2567460 Avatar answered Nov 03 '22 22:11

user2567460