NameError:名称“ request”未定义(以Django形式) [英] NameError: name 'request' is not defined, in Django forms

查看:154
本文介绍了NameError:名称“ request”未定义(以Django形式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get the current logged in user in forms.py? I am trying to pre-populate the email field of the current user.

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)

,然后在<$ c $类内接收请求c> ContactMe as:

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:名称 request未定义。我知道请求可以在 html models.py views.py 。如何在 forms.py 中获得它?

It throws the error NameError: name 'request' is not defined. I know request is accessible in html, models.py, views.py. How to get it in 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)


推荐答案

在构造表单类时,您试图通过请求。目前没有任何要求。该请求仅存在于您的视图函数内。因此,在构造表单实例时,应该在视图函数中传递请求。要预填充表单,可以使用表单构造函数的 initial 关键字。

You are trying to pass the request when constructing the form class. At this point there is no request. The request only exists inside your view function. You should, therefore, pass the request in your view function when constructing the form instance. To prepopulate the form, you can use the initial keyword of the form constructor. It takes a dictionary of field names and values as input.

示例:

#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)

这篇关于NameError:名称“ request”未定义(以Django形式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆