大家好我在找到Django视图中的语法错误时遇到了麻烦。我正在尝试使用内联formset的帮助创建一个用户创建帖子的视图,如下面给出的代码。但视图是抛出错误如果出现以下情况:
我在(:)
下面得到错误指示器 if request.method == 'POST':
SyntaxError: invalid syntax
我检查了缩进但是找不到错误点。如果你正在读它,你会发现它吗?
#views.py
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from . import forms
from .models import *
@login_required
def post(request):
form = forms.PostForm()
formset =forms.AnswerInlineFormSet(queryset=models.Answer.objects.none()
if request.method == 'POST':
form = forms.PostForm(request.POST)
formset=forms.ImageInlineFormSet(request.POST,request.
FILES,queryset=Images.objects.none())
if form.is_valid() and formset.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
images = formset.save(commit=False)
for image in images:
image.post = post
image.save()
messages.success(request,'Post added.')
return HttpResponseRedirect('/feeds')
return render(request,'usersubmit/post_create.html',{'form'= form,'formset' = formset,})
这是视图的表单:
#forms.py
from django import forms
from .models import Post,Images
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'synopsis','category','tags' )
class ImageForm(forms.ModelForm):
class Meta:
model = Images
fields = ('image','body', )
ImageFormset= forms.modelformset_factory(
models.Images,
form=ImageForm,
)
ImageInlineFormSet = forms.inlineformset_factory(
models.Post,
models.Images,
extra=2,
fields = ('image','body', ),
formset=ImageFormset,
min_num=1,
)
你们可以查看我的代码吗?
答案 0 :(得分:0)
需要添加)
formset =forms.AnswerInlineFormSet(queryset=models.Answer.objects.none()
到
formset =forms.AnswerInlineFormSet(queryset=models.Answer.objects.none())
# ^^^