使用表单集时出现缩进错误
我有这些模型:
class Tour(models.Model):
Name=models.CharField(max_length=100)
Count=models.SmallIntegerField()
PriceUnitCode=models.ForeignKey(PriceUnit)
Price=models.CharField(max_length=12)
Description=models.TextField()
ActionDate=models.DateTimeField(auto_now=True,editable=False)
ActionUserCode=models.ForeignKey(User,editable=False)
StatusTypeCode=models.ForeignKey(StatusType)
class Images(models.Model):
Image = models.ImageField(upload_to="gallery")
Tour=models.ForeignKey(Tour)
我想设计一个表单来添加一个游览,并在同一表单中为该游览添加一组图像。 这是我的FormModel:
class TourForm(ModelForm):
class Meta:
model = Tour
我在views.py中执行此操作:
def myview(request,key):
GalleryFormSet = inlineformset_factory(Tour,Images)
if request.method == 'POST':
form = TourForm(request.POST, request.FILES)
if form.is_valid():
tour=form.save()
formset=GalleryFormSet(request.POST, request.FILES,instance=tour)
if formset.is_valid():
formset.save()
else:
form = TourForm()
formset=GalleryFormSet()
return render_to_response('airAgency/addtour.html', {'form': form,'formset':formset})
在我的模板中:
<form method='POST' enctype="multipart/form-data" dir="rtl">
{% csrf_token %}
{{ form.as_p }}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
<input type="submit" />
</form>
但它有这个错误:
unexpected indent (views.py, line 122)
第122行是这样的:
if(form.is_valid()):
当然我对此代码进行了很多更改,但每次它在第122行中出现错误,而代码在第122行中122每次都在变化!!! 这是怎么回事?!!! 提前致谢
I have these models:
class Tour(models.Model):
Name=models.CharField(max_length=100)
Count=models.SmallIntegerField()
PriceUnitCode=models.ForeignKey(PriceUnit)
Price=models.CharField(max_length=12)
Description=models.TextField()
ActionDate=models.DateTimeField(auto_now=True,editable=False)
ActionUserCode=models.ForeignKey(User,editable=False)
StatusTypeCode=models.ForeignKey(StatusType)
class Images(models.Model):
Image = models.ImageField(upload_to="gallery")
Tour=models.ForeignKey(Tour)
I wanna design a form to add a tour and a set of Images for that Tour at the same form.
this is my FormModel:
class TourForm(ModelForm):
class Meta:
model = Tour
I do this in views.py :
def myview(request,key):
GalleryFormSet = inlineformset_factory(Tour,Images)
if request.method == 'POST':
form = TourForm(request.POST, request.FILES)
if form.is_valid():
tour=form.save()
formset=GalleryFormSet(request.POST, request.FILES,instance=tour)
if formset.is_valid():
formset.save()
else:
form = TourForm()
formset=GalleryFormSet()
return render_to_response('airAgency/addtour.html', {'form': form,'formset':formset})
in my template :
<form method='POST' enctype="multipart/form-data" dir="rtl">
{% csrf_token %}
{{ form.as_p }}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
<input type="submit" />
</form>
but it has this error:
unexpected indent (views.py, line 122)
line 122 is this :
if(form.is_valid()):
of course I changed this code alot,but every time it has error in line 122 while the code in line 122 is changing every time!!!
what's wrong with it?!!!
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很可能,您的缩进在第 122 行有所不同,很可能是因为前导空格存在不可见的差异。
示例:一个块中有很多行,全部以制表符开头,编辑器将其显示为... 8 个空格。其中一行(第 122 行)包含一个空格和一个制表符作为前导空白,编辑器将其显示为 ... 8 个空格。您看不到差异,但是 Python 的行开头不同,这就是问题所在。
建议:删除第 122 行中的所有前导空格,然后使用编辑器自动缩进以获得一致的结果。或者从第 122 行删除所有前导空格,并使用与块的其余部分相同的开头。
要验证情况是否如此,请使用 ViM 打开文件,然后输入
:set list
(所有字符)。现在 TAB 将显示为^I
,而空格将显示为单个空格字符。Very likely, your indentation differs in line 122, most probably because you have invisible differences in leading whitespace.
Example: You have many lines in a block, all start with a tab, which is displayed by your editor as ... 8 spaces. One of the lines (line 122) contains a space and a tab as leading whitespace, which is displayed by your editor as ... 8 spaces. You do not see the difference, yet, the line start is different for Python, and that is the problem.
Advice: Remove all leading space from line 122, then autoindent it with your editor to get consistent results. Or remove all leading whitespace from line 122 and use the same start as the rest of the block.
To verify this is the case, open your file with ViM, and type
:set list
(all characters). Now TAB will be displayed as^I
, while space will be displayed as a single space character.也许你在该行(或相反)中放置了空格而不是制表符,有时我会遇到这种情况
maybe you put spaces instead of tabs in that line (or the oposite), it happends to me sometimes
在Python中第122行是:
in python line 122 is:
诸如此类的最常见错误来源是前一种方法中的缩进更改,或者通常从报告实际错误的位置向上几行。问题的最常见来源是从网上复制+粘贴某些内容,这些内容要么是制表符/空格,要么是比您正在做的多/少缩进。
你的编辑应该警告你这样的事情。例如,Komodo IDE 提供箭头来指示缩进级别。因此,将其加载到一个精美的编辑器中,并用它来发现问题。有一篇关于 python IDE 的帖子。
The most common source of errors such as this one is indentation change in the previous method, or generally up a few lines from where the actual error is reported. The most most common source of the problem is copy+pasting something from the nets, which is either tabbed/spaced or indented more/less than what you are doing.
Your editor should warn you about stuff like this. For example Komodo IDE provides arrows to indicate indentation level. So load it up into a fancy editor, and use it to spot the problem. There's a SO post about python IDEs.