3

所以我有以下模型:

class Recipe(models.Model):
        title = models.CharField(max_length=100)
        ingredients = models.TextField(max_length=200,help_text="Put the ingredients required for the recepies here !")
        instructions = models.TextField(max_length=500)

        posted_on = models.DateTimeField('Posted On')

        def __unicode__(self):
                return self.title

现在我想要做的是我有一个名为 add.html 的 html 前端,其形式如下:

<!DOCTYPE html>


<head><title>New Recipe</title></head>
<body>
<h1>Add A new Recipe Here</h1>

<form action="/recipes/add/" method="post">
{% csrf_token  %}

<label>ID<label>
<input type="number" name="id"></input><br />

<label>Title </label>
<input type ="text" name="title"><br />

<label>Ingredients</label>
<input type="text" name="ingredients" />
<br />

<label>Instructions </label>
<input type="text" name="instructions" />
...

这是我使用以下方法保存表单的方法ModelForm

def add(request):
        if request.method == 'POST':
                form = RecipeForm(request.POST)
                if form.is_valid():

                        form.save()
                        #redirect
                        return HttpResponse("Thank you")
                else:
                        return HttpResponse("Form Not Valid")
        else:
                form = RecipeForm()

                context = Context({'form':form,})
                context.update(csrf(request))
                template = loader.get_template('myApp/add.html')
                return HttpResponse(template.render(context))

当我运行它时,我总是得到"form Invalid" 所以现在我的问题是,html 表单 add.html 是否应该将 EXACT 映射作为我的模型配方?
如果是,那么

  1. 如何types在 html 表单中添加相应的(for posted_on)?
  2. 如何处理由id隐式创建的syncdb
  3. 有没有其他选择?

我刚开始学习Django

4

2 回答 2

4

1) 更改posted_on为自动添加发布日期。

posted_on = models.DateTimeField(auto_now_add=True)

2) Django 将为您处理 pk id 创建。

3)为什么不使用 aModelForm呢?文档

class RecipeForm(ModelForm):
    class Meta:
        model = Recipe

您可以使用excludeincludeonfields确保您的表单仅包含Recipe您想要包含在表单中的字段。

于 2013-02-02T17:50:15.557 回答
2

模型.py

class Recipe(models.Model):
    title = models.CharField(max_length=100)
    ingredients = models.TextField(max_length=200,help_text="Put the ingredients required for the recepies here !")
    instructions = models.TextField(max_length=500)

    posted_on = models.DateTimeField(auto_add_now=True)

    def __unicode__(self):
            return self.title

page.html

<!DOCTYPE html>

<head><title>New Recipe</title></head>
<body>
<h1>Add A new Recipe Here</h1>

<form action="/recipes/add/" method="post">
{% csrf_token  %}
     {% form.as_p %}
     <input type="submit" value="submit">
</form>
</body>
</html>

视图.py

from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render

def add(request):
    if request.method == 'POST':
            form = RecipeForm(request.POST)
            if form.is_valid():
                form.save()
                    return HttpResponseRedirect(reverse('app_name:url'))
            else:
                messages.error(request, "Error")
    return render(request, 'myApp/add.html', {'form': RecipeForm()})
于 2013-02-01T18:56:21.457 回答