Django 验证并返回预览 URL?

发布于 2024-08-01 19:42:11 字数 947 浏览 5 评论 0原文

我再次询问:),我不知道如何做到这一点。

我的英语不太好,但我会尝试问这个:

如何验证表单并返回预览网址(相同的视图表单)并显示验证错误?我问这个是因为我有 2 个表单,第一个表单的操作将转到第二个表单(POST),但是在第二个表单(视图?)中,我需要验证第一个表单,如果第一个表单有效,我想显示第二个表单,否则将显示第一个表单,其中有错误。

我不知道我是否清楚,抱歉。

我正在考虑这个:

def secondForm(request):
    if request.method =='POST':
       form = FirstForm(request.POST)
       if form.is_valid():
          fields = request.POST.copy()
          # showing the second form?? with x differents fields, i have 2.
          if fields['xvalue']=='1': # this is from radio buttons
             form2 = xSecondForm()
          elif fields['xvalue']=='2':
             form2 = ySecondForm()
       else:
         # here go back with erros msgs????
         #return render_to_response('firstFormTemplate.html',{'form': form}, context_instance=RequestContext(request))
    else:
      #return something or go back ???

谢谢大家 PD:我很高兴,我正在编码 Django + python :D

im asking again :), i don't know how make this.

My English is not too good, but ill try to asking this:

how ill validate a form and go back to the preview url (the same view form) and show the validation errors?, im asking this because i have 2 form's, the first form's action is going to a second form (POST), but in this second form (view?) i need to validate the first form, if the first form is valid i want to show the second form else ill show the first form with errors.

i dont know if im clear , im sorry.

im thinking about this:

def secondForm(request):
    if request.method =='POST':
       form = FirstForm(request.POST)
       if form.is_valid():
          fields = request.POST.copy()
          # showing the second form?? with x differents fields, i have 2.
          if fields['xvalue']=='1': # this is from radio buttons
             form2 = xSecondForm()
          elif fields['xvalue']=='2':
             form2 = ySecondForm()
       else:
         # here go back with erros msgs????
         #return render_to_response('firstFormTemplate.html',{'form': form}, context_instance=RequestContext(request))
    else:
      #return something or go back ???

Thanks guys
PD: im happy, im coding Django + python :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以往的大感动 2024-08-08 19:42:11

是的,formwizard 可能是您的答案,但您可能可以在视图中使用一些逻辑来做到这一点。

就像是:

def your_view(request):
    context = {}
    data = request.method == 'POST' and request.POST or None
    form = FirstForm(data=data)
    # If the first form is valid, build the second.
    if form.is_valid():
        if form.cleaned_data['xvalue'] == 1:
            form2 = xSecondForm(data=data)
        else:
            form2 = ySecondForm(data=data)
        context['form2'] = form2
    # If both forms are valid, everything is done.
    if form.is_valid() and form2.is_valid():
        # TODO: put save/processing logic here
        # Now redirect.
        return http.HttpResponseRedirect(...)
    # If we get here, either there hasn't been a post yet, the second form hasn't
    # been entered, or there was an error in either form.
    context['form'] = form
    return render_to_response(...)

Yes, formwizard might be your answer, but you could probably do it with some logic in a view.

Something like:

def your_view(request):
    context = {}
    data = request.method == 'POST' and request.POST or None
    form = FirstForm(data=data)
    # If the first form is valid, build the second.
    if form.is_valid():
        if form.cleaned_data['xvalue'] == 1:
            form2 = xSecondForm(data=data)
        else:
            form2 = ySecondForm(data=data)
        context['form2'] = form2
    # If both forms are valid, everything is done.
    if form.is_valid() and form2.is_valid():
        # TODO: put save/processing logic here
        # Now redirect.
        return http.HttpResponseRedirect(...)
    # If we get here, either there hasn't been a post yet, the second form hasn't
    # been entered, or there was an error in either form.
    context['form'] = form
    return render_to_response(...)
握住你手 2024-08-08 19:42:11

好吧,伙计们,我认为我的解决方案是 FormWizard :)

但我不清楚我的第二个表单,因为如果无线电中选定的选项用于更多或更少的字段(在第二种形式)

;(

Ok, guys, i think that mi solution is FormWizard :)

but im not clear with something about my Second Form, because i need to show the second form in different way if the selected option in radio is for more or less fields (in the second form)

;(

与君绝 2024-08-08 19:42:11

我认为您想使用 process_step() 来对第二种形式中如何显示/显示什么进行一些逻辑。

I think you want to use process_step() to do some logic on how/what do display in your second form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文