所以我开始将表单模型切换到类基本视图。在EpisodeInfoView
中,当我在production = Production.objects.create(podcast=podcast)
中的if podcast
方法中对get
下的pod_funnel.py
发表评论或取消评论时,它会显示表单并让我填写字段但是当我在admin下签入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)
答案 0 :(得分:0)
我会考虑使用CreateView / UpdateView和初始数据字典来设置值,然后将保存功能移动到表单本身。这种方式视图要小得多,逻辑就是这种形式。