组合上下文和表单时,模板显示标记而不是页面
template shows markup and not the page when combining context and forms
我对 Django 还很陌生。我试图在同一页上显示字典和表格。但是,它只查看 Html 标记,而不查看实际页面。这是我的代码:
home/views.py:
from django.shortcuts import render
from recruit.forms import RecruitForm
articles = [
{
'author': 'Glenn',
'title':'title 1',
'content': 'first content',
'date_posted': 'January 12'
},
{
'author': 'batman',
'title':'title 2',
'content': 'second content',
'date_posted': 'January 12'
},
{
'author': 'batgirl',
'title':'title 3',
'content': ' Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptas ratione
eaque nam totam! Labore consectetur nostrum dicta magnam ex expedita facilis
illum odit quibusdam vitae?',
'date_posted': 'January 12'
}
]
# Create your views here.
def index(request):
form = RecruitForm()
context = {
'articles': articles
}
return render(request, 'home/index.html', context, {'form':form})
recruit/forms.py:
from django import forms
from phonenumber_field.modelfields import PhoneNumberField
from .models import Recruit
class RecruitForm(forms.ModelForm):
email = forms.EmailField()
phone = PhoneNumberField()
class Meta:
model = Recruit
fields = ['firstname', 'lastname', 'email', 'phone', 'city', 'contact_preference']
widgets = {'contact_preference': forms.RadioSelect }
当我不传递上下文时,整个页面都会呈现。形式也一样。我知道如何验证表单,我只是想让这个问题尽可能通用。所以要么我可以传递形式或上下文,但不能同时传递。当我都通过时,我得到了我的 html 标记而不是文档。我所说的标记是指实际的 .html 标记,所以 ... 任何帮助表示赞赏。
如果您将两个对象传递给模板,您将创建一个包含两个 条目 的字典,因此:
def index(request):
form = RecruitForm()
return render(request, 'home/index.html', <b>{'form':form, 'articles': articles}</b>)
所以你没有通过两个字典。 render(…)
function [Django-doc] 的第四个参数是 content_type
,不是一些额外的上下文。
我对 Django 还很陌生。我试图在同一页上显示字典和表格。但是,它只查看 Html 标记,而不查看实际页面。这是我的代码:
home/views.py:
from django.shortcuts import render
from recruit.forms import RecruitForm
articles = [
{
'author': 'Glenn',
'title':'title 1',
'content': 'first content',
'date_posted': 'January 12'
},
{
'author': 'batman',
'title':'title 2',
'content': 'second content',
'date_posted': 'January 12'
},
{
'author': 'batgirl',
'title':'title 3',
'content': ' Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptas ratione
eaque nam totam! Labore consectetur nostrum dicta magnam ex expedita facilis
illum odit quibusdam vitae?',
'date_posted': 'January 12'
}
]
# Create your views here.
def index(request):
form = RecruitForm()
context = {
'articles': articles
}
return render(request, 'home/index.html', context, {'form':form})
recruit/forms.py:
from django import forms
from phonenumber_field.modelfields import PhoneNumberField
from .models import Recruit
class RecruitForm(forms.ModelForm):
email = forms.EmailField()
phone = PhoneNumberField()
class Meta:
model = Recruit
fields = ['firstname', 'lastname', 'email', 'phone', 'city', 'contact_preference']
widgets = {'contact_preference': forms.RadioSelect }
当我不传递上下文时,整个页面都会呈现。形式也一样。我知道如何验证表单,我只是想让这个问题尽可能通用。所以要么我可以传递形式或上下文,但不能同时传递。当我都通过时,我得到了我的 html 标记而不是文档。我所说的标记是指实际的 .html 标记,所以 ... 任何帮助表示赞赏。
如果您将两个对象传递给模板,您将创建一个包含两个 条目 的字典,因此:
def index(request):
form = RecruitForm()
return render(request, 'home/index.html', <b>{'form':form, 'articles': articles}</b>)
所以你没有通过两个字典。 render(…)
function [Django-doc] 的第四个参数是 content_type
,不是一些额外的上下文。