【问题标题】:How do I display the form in a template?如何在模板中显示表单?
【发布时间】:2020-09-27 21:27:50
【问题描述】:

views.py

from django.shortcuts import render
from django.http import HttpResponse

from .models import *
from .forms import *
# Create your views here.

def index(request):
    tasks = Task.objects.all()

    form = TaskForm()    
    context = {'tasks':tasks, How do I display the value of a Django form field in a template?'form':forms}
    return render(request, 'tasks/list.html', context)

models.py

from django.db import models

# Create your models here.
class Task(models.Model):
    title = models.CharField(max_length =200)
    complete = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

forms.py

from django import forms

from .models import *

class TaskForm(forms.ModelForm):

    class Meta:
        model = Task
        fields = ('title','complete')

我希望表单域为我的 todo 初学者项目添加新任务。

我在 html 文件上使用 {{form}},但 Django 显示 <module 'django.forms' from '/home/***/.local/lib/python3.8/site-packages/django/forms/__init__.py'> 而不是任务字段

如何获取模板中的表单?

【问题讨论】:

标签: python django django-forms


【解决方案1】:

请遵循 Django 文档中的模板,它告诉您如何创建模板,请仔细阅读。我提到你的链接 https://docs.djangoproject.com/en/3.1/topics/templates/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 2011-02-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2018-04-07
    相关资源
    最近更新 更多