0

I have this form:

class TourForm(ModelForm):
   class Meta:
       model = Tour 

I wanna design a edit page,so i send tour_id to page and get tour with that tour_id,now I wanna fill this form with tour instance I've gotton.some thing like this in views.py:

tour=Tour.objects.get(pk=tour_id)
tform = TourForm(request.POST, request.FILES,instance=tour)

any help?

update:

these are models:

class Gallery(models.Model):
   HeadImage = models.ImageField(upload_to="gallery")

class Image(models.Model):
   Image = models.ImageField(upload_to="gallery")
   Gallery = models.ForeignKey(Gallery, related_name='images')



class Tour(models.Model):
   Name=models.CharField(max_length=100)
   Count=models.SmallIntegerField() 
   PriceUnit=models.ForeignKey(PriceUnit)
   Price=models.CharField(max_length=12)
   Description=models.TextField()
   ActionDate=models.DateTimeField(auto_now=True,editable=False)
   ActionUser=models.ForeignKey(User,editable=False)
   StatusType=models.ForeignKey(StatusType)
   Gallery = models.OneToOneField(Gallery,editable=False)

fields that are foreigkey like PriceUnit and StatusType,doesn't fill in form.

thanks in advance

1
  • sorry! I updated the question now. Commented Oct 27, 2011 at 9:29

1 Answer 1

1

I think that you should not pass instance nad request.POSt at the same time. Try to do something like this:

if request.method == "POST":
    tform = TourForm(request.POST, request.FILES)
else
    tform = TourForm(instance=tour)
Sign up to request clarification or add additional context in comments.

2 Comments

Any reasons not to pass them together?
@DrTyrsa generally because doing so triggers form validation. Form({}) is an invalid form, and will display errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.