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
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.