Python 基于类的视图及其模型之间的源代码关系
根据,Python 基于类的视图及其模型之间的源代码关系,python,django,django-class-based-views,Python,Django,Django Class Based Views,根据, models.py from django.db import models class Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = mod
models.py
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
...
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
model = Publisher
from django.urls import path
from books.views import PublisherList
urlpatterns = [
path('publishers/', PublisherList.as_view()),
]
views.py
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
...
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
model = Publisher
from django.urls import path
from books.views import PublisherList
urlpatterns = [
path('publishers/', PublisherList.as_view()),
]
url.py
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
...
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
model = Publisher
from django.urls import path
from books.views import PublisherList
urlpatterns = [
path('publishers/', PublisherList.as_view()),
]
在模板中,一个名为object\u list
的变量,包含所有发布者对象
{% extends "base.html" %}
{% block content %}
<h2>Publishers</h2>
<ul>
{% for publisher in object_list %}
<li>{{ publisher.name }}</li>
{% endfor %}
</ul>
{% endblock %}
{%extends“base.html”%}
{%block content%}
出版者
{对象_列表%]中发布者的%
- {{publisher.name}
{%endfor%}
{%endblock%}
我一直在深入研究django的源代码,以找出django在模板上下文(
object\u list
)中显式放置Publisher
模型的所有对象的确切位置。但是到目前为止运气不好,有人能分享一些见解吗?ListView
是BaseListView
的子类。对象列表
属性已初始化: