python - 我收到此错误 'User' 对象在联系表单中没有属性 'get'

标签 python django

H 当我想提交联系表单时,当我按下提交按钮时 我收到错误用户对象没有属性“get” 我在 else 函数末尾添加 else: contactform() 但之后又添加了 我收到此错误,我不知道我必须做什么? tnx 寻求帮助。

views.py

def ContactUs(request):
    title = 'Contac Us'
    if request.method =="POST":
        form = ContactForm(request.user)
        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            from_email = settings.EMAIL_HOST_USER
            to_email = [email,"some other thing "]
            contact_message = (name,email)
            send_mail(subject,contact_message,message,from_email,to_email,fail_silently=True)
    else :
        form = ContactForm()
    context = {'title': title, 'form': form}
    return render(request,'contact_us.html',context)

contact.html

{% extends "base.html" %}
{% block title %}
    Contact Us {{ block.super|title }}
{% endblock %}
{% block content %}
<div class="container">
    <div class="row">
        <div class="col">
            {{ title }}
            <form action="" method="POST">
                {% csrf_token %}
                {% for field in form %}
                <div class="form-group" >
                    {{ field.label_tag }}
                    {{ field.error }}
                    {{ field }}
                </div>
                {% endfor %}
                <input type="submit" class="btn btn-primary" value="Submit">
            </form>
        </div>
    </div>
</div>
{% endblock %}

表单.py

class ContactForm(forms.ModelForm):
    message = forms.TextInput()


    class Meta :
        model = ContactUs
        fields = ['name','email','subject']

最佳答案

您需要将 request.POST 属性传递给表单(或 request.GET 或另一个类似字典的对象,其中包含应传递给表单的数据)表单),而不是登录的用户,所以:

form = ContactForm(request.<b>POST</b>)

而不是:

form = ContactForm(request.<s><b>user</b></s>)

关于python - 我收到此错误 'User' 对象在联系表单中没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682229/

相关文章:

python - 类型错误 : Neo4j does not support PackStream parameters of type int64

python - 如何随机洗牌排列比 PRNG 周期更多的列表?

Python 如何将 zip 中的文件复制到内存中的另一个 zip?

python - 计算指定范围列的所有行中的非 0 值 - Python Pandas

python - 输出一列字数大于 3 的所有行

python - Django:将 views.py 分离到它自己的模块中是个好主意吗?

django social-registration 重定向 url 有时/social/setup 和有时/accounts/profile 。为什么?

python - Django hstore 字段和索引

python - Django 上的模板

python - 自定义 Django 字段来存储电子邮件地址列表