Django表单并将初始值设置为登录用户?

Django表单并将初始值设置为登录用户?,django,django-forms,django-views,Django,Django Forms,Django Views,我有一张这样的表格: from django import forms from django.contrib.auth.models import User from django_countries.countries import COUNTRIES from statuses.models import Status class StatusForm(forms.Form): country = forms.ChoiceField(choices=COUNTRIES)

我有一张这样的表格:

from django import forms
from django.contrib.auth.models import User

from django_countries.countries import COUNTRIES

from statuses.models import Status

class StatusForm(forms.Form):
    country = forms.ChoiceField(choices=COUNTRIES)
    mood = forms.IntegerField()
    sleep_quality = forms.IntegerField()
此表单仅显示给登录的用户,如何设置
request.user
,以便用户提交此表单时,我可以将表单条目与他们关联?对于用户FK,我的模型如下所示:

from django.db import models
from django.contrib.auth.models import User

from django_countries import CountryField

class Status(models.Model):
    user = models.ForeignKey(User)
    country = CountryField()
    mood = models.SmallIntegerField(default=4)
    sleep_quality = models.SmallIntegerField(default=4)
以下是我对该表格的看法:

@login_required
def index(request, template_name="status/index.html"):
    if request.method == 'POST':
        postdata = request.POST
        form = StatusForm(postdata)
        if form.is_valid():
            messages.success(request, 'Something happened, good!')
            return redirect(urlresolvers.reverse('profile'))
    else:
        form = StatusForm()
    context = RequestContext(request, { 'form': form })
    return render_to_response(template_name, context)
我想也许我应该创建一个hiddenfield并在其中存储request.user,但这似乎不安全,因为它可以很容易地用firebug之类的工具进行编辑。关于如何存储此表单的request.user,有何建议


谢谢

当前用户将作为
request.user
出现在请求中,因此您不需要将其包含在表单中。相反,为什么不利用它们,因为它们将处理将对象链接到表单的问题

class StatusForm(forms.ModelForm):
    country = forms.ChoiceField(choices=COUNTRIES)
    # The other fields are automatically included, we just overwrite country

    class Meta:
        model = Status
        exclude = ("user")
那么在你看来,

...
form = StatusForm(request.POST):
    if form.is_valid():
        # Because your model requires that user is present, we validate the form and 
        # save it without commiting, manually assigning the user to the object and resaving
        obj = form.save(commit=False)
        obj.user = request.user
        obj.save()
        messages.success(request, 'Something happened, good!')
        return redirect(urlresolvers.reverse('profile'))
     ...

当前用户将以
request.user
的形式出现在请求中,因此您无需将其包含在表单中。相反,为什么不利用它们,因为它们将处理将对象链接到表单的问题

class StatusForm(forms.ModelForm):
    country = forms.ChoiceField(choices=COUNTRIES)
    # The other fields are automatically included, we just overwrite country

    class Meta:
        model = Status
        exclude = ("user")
那么在你看来,

...
form = StatusForm(request.POST):
    if form.is_valid():
        # Because your model requires that user is present, we validate the form and 
        # save it without commiting, manually assigning the user to the object and resaving
        obj = form.save(commit=False)
        obj.user = request.user
        obj.save()
        messages.success(request, 'Something happened, good!')
        return redirect(urlresolvers.reverse('profile'))
     ...

在你看来,你想做什么?当前用户总是可以在
请求中访问。user
因此您根本不需要在表单中有字段。pastylegs,我在上面添加了我的视图您在视图中尝试做什么?当前用户总是可以在
请求中访问。user
因此您根本不需要表单中有字段。pastylegs,我在上面添加了我的视图