1

Whenever I submit my form, the data is added to the database but I am getting an error

ImproperlyConfigured at /issue/new/ No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model. 

Even though I already have a get_absolute_url in my model

EDIT: The above is when the form is valid, when the form is invalid, I get

TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()' 

model:

class Issue(models.Model): CATEGORIES = ( ('A', 'Asd'), ('B', 'Bd'), ('C', 'Cu'), ('D', 'Cr'), ) title = models.CharField(max_length=140) description = models.TextField() author = models.ForeignKey(User) category = models.CharField(max_length=1, choices=CATEGORIES) date_created = models.DateTimeField(auto_now_add=True) def get_absolute_url(self): return reverse('issue_detail', kwargs={'pk': self.pk}) def __unicode__(self): return self.title 

form:

class IssueForm(forms.ModelForm): class Meta: model = Issue fields = ('title','description', 'category',) 

view

class IssueCreateView(CreateView): form_class = IssueForm def form_valid(self,form): obj = form.save(commit=False) obj.author = self.request.user obj.save() return HttpResponseRedirect(self.get_success_url()) 

url

url(r'^new/', IssueCreateView.as_view(), name='issue_create'), 

template

<form action="/issue/new/" method="post"> {% csrf_token %} {{ issueform.as_p }} </form> 
7
  • Here: IssueCreateView.as_view(), try passing the success_url - like this: IssueCreateView.as_view(success_url=reverse('url-name-here'))) Commented Jun 17, 2014 at 19:28
  • @karthikr I tried, doesn't solve it. Commented Jun 17, 2014 at 19:41
  • Ok. I assume you changed url-name-here with the success URL name Commented Jun 17, 2014 at 19:41
  • @karthikr yes I did. Btw I have edited the question to add another error on form invalid Commented Jun 17, 2014 at 19:47
  • You need to define the template to be used via the template_name class-level constant, just like the error tells you to. Like in the examples in the documentation Commented Jun 17, 2014 at 20:00

2 Answers 2

3

sk1p is correct about your first issue, though I would use super() and write it in a bit more concise way:

class IssueCreateView(CreateView): form_class = IssueForm def form_valid(self, form): form.instance.author = self.request.user return super(IssueCreateView, self).form_valid(form) 

Using super will usually eliminate all issues from overriding a method, allowing you to customise behaviour without the need to reimplement the logic of the overridden method.

About your second issue:

Usually, a form is posted to it's own url, or at least a basic page is supplied if there are errors. You can add a basic template that shows the form with all it's errors if there are any on the first submission.

If you want to redirect back to the original form page, you'll lose the POST data on the redirect. There is however an easy workaround to this: you can save the POST data in the users session, and if this data is available on the page containing the form, use that to bind the form:

class IssueCreateView(CreateView): form_class = IssueForm def form_valid(self, form): form.instance.author = self.request.user return super(IssueCreateView, self).form_valid(form) def form_invalid(self, form): self.request.session['ISSUE_CREATE_FORM_DATA'] = self.request.body return HttpResponseRedicect(reverse('form-url')) 

And in your form view:

from django.http import QueryDict def form_view(request, *args, **kwargs): form_data = QueryDict(request.session.pop('ISSUE_CREATE_FORM_DATA', '')) form = IssueForm(form_data) ... 

This requires the session middleware to be installed ('django.contrib.sessions.middleware.SessionMiddleware').

Sign up to request clarification or add additional context in comments.

Comments

2

ModelFormMixin.form_valid, which you are overriding, sets self.object to the newly created object, which is then used by get_success_url. Try self.object = obj in form_valid and get_success_url should work again.

1 Comment

Thanks, this solves it. Btw, I added another error that Django throws when I submit a form that is not valid (missing required fields). I have edited that to the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.