django form version 1.3
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 d开发者_如何学JAVAescribe how to start writing or send me an url that would be helpful.
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)
精彩评论