Hi Jorge,

Thank you so much for the response!  This works perfectly.  Thanks for
cluing me in to request.GET.get() functionality.  I do have a slight
follow up here though.  Once this is implemented you end up with the
redirect URL looking something like this:

http://mygreatwebsite.com/django_app/process/?newcontext=22

Which seems okay, but I also have a couple of other fields I'd like to
default, so when we add in more fields, the URL can become unwieldy:

http://mygreatwebsite.com/django_app/process/?newcontext=22&newname=new%20string

Which doesn't seem to be in line with django principles of readable
URL's.  Thinking about this a little longer I thought it might be a
good idea to take a urls.py approach to the problem and create a new
entry such as:

    , (r'^process/defaultApplied/(?P<field>\w+)/(?P<id>\d+)/$',
'django_apps.projApp.views.process_task')

Then you could handle both types of defaults, e.g.:
http://mygreatwebsite.com/django_app/process/defaultApplied/context_id/22
http://mygreatwebsite.com/django_app/process/defaultApplied/name/new%20string

Which seems a lot better, but there's no way to pass *both* context_id
AND name at the same time in this approach.  Is there a better way I'm
not seeing?  Or am I just really making my life unnecessarily
difficult here and the GET approach is the best way to handle this?

Thanks again Jorge!!  This newsgroup is really fantastic!,
--Dennis

On Jan 22, 12:37 pm, "Jorge Gajon" <[EMAIL PROTECTED]> wrote:
> On Jan 20, 2008 11:58 PM, Dennis <[EMAIL PROTECTED]> wrote:
>
>
>
> > 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?
>
> Hi Dennis,
>
> You are right that the ideal way would be to return an
> HttpResponseRedirect, and that's what I would do too.
>
> You can pass the id of the created context object as a parameter in
> the request back to the 'process_task' view.
>
> For example, your create_context view would look like this:
>
> 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'] )
>            # Pass the context id in the 'newcontext' parameter
>            return HttpResponseRedirect('/process/?newcontext=%s' % c.id)
>    else:
>        form = CreateContextForm()
>        return render_to_response('create_projctx.html', {'form':form})
>
> Then your process_task view would be changed to check if there is a
> 'newcontext' parameter available in your request, in which case you
> use it to set the initial data of the form.
>
> 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(initial={
>               'context': request.GET.get('newcontext', None)
>        })
>    return render_to_response('process.html', {'form': form})
>
> The important part here is this:
>        form = ProcessForm(initial={
>               'context': request.GET.get('newcontext', None)
>        })
>
> You are setting an initial context depending on the value of the
> 'newcontext' request parameter, but because that parameter may not
> always be present you need to use the .get() method, the first
> parameter is the key to get from the dictionary and the second
> parameter is a value to return if the key is not found.
>
> In this case, when 'newcontext' is not found you'll get a None value
> back. After you create a new context on the other view and get
> redirected to process_task, there should be a value in the
> 'newcontext' request parameter, and the form will render with that
> context selected.
>
> Also note that this line:
> return HttpResponseRedirect( request.META['HTTP_REFERER'] )
> will cause you problems because after creating a context, the referrer
> will be the /create_context/ page.
>
> Hope this helps.
>
> Regards,
>
> - Jorge
--~--~---------~--~----~------------~-------~--~----~
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