简体   繁体   English

django表格版本1.3

[英]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. ques_time和ans_time将在该时间自动更新。 Answer will be filled from site owner in the django admin. 答案将由django管理员中的网站所有者填写。 asker_ip will be automatically received from browser ip address. asker_ip将自动从浏览器的IP地址接收。

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. 如果您只能描述如何开始写作或给我发送一个有用的网址。

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. 创建一个验证表单的视图, 如下所示 :如果有一个POST(因此,用户提交了表单),他将创建一个包含所有提交值的表单对象,然后您就可以进行操作。 if there's no POST request, it simply creates an empty form for the template. 如果没有POST请求,它只会为模板创建一个空表单。 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. 如果表单已提交或为空,则可以使用另一个设置为True / False的变量。
  • in the template, you can check with template tags if the form exists (with the variable) or not, with something like: 在模板中,您可以使用模板标签检查表单是否存在(使用变量),例如:

    {% if form_sent %} {%,如果form_sent%}

    Bravo! 太棒了! You submitted something 您提交了一些东西

    {% else %} {{ form.as_p }} {% endif %} {%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) 请仔细阅读此页面,如果您错过任何内容,上传将不会进行(即,我永远不记得在表单中包含正确的mimetype,而且我总是会出错)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM