我想從不在視圖相同目錄的模板存儲數據。 我的表單位於mysite/style.html
< -data這種形式插入,我想從另一個名爲「services」的應用程序的模型中傳遞它們。'NoneType'對象不可調用django cms
/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
回溯顯示錯誤在這行
Model = style(style=style, positions=positions, font_size=font_size, background=background, font=font)
對於像這樣的情況,當表單直接對應模型時,Django也具有ModelForm。 –