开发者

Django: POSTing directly into a Django-powered form?

I've got a Django forms page, set up in the classic Django way to show a form for a GET request and to handle form submission for a POST request, and return errors for invalid data.

def create_trial(request):
  trial_form = TrialForm()
  if request.method == 'POST':
    if request.POST['form'] == 'trial_create':
      trial_form = TrialForm(request.POST)
      if trial_form.is_valid():
         # etc etc
 return render_to_response(template, {'trial_form': trial_form},
                            context_instance=RequestContext(request))

Now, I need to create a static HTML page, located on a different subdomain, that POSTs directly into this Django form.

Obviously, if all the data is okay, then that's fine - the HTML page will just redirect to the Django success message.

However, what will happen if the Django form rejects some of the data? What will my user see?

What would be ideal is if they were redirected to the Django form (in the Django subdomain) with the data pre-filled and the rele开发者_开发问答vant error messages showing, but I don't know if that's actually possible.

Please could anyone advise?


You say the post is from a different site? Say I have site1 which contains the view and site2 which has the html form. I don't think its possible to go to site2 from within site1's view except when you call a redirect, in which case you lose access to all sorts of validation response like form errors etc., unless you find a way to pass them all via query strings. If you do, you could then create a handler in site2's side that will receive and parse responses from site1. I'm not sure if this is applicable in your case though, since you said its just static html.


Answering my own question: No problem - on testing, it all Just Works in Django.

A failed response redirects to the form on the Django subdomain, with the errors pre-filled. I heart Django!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜