如何使用django获取模板中显示的默认值?
我正在使用modelform扩展用户的配置文件。我希望以前保存的数据在用户决定重写之前显示在表单中。如何使用django在模板中执行此操作 模型 看法 样板如何使用django获取模板中显示的默认值?,django,templates,view,model,Django,Templates,View,Model,我正在使用modelform扩展用户的配置文件。我希望以前保存的数据在用户决定重写之前显示在表单中。如何使用django在模板中执行此操作 模型 看法 样板 当您实例化它时,将instance=user.get_profile()传递到UserProfileForm。当您实例化它时,将instance=user.get_profile()传递到UserProfileForm。我认为您只是将用户作为一个实例传递到表单中: @login_required ... else:
当您实例化它时,将
instance=user.get_profile()
传递到UserProfileForm
。当您实例化它时,将instance=user.get_profile()
传递到UserProfileForm
。我认为您只是将用户作为一个实例传递到表单中:
@login_required
...
else:
upform = UserProfileForm(instance=user)
...
然后,您可能需要手动生成模板:
所以
应输出已“保存”的报价。我认为您只需将用户作为实例传递到表单中:
@login_required
...
else:
upform = UserProfileForm(instance=user)
...
然后,您可能需要手动生成模板:
所以
应该输出已经“保存”的引号。在实例化表单的两个位置传递
instance=profile
。为了简化这一点,您可以创建一个dict并使用**
关键字扩展运算符:
@login_required
def user_profile(request):
user = User.objects.get(pk=request.user.id)
form_kwargs = {}
try:
form_kwargs['instance'] = user.get_profile()
except Profile.DoesNotExist(): # change Profile to whatever your profile model is named
pass
if request.method == 'POST':
upform = UserProfileForm(request.POST, **form_kwargs)
if upform.is_valid():
up = upform.save(commit=False)
up.user = request.user
up.save()
return HttpResponseRedirect('/accounts/profile')
else:
upform = UserProfileForm(**form_kwargs)
return render_to_response('reserve/templates/edit_profile.html', locals(), context_instance=RequestContext(request))
在实例化表单的两个位置传递
instance=profile
。为了简化这一点,您可以创建一个dict并使用**
关键字扩展运算符:
@login_required
def user_profile(request):
user = User.objects.get(pk=request.user.id)
form_kwargs = {}
try:
form_kwargs['instance'] = user.get_profile()
except Profile.DoesNotExist(): # change Profile to whatever your profile model is named
pass
if request.method == 'POST':
upform = UserProfileForm(request.POST, **form_kwargs)
if upform.is_valid():
up = upform.save(commit=False)
up.user = request.user
up.save()
return HttpResponseRedirect('/accounts/profile')
else:
upform = UserProfileForm(**form_kwargs)
return render_to_response('reserve/templates/edit_profile.html', locals(), context_instance=RequestContext(request))
如果传递一个UserProfile实例,则表单需要一个UserProfile实例,而不是用户实例 您有两个(或更多)选项可以将UserProfile实例传递到表单 选项1获取或创建 您可以使用get_或_create。它获取配置文件,如果它不存在,则创建并保存一个。 然后将userprofile传递给UserProfileForm的两个调用
upform = UserProfileForm(request.POST, instance=userprofile)
upform = UserProfileForm(instance=userprofile)
选项2获取配置文件()
如果要使用user.get_profile(),需要检查几件事。您需要确保您的用户配置文件设置正确,如下所示:
主要是在您设置的settings.py中:
AUTH_PROFILE_MODULE = "account.UserProfile"
这假设UserProfile是在名为“account”的应用程序中定义的,如果它是在MyApp中定义的:
AUTH_PROFILE_MODULE = "MyApp.UserProfile"
然后您可以使用(如前所述):
虽然您需要处理用户尚未创建配置文件的实例,但此处的get_profile()将失败,UserProfile.objects.get也将失败。如果您传递一个UserProfile实例,则表单需要一个UserProfile实例,而不是用户实例 您有两个(或更多)选项可以将UserProfile实例传递到表单 选项1获取或创建 您可以使用get_或_create。它获取配置文件,如果它不存在,则创建并保存一个。 然后将userprofile传递给UserProfileForm的两个调用
upform = UserProfileForm(request.POST, instance=userprofile)
upform = UserProfileForm(instance=userprofile)
选项2获取配置文件()
如果要使用user.get_profile(),需要检查几件事。您需要确保您的用户配置文件设置正确,如下所示:
主要是在您设置的settings.py中:
AUTH_PROFILE_MODULE = "account.UserProfile"
这假设UserProfile是在名为“account”的应用程序中定义的,如果它是在MyApp中定义的:
AUTH_PROFILE_MODULE = "MyApp.UserProfile"
然后您可以使用(如前所述):
虽然您需要处理用户尚未创建概要文件的实例,但此处的get_profile()将失败,UserProfile.objects.get也将失败。我将其更改为“upform=UserProfileForm(request.POST,instance=user.get_profile()”。我需要更改模板中的任何内容吗?您应该向我们展示您的模板,以了解是否需要更改changed@armonge我现在所拥有的模板中与表单相关的是上面列出的内容。我不知道如何编辑它以包含默认数据(用户已经输入的数据)。我给您的代码使用当前保存的数据作为字段的初始值来实例化表单。您只需将表单转储到模板中即可。您现在使用的模板代码只是为
网站
字段打印输入
元素,它不会输出整个表单。您可以简单地使用{{form.as{p}}
将整个内容放在页面上,或者使用{%for form%}
单独呈现每个字段。@ChrisPratt I将模板中当前的内容替换为简单的“{upform.as{p}”textbox/textarea中的默认值文本仍然没有呈现。我将其更改为“upform=UserProfileForm(request.POST,instance=user.get_profile())”。我需要更改模板中的任何内容吗?您应该向我们展示您的模板,以了解是否需要更改changed@armonge我现在所拥有的模板中与表单相关的是上面列出的内容。我不知道如何编辑它以包含默认数据(用户已经输入的数据)。我给您的代码使用当前保存的数据作为字段的初始值来实例化表单。您只需将表单转储到模板中即可。您现在使用的模板代码只是为网站
字段打印输入
元素,它不会输出整个表单。您可以简单地使用{{form.as{p}}
将整个内容放在页面上,或者使用{%for form%}
单独呈现每个字段。@ChrisPratt I将模板中当前的内容替换为简单的“{upform.as{p}”,文本框/文本区域中的默认值文本仍然没有呈现。
upform = UserProfileForm(request.POST, instance=request.user.get_profile())
upform = UserProfileForm(instance= instance=request.user.get_profile())