Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django form.field.value无法显示任何信息_Django - Fatal编程技术网

Django form.field.value无法显示任何信息

Django form.field.value无法显示任何信息,django,Django,我想编辑一篇文章的详细信息页面 如果请求方法是get,请使用数据预先制定表单 def edit_article(request, pk): article = Article.objects.get(pk=pk) if request.method == "GET": form = CommentForm(instance=article) context = {'form':form,} return render(request, "artic

我想编辑一篇文章的详细信息页面

如果请求方法是
get
,请使用数据预先制定表单

def edit_article(request, pk):
    article = Article.objects.get(pk=pk)
    if request.method == "GET":
        form = CommentForm(instance=article)
    context = {'form':form,}
    return render(request, "article/article_detail_edit.html", context)
模板

  <div class="form-group">
    <label for="title" class="col-sm-1 control-label">Title</label>
    <div class="col-sm-11">
      <input type="text" class="form-control" id="title" name="title"   value="{{ form.title.value }}">
    </div>
  </div>
  <div class="form-group">
    <label for="content" class="col-sm-1 control-label" >Content</label>
    <div class="col-sm-11">
      <textarea class="form-control" id="content" name="content" rows="10" cols="30">{{ form.content.value }}</textarea>
    </div>
  </div>
它打印所有数据

{'_state': <django.db.models.base.ModelState object at 0x10c977438>, 'id': 1, 'owner_id': 1, 'block_id': 1, 'title': 'Time and Tide', 'content':....}
文章模型

class Article(models.Model):
    STATUS = (
        (1,  'normal'),
        (0, 'deleted'),
    )
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    block = models.ForeignKey(Block, on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    content = models.TextField() # set the widget
    status = models.IntegerField(choices=STATUS)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ("id",)

    def __str__(self):
        return self.title
表单.py

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ['title', 'content']
        widgets = {'content': forms.Textarea}


class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('body',)
        widgets = {'body': forms.Textarea}

我的代码显示
form.field.value
有什么问题?

您没有将上下文中的表单传递给模板

试试看:

def edit_article(request, pk):
    article = Article.objects.get(pk=pk)
    if request.method == "GET":
        form = CommentForm(instance=article)
        context['form'] = form
    return render(request, "article/article_detail_edit.html", context)

我希望这将有助于您不要将上下文中的表单传递给模板

试试看:

def edit_article(request, pk):
    article = Article.objects.get(pk=pk)
    if request.method == "GET":
        form = CommentForm(instance=article)
        context['form'] = form
    return render(request, "article/article_detail_edit.html", context)

我希望这会有所帮助

看起来您正在向CommentForm传递一个文章实例。尝试:

if request.method == "GET":
    form = ArticleForm(instance=article)

因为CommmentForm没有标题或内容值。

看起来您正在将文章实例传递给CommentForm。尝试:

if request.method == "GET":
    form = ArticleForm(instance=article)

因为CommmentForm没有标题或内容值。

是否在上下文中更新表单?i、 e:context['form']=formI将表单传递给了context,它仍然没有显示任何内容@Bott0610可以显示您的文章模型吗?ty,当然@Bott0610好的,这是正确的,您的CommentForm类如何?您是否在上下文中更新表单?i、 e:context['form']=formI将表单传递给了context,它仍然没有显示任何内容@Bott0610可以显示您的文章模型吗?ty,当然@Bott0610好的,这是正确的,您的CommentForm类如何?我将表单传递给contextI将表单传递给上下文