简体   繁体   English

如何在Django的反馈表单中包含Submitted_date和Submitted_by?

[英]How to include submitted_date and submitted_by in a feedback form in Django?

I have a feedback form in my app. 我的应用程序中有一个反馈表。 A user must be logged in to submit a feedback. 用户必须登录才能提交反馈。 A user is asked to enter feedback into the text-area. 要求用户在文本区域中输入反馈。 Upon submission, I want to store the user-details in submitted_by field and time of creation of the form as submitted_date (which also contains time). 提交后,我想将用户详细信息存储在Submitted_by字段中,并将表单创建的时间存储为Submitted_date(还包含时间)。

I have read many discussions regarding the use of auto-now_add=True and auto_now but there are many opinions about this topic and a little confusing too. 我已经阅读了许多有关使用auto-now_add=Trueauto_now但是对此主题有很多意见,也有些困惑。

I am using model-form to input data from the user. 我正在使用模型形式从用户输入数据。 This is how my different file looks like: 这是我的不同文件的样子:

# myapp/models.py

class Feedback(models.Model):
    content = models.TextField(max_length=100)
    submitted_by = models.ForeignKey(User)
    submitted_date = models.DateTimeField() # what do add here in arguments ?

    def __str__(self):
        return self.content + " @ " + self.submitted_date

# myapp/forms.py

class FeedbackForm(ModelForm):
    class Meta:
        model = Feedback
        fields = [ 'content' ]

# myapp/views.py
def addfeeback(request):
    if request.method == "POST":
        form = FeedbackForm(request.POST)
        if form.is_valid():
            form.save()
            # Do I need to do something here ?
            return redirect('home')
    else:
        form = FeedbackForm()
    return render(request, 'myapp/addFeedback.html', { 'form': form})

 # myapp/urls.py

 url(r'^feedback/$', core_views.addfeeback , name='add-feedback'),

 # myapp/templates/addFeedback.html

 {% extends 'registration/base.html' %}

 {% block title %} Feedback {% endblock %}

 {% block content %}
   <form method="post" >
     {% csrf_token %}
     {{ form.as_p }}
     <input type="submit" value="Submit">
   </form>
 {% endblock %}     

It will be highly appreciated if you can suggest relevant changes into my code to do as intended. 如果您可以建议对我的代码进行相关更改以按预期进行,将不胜感激。 Thanks. 谢谢。

There's nothing wrong with using auto_now_add . 使用auto_now_add没什么错。 But since you have to set the submitter as well as the date, you wouldn't be gaining much by doing so. 但是,由于您必须设置提交者以及日期,因此这样做不会带来太多收益。

The pattern though is to use commit=False when saving the form, then modifying the object that is returned before saving it manually. 但是,该模式是在保存表单时使用commit=False ,然后在手动保存之前修改返回的对象。

if form.is_valid():
    obj = form.save(commit=False)
    obj.submitted_by = request.user
    obj.submitted_date = datetime.datetime.now()
    obj.save()
    return redirect('home')

You can do 你可以做

if form.is_valid():
    obj = form.save(commit=False)
    obj.submitted_by = request.user
    obj.save()

In models 在模型中

submitted_date = models.DateTimeField(auto_now_add=True)

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

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