can any body help me, i try create contact form on python-django, and when i try make migrations on data base i recieve error "AttributeError: 'ContactUs' object has no attribute 'model'"
vievs.py
from django.shortcuts import render
from .forms import ContactForm, ContactUs
from django.core.mail import EmailMessage
from django.shortcuts import redirect
from django.template.loader import get_template
def contact(request):
form_class = ContactForm
# new logic!
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
first_name = request.POST.get('first_name', '')
last_name = request.POST.get('last_name', '')
date = request.POST.get('date', '')
month = request.POST.get('month', '')
year = request.POST.get('year', '')
sender = request.POST.get('sender', '')
message = request.POST.get('message', '')
licence = request.POST.get('licence', '')
phoneNumber = request.POST.get('phoneNumber', '')
zipCode = request.POST.get('zipCode', '')
cdlType = request.POST.get('cdlType', '')
# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = {
'first_name': first_name,
'last_name': last_name,
'date': date,
'month': month,
'year': year,
'sender': sender,
'message': message,
'licence': licence,
'phoneNumber': phoneNumber,
'zipCode': zipCode,
'cdlType': cdlType,
}
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
"Your website" + '',
['[email protected]'],
headers={'Reply-To': sender}
)
email.send()
return redirect('contact')
return render(request, 'email.html', {
'form': form_class,
})
def contact_us(request):
form_class = ContactUs
# new logic!
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
first_name = request.POST.get('first_name', '')
last_name = request.POST.get('last_name', '')
sender = request.POST.get('sender', '')
message = request.POST.get('message', '')
phoneNumber = request.POST.get('phoneNumber', '')
# Email the profile with the
# contact information
template = get_template('contact_us_template.txt')
context = {
'first_name': first_name,
'last_name': last_name,
'sender': sender,
'message': message,
'phoneNumber': phoneNumber,
}
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
"Your website" + '',
['[email protected]'],
headers={'Reply-To': sender}
)
email.send()
return redirect('contact_us')
return render(request, 'email2.html', {
'form': form_class,
})
models.py
from django.db import models
class Form(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
date = models.DateTimeField()
month = models.DateTimeField()
year = models.DateTimeField()
sender = models.EmailField()
message = models.CharField()
licence = models.CharField(max_length=100)
zipCode = models.CharField(max_length=100)
phoneNumber = models.CharField(max_length=100)
cdlType = models.CharField(max_length=100)
class ContactUs(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
sender = models.EmailField()
message = models.CharField()
phoneNumber = models.CharField(max_length=100)
forms.py
from django import forms
class ContactForm(forms.Form):
first_name = forms.CharField(max_length=100, required=True)
last_name = forms.CharField(max_length=100, required=True)
date = forms.DateTimeField(required=True)
month = forms.DateTimeField(required=True)
year = forms.DateTimeField(required=True)
sender = forms.EmailField(required=True)
message = forms.CharField(required=True)
licence = forms.CharField(max_length=100, required=True)
zipCode = forms.CharField(max_length=100, required=True)
phoneNumber = forms.CharField(max_length=100, required=True)
cdlType = forms.CharField(max_length=100, required=True)
class ContactUs(forms.Form):
first_name = forms.CharField(max_length=100, required=True)
last_name = forms.CharField(max_length=100, required=True)
sender = forms.EmailField(required=True)
message = forms.CharField(required=True)
phoneNumber = forms.CharField(max_length=100, required=True)
traceback
C:\Contact_Form\back>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Contact_Form\testenv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Contact_Form\testenv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Contact_Form\testenv\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Contact_Form\testenv\lib\site-packages\django\core\management\base.py", line 350, in execute
self.check()
File "C:\Contact_Form\testenv\lib\site-packages\django\core\management\base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "C:\Contact_Form\testenv\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Contact_Form\testenv\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Contact_Form\testenv\lib\site-packages\django\contrib\admin\checks.py", line 26, in check_admin_app
errors.extend(site.check(app_configs))
File "C:\Contact_Form\testenv\lib\site-packages\django\contrib\admin\sites.py", line 81, in check
if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'ContactUs' object has no attribute 'model'
admin.py
from django.contrib import admin
from .models import Form, ContactUs
admin.site.register(Form, ContactUs)
At start i made contact form just for sending mails but then i modificate it because i need display data in site admin.
from django.contrib import admin
from .models import Form, ContactUs
admin.site.register(Form, ContactUs)
change your admin file to this
from django.contrib import admin
from .models import Form, ContactUs
admin.site.register(Form)
admin.site.register(ContactUs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With