我的 MySQL 数据库中有一个名为 mysite_categories 的表,有 4 列,但出于我的目的,我只需要两个(名称,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 循环中使用它。就像是:
{% 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。我打算删除它并重新开始,但我想我不妨张贴它看看我可能哪里出错了?
视图.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)
模型.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'
索引.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)
类别列表/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
类别列表/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'
类别列表/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
我的网站/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 %} ”而不是类别?我尝试将包含更改为指向“类别/ ”,这在浏览器中有效,但它告诉我找不到模板?
我现在好难过..?