我有以下基于类的TemplateView,我希望我的TemplateView像UpdateView.But我不太确定在哪里设置实例参数的表单。 我在get_context_data中尝试但是没有用。有任何想法吗 ?
class ProfileUpdateView(generic.TemplateView):
template_name = "profile/update_profile.html"
def dispatch(self, *args, **kwargs):
if self.request.user.is_authenticated():
return super(ProfileUpdateView, self).dispatch(*args, **kwargs)
else:
return HttpResponseRedirect('/profiles/login')
def get_context_data(self, *args, **kwargs):
context = {}
try:
Profile.objects.get(user__id=self.request.user.id)
user = Profile.objects.get(user__id=self.request.user.id)
context['profile_form'] = profile_form(data=request.POST, instance=user)
except:
context['profile_form'] = ProfileForm(data=self.request.POST)
context['profile'] = Profile.objects.filter(user=self.request.user.id)
context['experiences'] = Experience.objects.filter(user=self.request.user.id)
context['education'] = Education.objects.filter(user=self.request.user.id)
context['award'] = Award.objects.filter(user=self.request.user.id)
context['publication'] = Publication.objects.filter(user=self.request.user.id)
context['membership'] = Membership.objects.filter(user=self.request.user.id)
context['patent'] = Patent.objects.filter(user=self.request.user.id)
context['certificate'] = Certificate.objects.filter(user=self.request.user.id)
context['language'] = Language.objects.filter(user=self.request.user.id)
return context
def post(self, request, *args, **kwargs):
if self.request.POST:
profile_form = ProfileForm(data=request.POST)
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.user = request.user
profile.save()
return HttpResponseRedirect('/')
答案 0 :(得分:1)
尝试使用CBV(CreateView),然后使用initial
和def get_initial(self):
请参见link,我认为这可以为您提供帮助。 9-11和9-12。