Reputation: 37
H when i want to submit contact form when i push submit button i get error user object has no attribute 'get' i add else: in end of function else : contactform() but after that again i get this error i don't know what i have to do ? tnx for help .
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 %}
Forms.py
class ContactForm(forms.ModelForm):
message = forms.TextInput()
class Meta :
model = ContactUs
fields = ['name','email','subject']
Upvotes: 1
Views: 450
Reputation: 477230
You need to pass the request.POST
attribute to the form (or request.GET
or another dictionary-like object that contains the data that should be passed to the form), instead of the user that logged in, so:
form = ContactForm(request.POST)
instead of:
form = ContactForm(request.user)
Upvotes: 3