我希望你能给我一些建议。 所以我无法通过RecipeCreateForm将Recipe保存到我的数据库中。
这是我的代码: models.py
class Recipe(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='recipes_created')
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, blank=True)
image = models.ImageField(upload_to='recipes/%Y/%m/%d')
description = models.TextField("Description")
ingredients = models.TextField("Ingredients")
preparation = models.TextField("Preparation")
created = models.DateField(auto_now_add=True, db_index=True)
def __str__(self):
return self.title
forms.py
from django import forms
from .models import Recipe
class RecipeCreateForm(forms.ModelForm):
class Meta:
model = Recipe
fields = ('title', 'image' 'description', 'ingredients', 'preparation')
答案 0 :(得分:0)
您需要在views.py文件中创建一个视图。要创建新对象,视图将如下所示:
comment 99(this is id number)
<div class="dialogbox">
<div class="body">
<span class="tip tip-left"></span>
<div class="message">
<span id="99">
<b><a href="/profil/index/1">User Name</a></b>
<div class="comment">hello world first clck on this. after click on o other edit button. </div>
<a style="float:right; font-size: 9px;" onclick="return false;" href="" title="">1 day ago</a><br>
<a href="/posta/index/1">See the post</a> | <a class="edit" id="edit" onclick="return false;">Edit</a> |
<a href="/comment/deletecomment/99">Delete</a>
</span>
</div>
</div>
</div>
<br><br>
comment 12 (this is id number)
<div class="dialogbox">
<div class="body">
<span class="tip tip-left"></span>
<div class="message">
<span id="12">
<b><a href="/profil/index/1">User Name</a></b>
<div class="comment">hello world</div>
<a style="float:right; font-size: 9px;" onclick="return false;" href="" title="">1 day ago</a><br>
<a href="/posta/index/1">See the post</a> | <a class="edit" id="edit" onclick="return false;">Edit</a> |
<a href="/comment/deletecomment/12">Delete</a>
</span>
</div>
</div>
</div>
$(document).on("click", ".edit", function(event) {
var obje = $(event.target).parent();
var commentID = obje.attr("id");
var prev = $(event.target).prev().prev().prev().prev();
comment = prev.html();
prev.html("<div><textarea class='commentval' style='width: 458px;'>"+comment+"</textarea><button class='save'>save</button></div>");
$(event.target).removeAttr("class","edit");
$(event.target).attr("class", "cancel");
$(event.target).html("Cancel");
});
$(document).on("click", ".cancel", function(event) {
$(event.target).removeAttr("class","cancel");
$(event.target).attr("class", "edit");
$(event.target).html("Edit");
var prev = $(event.target).prev().prev().prev().prev();
prev.html(comment);
});
如果请求是post,即您正在将数据从表单保存到数据库。如果请求得到你正在呈现一个空表格。
答案 1 :(得分:0)
你错过了一个&#39;,&#39;在&#39;图像&#39;之间的形式和&#39;描述
这就是为什么错误是&#39; imagedescription&#39;,它将它们都读作1个字符串。
fields = ('title', 'image' 'description', 'ingredients', 'preparation')
与&#39;,&#39;它会读
fields = ('title', 'image', 'description', 'ingredients', 'preparation')