【问题标题】:'NoneType' object is not callable django cms'NoneType' 对象不可调用 django cms
【发布时间】:2017-04-18 01:29:00
【问题描述】:

我想存储与视图不在同一目录中的模板中的数据。 我的表单位于mysite/style.html

/mysite/ 是我的根目录。

 <form name = "form" action="{% url 'services.views.add_style' %}" method = "POST" class="form-inline">{% csrf_token %}
        <div class="col-sm-6 form-group">
          <input class="form-control" id="style" name="name" placeholder="style" type="style" required>
        </div>
        <div class="col-sm-6 form-group">
          <input class="form-control" id="color" name="color" placeholder="color" type="color" required>
        </div>

        <input class="form-control" id="positions" name="positions" placeholder="positions" type="positions" required>
        <input class="form-control" id="font_size" name="font_size" placeholder="font_size" type="font_size" required>
        <input class="form-control" id="background" name="background" placeholder="background" type="background" required>
        <input class="form-control" id="font" name="font" placeholder="font" type="font" required>
        <button class="btn btn-default pull-right" type="submit">Send</button>
</form>

比我分离的应用程序称为“服务”

这里是views.py

def add_style(request):
    if request.method == "POST":
        style = request.POST.get('style')
        color = request.POST.get('color')
        positions = request.POST.get('positions')
        font_size = request.POST.get('font_size')
        background = request.POST.get('background')
        font = request.POST.get('font')
        Model = style(style=style, color=color, positions=positions, font_size=font_size, background=background, font=font)
        Model.save()

    return redirect('/')

和models.py

class style(CMSPlugin):
        style = models.CharField(max_length=30)
        color=RGBColorField(max_length=30)
        positions = models.CharField(max_length=30)
        font_size = models.CharField(max_length=30)
        background = models.CharField(max_length=100)
        font = models.CharField(max_length=100)

        def __str__(self):
            return self.style

Traceback 在这一行显示错误

 Model = style(style=style,  positions=positions, font_size=font_size, background=background, font=font)

【问题讨论】:

    标签: django django-views django-cms


    【解决方案1】:

    问题出在您使用的 HTML 表单中: 在这一行:

      <input class="form-control" id="style" name="name" placeholder="style" type="style" required>
    

    name 属性定义为 name 但在您的 views.py 中,您使用的是:

     style = request.POST.get('style')
    

    所以正确的 HTML 表单应该是:

    <form name = "form" action="{% url 'services.views.add_style' %}" method = "POST" class="form-inline">{% csrf_token %}
      <div class="col-sm-6 form-group">
        <input class="form-control" id="style" name="style" placeholder="style" type="style" required>
      </div>
      <div class="col-sm-6 form-group">
        <input class="form-control" id="color" name="color" placeholder="color" type="color" required>
      </div>
    
      <input class="form-control" id="positions" name="positions" placeholder="positions" type="positions" required>
      <input class="form-control" id="font_size" name="font_size" placeholder="font_size" type="font_size" required>
      <input class="form-control" id="background" name="background" placeholder="background" type="background" required>
      <input class="form-control" id="font" name="font" placeholder="font" type="font" required>
      <button class="btn btn-default pull-right" type="submit">Send</button>
    </form>
    

    【讨论】:

      【解决方案2】:

      您有一个名为 style 的类,还有一个名为 style 的局部变量,它覆盖了该名称。

      class style(CMSPlugin):
          ...
      
      style = request.POST.get('style')  # this is None, and also overrides your class 
      ...
      Model = style(style=style, color=color,)  # you are trying to call None()
      

      如果您遵循使用标题大小写命名类的约定,您将避免此错误。

      class Style(CMSPlugin):  # Title case
          ...
      
      style = request.POST.get('style')  # No shadowing of other variable 
      ...
      model = Style(style=style, color=color,) 
      

      【讨论】:

      • Django 也有专门用于这种情况的 ModelForm,当表单直接对应于模型时。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2019-08-12
      • 2012-08-10
      • 2015-04-29
      相关资源
      最近更新 更多