gpt4 book ai didi

python - 多表单处理 Django python 3.x

转载 作者:行者123 更新时间:2023-12-05 07:03:00 25 4
gpt4 key购买 nike

我想用两个输入表单制作简单的页面第一个用于将数据发布到文件中第二个用于获取索引并从文件中删除数据他们都独立工作但到目前为止,我只能像第二种形式一样使用第一种形式,它甚至没有达到第二种形式的单一方法都没有完成代码如下:

###############
#Views.py

class HomeView(TemplateView):
template_name = 'index/num.html'

def get(self, request):
form = AddForm()
form2 = DeleteForm()
result = printResult()
return render(request, self.template_name, {'form': form, 'form2': form2,'result': result})

def post(self, request):

result = "result"

####____add option###################
if request.method == 'POST':
form = AddForm(request.POST)
form2 = DeleteForm(request.POST)
if form.is_valid():
text = form.cleaned_data['post']
if (re.match("[0-9]+", str(text))):
text = text
else:
text= None

result = addToFile(text)
args = {'form': form, 'form2': form2, 'text': text, 'result': result }
return render(request, self.template_name, args)
##########
###____Delete option#######
if form2.is_valid():

index = form2.cleaned_data['dell']
result = DeleteIndexFromFile(index)
args2 = {'form': form, 'form2': form2, 'text': index, 'result': result }

return render(request, self.template_name, args2)

##########
#forms.py
from django import forms

class AddForm(forms.Form):
post = forms.CharField(required = False)
class DeleteForm(forms.Form):
delete = forms.IntegerField(required = False)

#num.html
<div class="container">
<p>
<form method="post">
{% csrf_token %}
{{ form }}

<button type="submit">Submit</button>
</form>

<form method="delete" >
{% csrf_token %}
{{ form2 }}

<button type="submit">Delete</button>
</form>
</p>
<p>
<h4>Data: {{ text }}</h4>
<h4>{{ result }}</h4>
</p>
</div>

最佳答案

有一个叫做 django-shapeshifter 的包可以用来做这个:

https://github.com/kennethlove/django-shapeshifter

为了全面披露,我是该项目的贡献者。

关于python - 多表单处理 Django python 3.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63392689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com