gpt4 book ai didi

django - django 中的多步表单和模型继承

转载 作者:行者123 更新时间:2023-12-04 11:51:37 27 4
gpt4 key购买 nike

我已经在许多 Web 应用程序中看到过这种方法(例如,当您订阅保险时),但是我找不到在 django 中实现它的好方法。我的模型中有几个类继承自基类,因此它们有几个共同的字段。在创建 View 中,我想使用该继承,因此首先询问公共(public)字段,然后询问特定字段,这取决于用户的选择。
天真的例子,假设我想填充一个地点数据库

class Place(Model):
name = models.CharField(max_length=40)
address = models.CharField(max_length=100)

class Restaurant(Place):
cuisine = models.CharField(max_length=40)
website = models.CharField(max_length=40)

class SportField(Place):
sport = models.CharField(max_length=40)
现在我想在有公共(public)字段(名称和地址)时创​​建 View ,然后可以选择地点类型(餐厅/运动场)。一旦选择了某种地点(或用户按下“继续”按钮),就会出现新的字段(我想是为了简化页面需要重新加载),而旧的字段仍然可见,并且已经填满。
我已经多次看到这种方法,所以我很惊讶没有标准方法,或者一些扩展已经对此有所帮助(我已经从 django-formtools 中查看了表单向导,但并没有真正与继承相关联),也做了更复杂的事情,因为在继承方面有更深的层次。

最佳答案

模型.py

class Place(models.Model):
name = models.CharField(max_length=40)
address = models.CharField(max_length=100)


class Restaurant(Place):
cuisine = models.CharField(max_length=40)
website = models.CharField(max_length=40)


class SportField(Place):
sport = models.CharField(max_length=40)
forms.py
from django.db import models
from django import forms

class CustomForm(forms.Form):
CHOICES = (('restaurant', 'Restaurant'), ('sport', 'Sport'),)
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Name'}))
address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Address'}))
type = forms.ChoiceField(
choices=CHOICES,
widget=forms.Select(attrs={'onChange':'renderForm();'}))
cuisine = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Cuisine'}))
website = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Website'}))
sport = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Sport'}))
View .py
from django.http.response import HttpResponse
from .models import Restaurant, SportField
from .forms import CustomForm
from django.shortcuts import render
from django.views import View


class CustomView(View):

def get(self, request,):
form = CustomForm()
return render(request, 'home.html', {'form':form})

def post(self, request,):
data = request.POST
name = data['name']
address = data['address']
type = data['type']
if(type == 'restaurant'):
website = data['website']
cuisine = data['cuisine']
Restaurant.objects.create(
name=name, address=address, website=website, cuisine=cuisine
)
else:
sport = data['sport']
SportField.objects.create(name=name, address=address, sport=sport)
return HttpResponse("Success")
模板/home.html
<html>

<head>
<script type="text/javascript">
function renderForm() {
var type =
document.getElementById("{{form.type.auto_id}}").value;
if (type == 'restaurant') {
document.getElementById("{{form.website.auto_id}}").style.display = 'block';
document.getElementById("{{form.cuisine.auto_id}}").style.display = 'block';
document.getElementById("{{form.sport.auto_id}}").style.display = 'none';
} else {
document.getElementById("{{form.website.auto_id}}").style.display = 'none';
document.getElementById("{{form.cuisine.auto_id}}").style.display = 'none';
document.getElementById("{{form.sport.auto_id}}").style.display = 'block';
}

}
</script>
</head>

<body onload="renderForm()">
<form method="post" action="/">
{% csrf_token %}
{{form.name}}<br>
{{form.address}}<br>
{{form.type}}<br>
{{form.website}}
{{form.cuisine}}
{{form.sport}}
<input type="submit">
</form>
</body>

</html>
在 settings.py 中添加模板文件夹
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
]

关于django - django 中的多步表单和模型继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67967805/

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