是否有一种简单的方法来显示数据 mysql 在模板中 Django 没有创建申请?

我的数据库中有一个表 MySQL 名称 mysite_categories, 有 4 专栏,但是为了我的目的,我只需要两个 /name, base_url/.

目前我有一个模板 '*base_categories.html*', 我用来手动加载类别。

base_categories.html /裁剪

/


{% block content %}
<div class="section" style="float: right;">
<h4 class="gradient">Category List</h4>
<ul>
<li><a href="/categories/Art" id="nav_font">Art</a></li>
<li><a href="/categories/Biography" id="nav_font">Biography</a></li>
<li><a href="/categories/Science" id="nav_font">Science</a></li>
</ul>
</div>
{% endblock %}


我想做的是从数据库中提取数据并使用它们 for loop. 有点:


{% block content %}
<div class="section" style="float: right;">
<h4 class="gradient">Category List</h4>
<ul>
{% for category in mysite_categories %}
<li><a href="{{ category.base_url }}" id="nav_font">{{ category.name }}</a></li>
{% endfor %}
</ul>
</div>
{% endblock %}


这可能是一个初学者问题,但没有创建应用程序是类似的东西?


*EDIT 1*


这些是我的应用程序文件,我承认它可能是一个垃圾,我尝试了许多不同帖子的许多编辑,我相信某个地方打破了它 :P. 我打算删除它并首先开始一切,但决定我也可以发表他看看我犯了一个错误的地方?

views.py


from django.shortcuts import render_to_response
from categories.models import categoryList


def index/request/:
categories = categoryList.objects.all//
extra_context = {"categories": categories}

return render_to_response/"myapp/index.html", extra_context/


models.py


from django.db import models


class categoryList/models.Model/:
#id = models.IntegerField/unique=True, db_column='ID'/ # Field name made lowercase.
name = models.CharField/max_length=255L, unique=True/
base_url = models.CharField/max_length=255L, unique=True/
thumb = models.CharField/max_length=1L, unique=True, blank=True/
class Meta:
db_table = 'mysite_categories'


index.html


{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<div class="section" style="float: right;">
<h4 class="gradient">Category List</h4>
<ul>
{% for category in categories %}
<li><a href="" id="nav_font">{{ category.title }}</a></li>
{% endfor %}
</ul>
</div>


正如已经提到的那样,目前它可能是垃圾,如果你们中的任何人都可以帮助我弄清楚,我会非常感激!


*EDIT 2*


base_right_panel.html


{% block content %}
<div style="float: right;">
<div id="base_categories" style="margin: 10px; padding-bottom: 10px;">
{% block base_categories %}
{% include "base_categories.html" %}
{% endblock %}
</div>
</div>
{% endblock %}



*Edit 3*


base_categories.html


{% block content %}
<div class="section" style="float: right;">
<h4 class="gradient">Category List</h4>
<ul>
{% if categories %}
{% for category in categories %}
<li><a href="" id="nav_font">{{ category.title }}</a></li>
{% endfor %}
{% else %}
<p>no data! {{ categories|length }}</p>
{% endif %}
</ul>
</div>
{% endblock %}



*EDIT 4*


/应用程序的名称已更改为 CategoryList/

CategoryList/views.py


from django.views.generic import TemplateView
from CategoryList.models import CategorylistCategorylist #<-- Changed to match inspectdb result

class IndexView/TemplateView/:
template_name="categorylist.html" #<-- Changed name from index.html for clarity

def get_context_data/self, **kwargs/:
context = super/IndexView, self/.get_context_data/**kwargs/
context["categories"] = CategorylistCategorylist.objects.all//
return context


CategoryList/models.py


from django.db import models

class CategorylistCategorylist/models.Model/: #<-- Changed to match inspectdb
id = models.IntegerField/primary_key=True/
name = models.CharField/max_length=255L, unique=True/
base_url = models.CharField/max_length=255L, unique=True/
thumb = models.ImageField/upload_to="dummy", blank=True/ #<-- Ignored inspectdb's suggestion for CharField

def __unicode__/self/:
return self.name

# Re-added Meta to match inspectdb
class Meta:
db_table = 'categorylist_categorylist'


CategoryList/urls.py


from django.conf.urls.defaults import patterns, url, include
from django.contrib import admin
from django.conf import settings
from CategoryList import views

admin.autodiscover//

urlpatterns = patterns/'',
url/r'^$', views.IndexView.as_view//, name='categorylist'/,
/

if settings.DEBUG:
urlpatterns = patterns/'',
url/r'^media//?P<path>.*/$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}/,
url/r'', include/'django.contrib.staticfiles.urls'//,
/ + urlpatterns


MySite/urls.py


from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from home import views as home_view
from CategoryList import views as index_view

admin.autodiscover//

urlpatterns = patterns/'',
url/r'^$', home_view.HomeView.as_view//, name="home"/,

url/r'^categories/$', index_view.IndexView.as_view//, name='categorylist'/,#include/'CategoryList.urls'//,

url/r'^admin/', include/admin.site.urls//,
#url/r'^admin/doc/', include/'django.contrib.admindocs.urls'//,
/

if settings.DEBUG:
urlpatterns = patterns/'',
url/r'^media//?P<path>.*/$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}/,
url/r'', include/'django.contrib.staticfiles.urls'//,
/ + urlpatterns


到目前为止,我可以下载 url "

localhost:8000/categories

", 我会看到类别名称列表将显示在屏幕的右侧,如预期,但模板的格式不适用。 在我的文件里面 "*base_right_panel.html*" 我试过了 "

{%include "categorylist.html%}

" 直接绑定显示模板正确格式的应用程序,但显示答案。 "

{% else %}

" 经过 "

{% if categories %}

" 而不是类别? 我试图改变 include, 表示 "

categories/

", 在浏览器中有效,但他告诉我它找不到模板?

我现在很困惑......?
</path></path>
已邀请:

奔跑吧少年

赞同来自:

这个答案并不意味着与仲裁者的答案不同意 - 他只是添加有关使用模板的其他信息。

主要查询处理程序 HTTP 到申请 Django 是演示文稿。 介绍收到请求 HTTP, 以及捕获的任何参数 URL, 并负责返回实例
HttpResponse

/或他的一个子类的例子/, 它将返回到浏览器。

表示不需要使用任何特定的创建方法
HttpResponse

. 模板的呈现以启用从数据库中获取的信息以及请求或参数信息 URL, 它足以分发,以便为其支持(例如快捷方式)存在代码
render

或其主要过时的前身
render_to_response

, 但这不是必需的情况。 完全合法地有一个想法直接建造
HttpResponse

:


def index/request/:
return HttpResponse/'This is an index page.'/


或者,非常简单 HTML:


def index/request/:
return HttpResponse/'<html><head><title>example</title></head><body>This is an index page.</body></html>'/


在实践中,我经常创建标本
HttpResponse

直接返回数据 json 或动态创建的文件 PDF 或者 Excel.

在答案中插入从数据库收到的信息的简单方法 - 使用内置字符串插值 Python:


def index/request/:
return HttpResponse/'Hello, %s' % request.user.email/


或者您可以使用高级字符串格式选项:


def index/request/:
user_names = {}
user_names['first_name'] = request.user.first_name
user_names['last_name'] = request.user.last_name
return HttpResponse/'Hello, %/first_name/s %/last_name/s' % user_names/


这一切都是建立在一定程度上,无论您如何生成您的文本内容并不重要
HttpResponse

. 一切都很重要 - 这就是你将返回的东西。

模板系统是一个强大而可扩展的工具,用于创建文本内容,但这就是她所做的一切。 如果你看
https://docs.djangoproject.com ... ntext
关于模板呈现,您将看到一些与上述字符串插值完全一致的示例。


render_to_response

- 这是一个带模板和上下文并返回的标签
HttpResponse

具有此模板和上下文的可视化内容。 通过它的参数
context_instance


content_type

为了演示,这两个代码块实际上是相同的:


def index/request/:
t = Template/'Hello, {{ first_name }} {{ last_name }}'/
c = Context/{'first_name': request.user.first_name, 'last_name': request.user.last_name}/
response_text = t.render/c/
return HttpResponse/response_text/


假设模板
index.txt

如下所示,在锥度中的录制顶部
TEMPLATE_DIRS

范围
https://docs.djangoproject.com ... tting

index.txt
Hello, {{ first_name}} {{ last_name}}


然后可以替换顶视图:


def index/request/:
t = get_template/'index.html'/
c = Context/{'first_name': request.user.first_name, 'last_name': request.user.last_name}/
response_text = t.render/c/
return HttpResponse/response_text/


此外,您还可以跳过语境对象的显式创建和字符串中模板的呈现,因此:


def index/request/:
return render_to_response/'index.html', {'first_name': request.user.first_name, 'last_name': request.user.last_name}/


在更高版本中 Django 您通常必须使用快捷方式
render

, 但不是
render_to_response

- 如果您仍然在使用模板中收到上下文仍在努力,详细信息有点太多了。


def index/request/:
return render/'index.html', {'first_name': request.user.first_name, 'last_name': request.user.last_name}/


当然,将模板有用的一部分是渲染机制可以执行某些类型的逻辑和搜索。 事实上,我不需要清楚地继续看
first_name


last_name

- 我可以通过
request

作为我的上下文的一部分,并在模板中查看其属性:


index_showing_context.html
Hello, {{ request.user.first_name }} {{ request.user.last_name }}

def index_showing_context/request/:
return render/'index_showing_context.html', {'request': request}/


甚至过渡B.
request

在这个例子中不是严格必要的,因为其中一个差异
render


render_to_response

, 我上面提到的是那个
request

始终是上下文的一部分
render

. 但是,这是一个先进的主题。

因此,对于您的特定问题,如果您在演示文稿的上下文中向其显示并可视化正确的模式,则在模板中显示所需数据的位置无关紧要。 实际上,模板只是用于搜索和构建上下文将插入的字符串的文件的名称。

模板标记
{% include %}

- 这是将模式片段与其他模板混合的方法之一。 如果我想,我可以安排自己的照片:


header.html:
<head>
<title>This is a sample title.</title>
</head>

index.html:
<html>
{% include "header.html" %}
<body><p>This is my template body, {{ request.user.first_name }} {{ request.user.last_name }}.</p></body>
</html>

detail.html:
<html>
{% include "header.html" %}
<body><p>This is a detail page, probably for something selected in the context and given the context key 'object'.</p>
<p>{{ object }}</p>
</body>
</html>


它很棒,但这不是唯一的选择。 从你的问题来看,我看到你使用块并继承模式。 常见的成语是基本模板的定义,所有其他模板都将继承:


base.html
<html>
<head>
<title>{% block title %}Default title{% endblock %}</title>
{% block extra_head_elements %}{% endblock %}

<body>
{% block body_header %}Standard page header here {% endblock %}
{% block body_content %}{% endblock %}
{% block body_footer %}Standard page footer here {% endblock %}
</body>
</head></html>

index.html
{% extends "base.html" %}
{% block title %}index {% endblock %}
{% block body_content %}<p>This is my template body, {{ request.user.first_name }} {{ request.user.last_name }}.</p>{% endblock %}

detail.html
{% extends "base.html" %}
{% block title %}detail{% endblock %}
{% block body_content %}<p>This is a detail page, probably for something selected in the context and given the context key 'object'.</p>
<p>{{ object }}</p>
{% endblock %}


因此,最终,我不太确定如何最好地缝制右侧面板的概念,因为它取决于您希望如何工作的工作。 如果他到处都是或几乎无处不在,我都会建议将它放在将扩展的基本模板中 rest 你的模板。 如果您希望它完全在一个页面上,请刚刚在此模板上转动它。 如果您希望它在某些情况下,但不是在所有页面上,它可能会使用您可以的模板片段
{% include %}

.

主要是要了解模板的模式如何是您的标签
{% include %}


{% extends %}

, 并将必要的数据提供给演示文稿中模板的上下文。

编辑:
如果我想拥有几个观点和模板,它只是删除了类别,这是一种简单的方法,使用模型代码和渲染的示例来设置它。 还有其他选项。


index.html
<html>
<head><title>Simple category listing</title></head>
<body><p>The categories are:</p>
<ul>
{% for category in categories %}
<li><a href="{{ category.base_url }}" id="nav_font">{{ category.name }}</a></li>
{% endfor %}
</ul>
</body>
</html>

view:
def index/request/:
categories = categoryList.objects.all//
extra_context = {"categories": categories}
return render_to_response/"index.html", extra_context/


如果我想重新使用几页上的类别列表,那将恢复讨论
include

vs.
extends

更高。 在任何情况下,模板都将始终要求您的演示文稿传输到
categories

作为上下文变量。

江南孤鹜

赞同来自:

你可以
https://docs.djangoproject.com ... ectly
要删除视图中的类别并在模板中执行输出周期。 这需要申请。

如果您创建模型,可以使用 Django queryset api, 例如,这非常方便


mysite_categories = Category.objects.all//


它真的需要您创建一个应用程序。 但是,创建应用程序非常简单,只需使用命令
https://docs.djangoproject.com ... ation
.


python manage.py startapp myapp


创建应用程序后,您可以使用该命令
inspectdb

检查数据库并为表创建模型
mysite_categories

.

要回复问题请先登录注册