redmonkey wrote:
> Hi,
>
> I have a small form on a page that is mostly optional fields with the
> exception of some drop down boxes. I want to take the data submitted
> through this form and use them to form a URL.
>
> My problem is with the URL writing. I first wrote some unit tests to
> find the regular expressions that worked but Django doesn't seem to
> like the '?' in URL configurations.
>
> How can I write a workflow that converts optional, POSTed data to a
> RESTful URL for processing?
>
> Here's an example:
>
> class IceCreamForm(forms.form):
> description = forms.CharField(required=False)
> flavour = forms.ChoiceField(choices=FLAVOUR_CHOICES, initial=0)
> FLAVOUR_CHOICES = (
> ...
> )
>
> This is my form class. It contains one optional field, and one
> required field. When rendered on a page, the form POSTs it's data to
> the the 'process' view:
>
> def process(request):
> # Standard form stuff (is_valid) ...
> params = {}
> if form.cleaned_data['description']:
> params.update(description=form.cleaned_data['description'])
> if form.cleaned_data['flavour']:
> params.update(flavour=form.cleaned_data['flavour'])
>
> return HttpResponseRedirect('proj.app.views.filter', kwags=params)
>
> def filter(request, description=None, flavour=0):
> ... do stuff ...
>
> Here, I collect the parameters from the form and try to turn them into
> a pretty URL that redirects to the filter view to do something with
> the data. It's the URL writing I'm having problems with. I want to
> write things like:
>
> urlpatterns += patterns('proj.app.views',
> (r'^(?P<flavour>\w+)/((?P<description>\w+)/)?$', 'filter'),
> )
>
It might be better to try something that makes the last slash optional
in a different way. How about
(r'^(?P<flavour>\w+)(/(?P<description>\w+))/?$', 'filter'),
> The question mark is wrapped around an extra '/' to complete the URL,
> but it doesn't work. Nor does
>
> urlpatterns += patterns('proj.app.views',
> (r'^(?P<flavour>\w+)/$', 'filter'),
> (r'^(?P<flavour>\w+)/(?P<description>\w+/$', 'filter'),
> )
>
> I'm surprised with the result of the last one, and so I'm sure I'm
> missing something. Does anyone have any ideas or simplifications for
> me?
>
The last one doesn't even appear to be a syntactically correct re due to
mismatching parentheses ... did you copy and paste, or mis-transcribe?
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---