AttributeError:'ContactUs'对象没有属性'model'

loading...


0

任何身体可以帮助我,我尝试在python-django上创建联系表单,当我尝试在数据库上进行迁移时我收到错误“AttributeError:'ContactUs'对象没有属性'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" + '',
            ['youremail@gmail.com'],
            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" + '',
                ['youremail@gmail.com'],
                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)

追溯

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)

一开始我只是发送邮件联系表格,然后我修改它,因为我需要在网站管理员显示数据 .

loading...

2回答

  • 0
    from django.contrib import admin
    from .models import Form, ContactUs
    admin.site.register(Form, ContactUs)
    

    将您的管理文件更改为此

    from django.contrib import admin
    
    from .models import Form, ContactUs
    
    admin.site.register(Form)
    admin.site.register(ContactUs)
    

  • 0

    您可以将多个模型注册为列表

    admin.site.register([Form, ContactUs])
    
评论

暂时没有评论!