python - Django 表单中的 NameError : name 'request' is not defined,

标签 python django django-forms django-views

如何在 forms.py 中获取当前登录的用户?我正在尝试预填充当前用户的电子邮件字段。

class ContactMe(forms.Form):
    name                 = forms.CharField(label = "Name")
    email_address        = forms.CharField(label = "Email Address", intital = request.user.email)
    subject              = forms.CharField(label = "Subject")
    message              = forms.CharField(label = "Message", widget=forms.Textarea(attrs={'cols': 10, 'rows': 3}))
    additional_comments  = forms.CharField(required = False)
    class Meta:
        model = Contact_me

我尝试将来自 views.py 的请求传递为:

contact_form = ContactMe(request.POST or None, request)

然后在 ContactMe 类中接收请求:

class ContactMe(forms.Form, request):
    name                 = forms.CharField(label = "Name")
    email_address        = forms.CharField(label = "Email Address", intital = **request.user.email**)
    subject              = forms.CharField(label = "Subject")
    message              = forms.CharField(label = "Message", widget=forms.Textarea(attrs={'cols': 10, 'rows': 3}))
    additional_comments  = forms.CharField(required = False)
    class Meta:
        model = Contact_me

它抛出错误 NameError: name 'request' is not defined。我知道可以在 htmlmodels.pyviews.py 中访问请求。如何在forms.py中获取?

views.py:

def list_posts(request):
    request.session.set_expiry(request.session.get_expiry_age())        # Renew session expire time
    instance_list = Post.objects.all()
    register_form = UserRegisterForm(data=request.POST or None)
    if register_form.is_valid():
        personal.views.register_form_validation(request, register_form)

    login_form = UserLoginForm(request.POST or None)
    if login_form.is_valid() :
        personal.views.login_form_validation(request, login_form)

    feedback_form = FeedbackForm(request.POST or None)
    if feedback_form.is_valid() :
        personal.views.feedback_form_validation(request, feedback_form)

    contact_form = ContactMe(request.POST or None, request)
    if contact_form.is_valid() :
    personal.views.contact_form_validation(request, login_form)

    if request.POST and not(register_form.is_valid() or login_form.is_valid()):
        if request.POST.get("login"):
            return accounts.views.login_view(request)
        else:
            return accounts.views.register_view(request)

    template = 'blog/archives.html'
    dictionary = {

        "object_list"   : content,
        "register_form" : register_form,
        "login_form"    : login_form,
        "feedback_form" : feedback_form,
        "contact_form"  : contact_form,
    }
    return render(request,template,dictionary)

最佳答案

您在构造表单类时试图传递请求。此时没有请求。该请求仅存在于您的 View 函数中。因此,您应该在构造表单实例时在 View 函数中传递请求。要预填充表单,您可以使用表单构造函数的 initial 关键字。它以字段名称和值的字典作为输入。

例子:

#views.py
from django.shortcuts import render
from django import forms

class TestForm(forms.Form):
    foo = forms.CharField()

def test_form(request):
    form = TestForm(initial=dict(foo=request.<some_property>))
    context = dict(form=form)
    template_name = 'testapp/test.html'
    return render(request, template_name, context)

关于python - Django 表单中的 NameError : name 'request' is not defined,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42904050/

相关文章:

python - requests-html 找不到页面元素

python - 使 Python 3.3.3 脚本可执行?

python - django密码重置NoReverseMatch错误

django - 如何在 Django 抽象模型类中动态命名权限?

python - Django 表单在控制台中验证,但为什么不在浏览器中验证?

Django 表单不调用 clean_<fieldname>

python - 将列表作为值的字典与生成值的字典进行比较

python - 在工作表之间切换

python - 具有来自另一个模型的两个字段的django模型

django - 为非 super 管理员的用户排除 django admin 中的字段