Sunday, May 13, 2012

Re: UpdateView Creates a new object

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.

Le 13 mai 2012 à 21:19, Michael Ackerman a écrit :

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.

On Sun, May 13, 2012 at 5:40 AM, Xavier Ordoquy <xordoquy@linovia.com> wrote:
Hi,

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

Regards,
Xavier Ordoquy,
Linovia.

Le 13 mai 2012 à 11:59, Michael Ackerman a écrit :

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.

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


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


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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate