def post(request, lang): sid = transaction.savepoint() if lang == 'ja': form = TopicForm(request.POST) topic_model = Topic() video_model = Video() else: form = TopicEnForm(request.POST) topic_model = TopicEn() video_model = VideoEn() if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: res_topic = topic_model.create_topic({ 'new': form.cleaned_data.get('new'), 'post_type': form.cleaned_data.get('post_type'), 'title': form.cleaned_data.get('title'), 'text': form.cleaned_data.get('text'), 'thumbnail': form.cleaned_data.get('thumbnail'), 'url': form.cleaned_data.get('url'), 'event_date': form.cleaned_data.get('event_date'), 'button_label': form.cleaned_data.get('button_label'), }) add_videos = form.cleaned_data.get('videos') if add_videos: video_model.add_video_from_topic(res_topic.id, add_videos) add_images = form.cleaned_data.get('images') if add_images: topic_model.add_image(res_topic.id, add_images) transaction.savepoint_commit(sid) return HttpResponseRedirect('/{}/admin/topics'.format(lang)) except: transaction.savepoint_rollback(sid) pass select_videos = [] if form.cleaned_data.get('videos'): video_ids = list(map(int, form.cleaned_data.get('videos'))) select_videos = video_model.get_by_ids(video_ids) select_images = [] if form.cleaned_data.get('images'): image_ids = list(map(int, form.cleaned_data.get('images'))) select_images = Image.get_by_ids(image_ids) return TemplateResponse( request, 'topic_create.html', { 'title': '新規投稿 | トピックス | FEED App 管理', 'select_videos': select_videos, 'select_images': select_images, 'form_data': form.cleaned_data, 'error_messages': get_error_message(request), 'lang': lang, 'post_types': [ {'name': 'トピック', 'val': 'topic'}, {'name': 'イベント', 'val': 'event'}, ] })