我想存储来自与视图不在同一目录中的模板的数据。
我的表单位于mysite/style.html
< -data插入此表单中,我想在另一个名为" services"的应用程序的模型中传递它们。
/ 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>
比我分离了名为&#34; services&#34;
的应用程序这是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)
答案 0 :(得分:0)
问题出在您使用的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>