On Tue, Mar 10, 2009 at 16:06, Daniel Roseman
<[email protected]> wrote:
> On Mar 10, 12:54 pm, Dmitry Teslenko <[email protected]> wrote:
>> Hello!
>>
>> I'm starting with django framework; I use django 1.0 and sqlite
>> database backend.
>> I've stumbled upon this thing: when editing model entry with ModelForm
>> I get insert instead of update.
>>
>> Here's view procedure:
>> ===========================================================================
>> ==
>> def edit(request, task_id):
>> if request.method == 'POST':
>> if task_id == -1:
>> form = NewTaskForm(data=request.POST, instance=None)
>> if form.is_valid():
>> form.save()
>> else:
>> try:
>> task = Task.objects.get(pk=task_id)
>> form = NewTaskForm(data=request.POST,
>> instance=task)
>> if form.is_valid():
>> form.save()
>> except Task.DoesNotExist:
>> raise Http404
>> return HttpResponseRedirect(get_test_app_url())
>> else:
>> if task_id == -1:
>> form = NewTaskForm()
>> else:
>> try:
>> task = Task.objects.get(pk=task_id)
>> form = NewTaskForm(instance=task)
>> except Task.DoesNotExist:
>> raise Http404
>> return render_to_response('test_app/edit.html', { 'form' :
>> form })
>> ===========================================================================
>> ==
>>
>> test_app/edit.html looks like this:
>> ===========================================================================
>> ==
>> <form action="/py/test_app/edit/" method="POST">
>> {{ form.as_p }}
>> <input type="submit" value="Save" />
>> </form>
>> ===========================================================================
>> ==
>>
>> I guess I miss some obvious thing. Please help to spot it.
>
>
> How is the view getting the task_id parameter? Your form is always
> posting to edit/, with no parameter, so there's no way it can pass the
> task_id to the view. Presumably your URLconf has a default to -1, and
> there's no way to get anything else, which is why the form always
> inserts.
> --
> DR.
View gets proper task_id; I've added it to the edit.html template and
get assured
it being set right;
Additons go from this link:
=============================================================================
<a href="/py/test_app/edit">New task</a>
=============================================================================
And editions go from this link:
=============================================================================
<a href="/py/test_app/edit/{{ task.pk }}">Edit</a>
=============================================================================
Here's excerpt from urls.py:
=============================================================================
(r'^%stest_app/edit/$' % URL_PREFIX,
'test_django.test_app.views.edit' , { 'task_id' : -1 }),
(r'^%stest_app/edit/(?P<task_id>\d+)/$' % URL_PREFIX,
'test_django.test_app.views.edit'),
=============================================================================
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---