-1

I want to have a user submitted form in a certain page. My model is like this

class Ask(models.Model):
    name = models.CharField(max_length=500)
    designation = models.CharField(max_length=200, blank=True)
    email = models.EmailField()
    phone = models.CharField(max_length=20,  blank=True)
    avatar = models.ImageField(upload_to="/ask/users/avaters", blank=True)
    question = models.CharField(max_length= 1024)
    ques_time = models.DateField()
    answere = models.TextField()
    ans_time = models.DateField()
    display = models.BooleanField()
    asker_ip = models.CharField(max_length=100)

From user I will receive name, designation, email, phone, avater and question. ques_time and ans_time will automatically update in that time. Answer will be filled from site owner in the django admin. asker_ip will be automatically received from browser ip address.

I have a basic template in the template section. When the form submitted user will redirect to that page but with a success message in the form portion. BTW I want user to upload that avater image.

Now how should I start on this topic. If you can just describe how to start writing or send me an url that would be helpful.

laurent
  • 88,262
  • 77
  • 290
  • 428
themunna
  • 47
  • 1
  • 6
  • I'm not sure what you're asking, it sounds like you want the following: the user submits the form, once its been successfully submitted you render the same page but with a success message instead of the form. Is that so? – Phil Aug 02 '11 at 13:50
  • yes, also I need to know how to enter the user submitted data in the database – themunna Aug 02 '11 at 15:22
  • Anything wrong with the very good documentation on [forms for models](https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/)? – Daniel Roseman Aug 02 '11 at 16:04
  • :) I wasn't understand a thing. But now its looking a bit understandable. I was looking for a answer like this... "Do this, read here do this create a file name this etc." BTW I'm a complete newbie to python, django – themunna Aug 02 '11 at 16:25

1 Answers1

2

you can do something like this:

  • create a view that validates the form like this: if there's a POST (so, user submitted a form) he creates a form object with all the submitted values in it, then you can manipulate. if there's no POST request, it simply creates an empty form for the template. both ways, the view returns a form to the template. maybe use another variable set to True/False if form has been submitted or it's empty.
  • in the template, you can check with template tags if the form exists (with the variable) or not, with something like:

    {% if form_sent %}

    Bravo! You submitted something

    {% else %} {{ form.as_p }} {% endif %}

something like this. this should cover the first part of your question. This is what you're looking for files upload. Read this page very carefully, if you miss something, the upload won't go (ie, i never remember to include the correct mimetype into the form, and i always get errors)

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
  • actually I already have the form in the template. I just want to to save the data when the form is submitted. So let me clear, I will make a form.py file. Validate the form in the view so Where should I say save this form data? BTW I read the first link you gave. It goes above my head. :) another thing how I get the user/form submitter's ip? – themunna Aug 02 '11 at 15:36
  • you perform the save in the view, then everything is stored in your backend. ex: the form gives you some valid data, you store the data into an Ask object and then you call the save() method from within the view (where you manipulate your data) and it stores the object into the db. If you check with my first link, the save can be done after the is_valid() method call (inside that block) – Samuele Mattiuzzo Aug 03 '11 at 07:47