'Client'对象没有属性'text' Django

um6iljoc  于 2023-03-04  发布在  Go
关注(0)|答案(1)|浏览(129)

我的项目中有模型

class Client(models.Model):
    """Аккаунт, с которого рассылаются сообщения"""
    name = models.CharField(max_length = 25)
    last_name = models.CharField(max_length = 25)
    description = models.TextField(max_length = 70)
    avatar = models.ImageField('Аватар', upload_to = 'sender/', blank = True)
    phoneNumberRegex = RegexValidator(regex = r"^\+?\d{8,15}$")
    phone_number = models.CharField(validators = [phoneNumberRegex], max_length = 16, unique = True)
    api_id = models.CharField(max_length=9)
    api_hash = models.CharField(max_length=33)
    proxy = models.ForeignKey(
        Proxy,
        blank=True,
        on_delete=models.CASCADE
    )
    owner = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='owner',
        verbose_name='Владелец',
    )

    class Meta:
        ordering = ('name',)
        verbose_name = 'Аккаунт'
        verbose_name_plural = 'Аккаунты'

    def __str__(self):
        return self.text

class Sender(models.Model):
    """Рассылка"""
    NUMBER_PHONE = 'По номеру телефона'
    USER_NAME = 'По имени пользователя'
    SENDER_CHOICES = (
        (NUMBER_PHONE, 'По номеру телефона'),
        (USER_NAME, 'По имени пользователя'),
    )
    name = models.CharField(
        'Название рассылки',
        max_length=10,
    )
    text = models.TextField(
        'Сообщение',
        max_length=4096,
    )
    type = models.CharField(
        'Тип рассылки',
        max_length=30,
        choices=SENDER_CHOICES,
        default=NUMBER_PHONE,
        db_index=True,
    )
    author = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='Mailer',
        verbose_name='Автор',
    )
    sender_man = models.ForeignKey(
        Client,
        on_delete=models.CASCADE,
        related_name='Sender_man',
        verbose_name='Отправитель',
        null=True,
        blank=True,
    )
    def __str__(self):
        return self.name

从这些模型中我创造出

class SenderForm(forms.ModelForm):
    class Meta:
        model = Sender
        fields = ('name', 'text', 'type', 'author', 'sender_man')

class ClientForm(forms.ModelForm):
    class Meta:
        model = Client
        fields = ('name', 'last_name', 'description', 'avatar', 'phone_number', 'api_id', 'api_hash', 'proxy')

当我创建Client模型的一个示例时,一切都没有问题,页面被呈现,我填写表单,提交表单,然后模型的一个新示例出现在数据库中。
当我尝试进入一个页面时,出现了这个问题,该页面应该有一个表单,Sender模型的示例将通过该表单添加
http://127.0.0.1:8000/sender/creating/
我收到错误“客户端”对象没有属性“文本”
enter image description here
他为什么要谈论“客户端”模型?在这个表单中,我正在访问Sender模型!
帮帮我吧。我完全搞不定了。
urls.py

from django.urls import path, include
from . import views

app_name = 'sender'

urlpatterns = [

    path('', views.index, name='index'),
  
    path('proxy/', views.proxy_list, name='proxy'),

    path('proxy/add/', views.proxy_add, name='new_proxy'),

    path('client/', views.client_list, name='client'),

    path('client/add/', views.client_add, name='new_client'),

    path('sender/', views.sender_list, name='sender'),

    path('sender/creating/', views.sender_create, name='new_sender'),

]

views.py

def sender_create(request):
    form = SenderForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        sender = form.save(commit=False)
        sender.author = request.user
        sender.save()
        return redirect('sender:sender')
    return render(request, 'sender/addsender.html', {'form': form})

模板添加发件人. html

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title %}
Форма заявления
{% endblock %}

{% block content %}

<div class="container">
  <div class="row justify-content-center">
    <div class="col-8">
      <br>
      <p class="h2">
        Создание рассылки
      </p>
    </div>
      <div class="col-4">
    </div>
  </div>
</div>
<div class="container">
  <div class="row justify-content-left">
    <div class="col-md-6 p-5">
      <div class="mb-3">
        <div class="card">
          <div class="card-body">
            <form method="post" enctype="multipart/form-data" action="{% url 'sender:new_sender' %}" class="row g-3 needs-validation" novalidate>
              {% csrf_token %}
              <div class="form-group">
                <div class="form-group row my-3 p-3">
              <p>
                  {{ form.name|as_crispy_field }}
              </p>
              <p>
                  {{ form.text|as_crispy_field }}
              </p>
              <p>
                  {{ form.type|as_crispy_field }}
              </p>
              <p>
                  {{ form.sender_man|as_crispy_field }}
              </p>
              <br>
              <hr>
              <br>
              <br>
              <div class="d-grid gap-2 col-8 mx-auto">
                <input type="submit" class="btn btn-primary btn-lg" value="ЗАПУСТИТЬ РАССЫЛКУ">
              </div>
        </form>
      </body>
      </body>
    </div>
  </div>
</div>
</div>
{% endblock %}

我尝试删除数据库,删除迁移。在def sender_create(request)函数中,我尝试注解掉个别行,并得出结论,错误发生在form = SenderForm(request.POST or None, request.FILES or None)这一行

jhdbpxl9

jhdbpxl91#

def __str__(self):
        return self.text

此处您提到了self.text,但客户端中没有文本字段。请将其替换为name

def __str__(self):
        return self.name

相关问题