【发布时间】:2016-10-31 09:27:38
【问题描述】:
所以我开始将表单模型切换到类基础视图。在EpisodeInfoView 中,当我在pod_funnel.py 的get 方法中在if podcast 下评论或取消评论production = Production.objects.create(podcast=podcast) 时,它会显示表单并让我填写字段但是当我在管理员下签入Productions ,它显示正在使用 Podcast 名称创建作品(从其 ForeignKey 中提取)but without the rest of the info (episode number, episode guest, episode title, etc...) 如果我删除并添加它以在帖子中创建对象,它会向我显示表单,但它什么也不做它显示任何错误。
pod_funnel.py 查看:
class EpisodeInfoView(LoginRequiredMixin, View):
form_class = EpisodeInfoForm
template_name = 'pod_funnel/forms_episode_info.html'
def get(self, request, *args, **kwargs):
initial_values = {}
user = request.user
# Lets get client and podcast for the user already. if not existent raise 404
client, podcast = get_podfunnel_client_and_podcast_for_user(user)
if client is None or podcast is None:
raise Http404
if podcast:
initial_values['podcast_name'] = podcast.name
initial_values['podcast_id'] = podcast.id
production = Production.objects.filter(podcast=podcast).first()
# production = Production.objects.create(podcast=podcast)
if production:
initial_values['episode_number'] = production.episode_number
initial_values['episode_title'] = production.episode_title
initial_values['episode_guest_first_name'] = production.episode_guest_first_name
initial_values['episode_guest_last_name'] = production.episode_guest_last_name
initial_values['episode_guest_twitter_name'] = production.episode_guest_twitter_name
initial_values['episode_summary'] = production.episode_summary
form = self.form_class(initial=initial_values)
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
# lets get the data
# podcast_id = form.cleaned_data.get('podcast_id')
production_id = form.cleaned_data.get('production_id')
episode_number = form.cleaned_data.get('episode_number')
episode_title = form.cleaned_data.get('episode_title')
episode_guest_first_name = form.cleaned_data.get('episode_guest_first_name')
episode_guest_last_name = form.cleaned_data.get('episode_guest_last_name')
episode_guest_twitter_name = form.cleaned_data.get('episode_guest_twitter_name')
episode_summary = form.cleaned_data.get('episode_summary')
user = request.user
# Get the production
# podcast = get_object_or_404(Podcast, id=podcast_id)
production = get_object_or_404(Production, id=production_id)
# Lets see if we have client and podcast for the user already
client, podcast = get_podfunnel_client_and_podcast_for_user(user)
if production is None:
production = Production.objects.create(podcast=podcast)
production.episode_number = episode_number
production.episode_title = episode_title
production.episode_guest_first_name = episode_guest_first_name
production.episode_guest_last_name = episode_guest_last_name
production.episode_guest_twitter_name = episode_guest_twitter_name
production.episode_summary = episode_summary
production.save()
# TODO: Needs to redirect to next step
return HttpResponseRedirect(reverse('podfunnel:episodeimagefiles'))
return render(request, self.template_name, {'form': form})
episode_info.py 表单:
class EpisodeInfoForm(forms.Form):
podcast_name = forms.CharField(widget=forms.Field.hidden_widget, required=False, max_length=100, disabled=True)
podcast_id = forms.IntegerField(widget=forms.Field.hidden_widget)
production_id = forms.IntegerField(widget=forms.Field.hidden_widget, required=False)
id = forms.IntegerField(widget=forms.Field.hidden_widget)
episode_number = forms.IntegerField(widget=forms.NumberInput, required=True)
episode_title = forms.CharField(max_length=255, required=True)
episode_guest_first_name = forms.CharField(max_length=128)
episode_guest_last_name = forms.CharField(max_length=128)
episode_guest_twitter_name = forms.CharField(max_length=64)
episode_summary = forms.CharField(widget=forms.Textarea)
【问题讨论】:
-
能否包含您的 html 模板?
标签: python django django-models django-forms django-class-based-views