Hi All,

In my application, I have a form with a choice field which populated
from the model using ModelChoiceField.  On the view I have a link
which takes the user to a view which lets them create a new model
object.  What I'd like to have happen after the successful creation is
redirect back to the original form with that new Model selected in the
ModelChoiceField.  Here's some sample code to give you an idea:

## URLs #############
from django.conf.urls.defaults import *

urlpatterns = patterns(''
    , (r'^create_context/$',
'django_apps.projApp.views.create_context')
    , (r'^process/$', 'django_apps.projApp.views.process_task')
    ,
)

## TEMPLATE (this is process.html) ###########
                <form method="post" action=".">
                                {{form.context.label_tag}} {{form.context}}
                                        <a href={% url 
django_apps.projApp.views.create_context %}>
                                                <img width="22" height="22" 
src="{% get_static_url %}/images/
list-add.png"
                                                                alt="Add 
Context"/>
                                        </a>
                                {{form.notes.label_tag}} {{form.notes}}
                                {{form.due_date.label_tag}} {{form.due_date}}
                        <input type="submit" />
                </form>

## VIEW (first the view for the template, above) ###########
def process_task(request):
    if request.method == 'POST':
        form = ProcessForm(request.POST)
        if form.is_valid():
            # Do form processing here...
            # todo: clean_data is legacy, will need to be changed to
cleaned_data in next release.
            data = form.clean_data

            t = Task.objects.create( context=data['context']
                                    , due_date=data['due_date'] )
            return
HttpResponseRedirect( request.META['HTTP_REFERER'] )
    else:
        form = ProcessForm()
    return render_to_response('process.html', {'form': form})

## VIEW (for the create_context)  ###########
def create_context( request ):
    if request.method == 'POST':
        form = CreateContextForm(request.POST)
        if form.is_valid():
            # Do form processing here...
            # todo: clean_data is legacy, will need to be changed to
cleaned_data in next release.
            data = form.clean_data

            c = Context.objects.create(  name=data['name'] )
            # return with defaults selected
            processForm = ProcessForm(initial={'context':c.id })
            return render_to_response('process.html', {'form':
processForm})
    else:
        form = CreateContextForm()
        return render_to_response('create_projctx.html', {'form':
form})

This works from the standpoint of getting back to the "process.html"
template with the new Context object selected on the original form.
However, since this is using render_to_response instead of a redirect,
the URL on the user's browser still holds the "create_context"
location.  So, when the user hits "submit" on the process.html page,
the form gets sent back to the "create_context" view and things don't
operate as expected (another context gets created rather than
submitting to the "process_task" view)

The ideal way to do this would be to HttpResponseRedirect in place of
the "render_to_response" but this doesn't allow me to pass the form
object or any kind of dictionary representing the initial values.  Is
there a way to do this?  Or am I going about this in the wrong manner?

Thanks in advance for any words of wisdom!!
Lemme know if I can provide any additional code snips to help
highlight what I'm trying to do.
Dennis







--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to