UpdateView Creates a new object

1,155 views
Skip to first unread message

Michael Ackerman

unread,
May 13, 2012, 5:59:50 AM5/13/12
to django...@googlegroups.com
My updateview seems to be creating another object instead of updating the object. The good news is my UpdateView form is filled in with initial data, but when I press submit, it creates a new ticket instead of updating the old one.

#views.py
class create_ticket(CreateView):
    model = ticket
    form_class = ticket_form
    template_name = "create_ticket.html"
    success_url = "/tickets/thanks/"

class update_ticket(UpdateView):
    model = ticket
    form_class = ticket_form
    template_name = "create_ticket.html"
    success_url = "/tickets/thanks/"

#models.py
class ticket(models.Model):
    title = models.CharField(max_length = 100)
    date_created = models.DateTimeField(auto_now_add=True)
    description = models.TextField()
    ranking = models.PositiveIntegerField(default=0)

    def __unicode__(self):
        return self.title
#urls.py
urlpatterns = patterns('',
    (r'^$',
        ticket_list.as_view()),
    (r'^(?P<pk>\d+)/$',
        ticket_detail.as_view()),
    (r'^create/$',
        create_ticket.as_view()),
    (r'^thanks/$',
        thanks_view.as_view()),
    (r'^(?P<pk>\d+)/update/$',
        update_ticket.as_view()),
)

My other 2 quick questions:
- Is the success_url and redirecting necessary?
- Is it possible to combine the create_ticket view and the update_ticket view?

All help is greatly appreciated.

Xavier Ordoquy

unread,
May 13, 2012, 8:40:00 AM5/13/12
to django...@googlegroups.com
Hi,

What does you form html tag looks like ? Sounds like you're sending the updateview form to the createview.

Regards,
Xavier Ordoquy,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Michael Ackerman

unread,
May 13, 2012, 3:19:14 PM5/13/12
to django...@googlegroups.com
Yes they are using the same template.
Its a very basic template:
{% extends "base.html" %}
{% block main %}
<form action="/tickets/create/" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
{% endblock %}

I figured since update and create were practically the same, I wanted to see if I could be a bit more DRY. Hence why I'm also looking for a way to see if I can combine the 2 views into one.

Xavier Ordoquy

unread,
May 13, 2012, 9:06:43 PM5/13/12
to django...@googlegroups.com
Hi,

You should just use action="" since it will post to the url the page was generated by.
This will allow you to use a single template for both create and view view while they keep their respective urls.
As a side note, you should use the url template tag rather than hardcode urls ;)

Regards,
Xavier Ordoquy,
Linovia.

Michael Ackerman

unread,
May 14, 2012, 11:42:45 AM5/14/12
to django...@googlegroups.com
Thank you very much, that worked. Also, do I have to redirect after POST (success_url)? for example posting a status on facebook or twitter?

Xavier Ordoquy

unread,
May 14, 2012, 11:57:27 AM5/14/12
to django...@googlegroups.com

No problem.
Redirecting after a POST is a good habit. This will avoid duplicated data if the user reloads the page because if you don't redirect, you'll get a second POST.

Regards,
Xavier Ordoquy,
Linovia.
Reply all
Reply to author
Forward
0 new messages