填充下拉Django模型表单

2024-09-28 01:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

表单.py

class BetaUser(ModelForm):
    Industry = forms.ModelChoiceField(queryset = homepagelaunch_industries.objects.all(), initial=1)
        class Meta:
            model = BetaUsers
            fields = ['Industry']

模型.py

^{pr2}$

视图.py

def BetaUserDisplayForm(request):
    BetaUserGatherForm = BetaUser(request.POST or None)
    if request.method == 'POST':
        if BetaUserGatherForm.is_valid():
            BetaUserIndustry = BetaUserGatherForm.cleaned_data['Industry']
            BetaUserGatherForm.save()
            try:
                return HttpResponseRedirect('/BetaUserThankYou/')
            except:
                raise ('Invalid request')
        else:
            BetaUserGatherForm = BetaUser()

    return render(request, 'apage.html', {
        'BetaUserGatherForm': BetaUserGatherForm
    })

我只是试图从模型类的数据库列IndustryChoice填充Industry的值:Industries。 我做错什么了?有人能帮忙吗?在

Im收到错误:

name 'homepagelaunch_industries' is not defined

谢谢!在


Tags: py模型表单returnifisrequestpost

热门问题