【发布时间】:2018-07-06 23:27:20
【问题描述】:
当用户在我的网站上注册时,他们会选择一个他们希望在用户个人资料中看到的模板。在这种情况下,因为他们想查看“绿色”配置文件,所以 user.userprofile.color 等于 1。
我已经为绿色模板构建了一个特定的模型(称为 model1),并为该模型构建了一个特定的表单(称为 form1)。当用户在 UserProfile 表单中选择“绿色”并提交表单时,会自动为用户分配一个 model1 的实例
在 views.py 中,我想创建一个通用视图,它采用 user.userprofile.color 值并使用它来建立
a) 以什么形式提供网页
b) 表单基于什么模型
而不是像这样硬编码表单和模型值:
def 主页模板(请求):
if request.method == 'POST':
form = form1(request.POST, request.FILES,
instance=request.user.model1)
if form.is_valid():
form.save()
return redirect('/accounts/')
else:
form = form1(instance=request.user.model1)
args = {'form': form}
return render(request, 'homepage.html', args)
在函数中而不是指定'form1'和'model1',我希望表单和模型值等于'form'+i和'model'+i,其中i等于user.userprofile.color的值(即 1)。
对此的任何帮助将不胜感激!
【问题讨论】:
-
在模板 {% if user.userProfile.1 %} 使用模板 {% elif user.userProfile.2 %} 使用模板 {% endi f%}
标签: django django-models django-forms django-views