Buc*_*uck 5 django django-templates django-models django-forms
我想知道如何访问模板代码中的外键字段。
鉴于示例 models.py 代码:
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
class Book(models.Model):
title = models.CharField(max_length=100)
publisher = models.ForeignKey(Publisher)
Run Code Online (Sandbox Code Playgroud)
和示例 forms.py 代码:
class BookForm(ModelForm):
class Meta:
model = Book
fields = ['title', 'publisher']
Run Code Online (Sandbox Code Playgroud)
和示例 views.py 代码:
def sample(request):
bf = BookForm(request.POST)
return render(request, 'sample.html', {'BookForm': bf})
Run Code Online (Sandbox Code Playgroud)
使用“BookForm”从模板代码中正确访问值:publisher.name所需的代码行是什么?