我有一个Django项目,我有一个base.html,所有模板都从该继承。然后,在base.html中添加一个category.html文件,这只是我的导航栏。因此,我有一个名为Category的模型,我想将此模型传递给category.html以显示我的类别。我不知道该怎么办?
base.html:
<!DOCTYPE html>
{% load static %}
<html lang="en" style="height: 100%;">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>{% block title %}{% endblock %}</title>
<!-- Google Fonts -->
{% comment %} <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> <!-- Bootstrap core CSS --> {% endcomment %}
<link href="{% static 'fontawesome/css/all.css' %}" rel="stylesheet"> <!--load all styles -->
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
<!-- Material Design Bootstrap -->
<link href="{% static 'css/mdb.min.css' %}" rel="stylesheet">
<!-- Your custom styles (optional) -->
<link href="{% static 'css/style.min.css' %}" rel="stylesheet">
<!-- Mega menu style -->
<link href="{% static 'css/bs4megamenu.css' %}" rel="stylesheet">
{% block extra_head %}
{% endblock %}
{% block slideshow %}
{% endblock slideshow %}
</head>
<body>
<!-- Category Navbar -->
{% include 'category_navbar.html' %}
<!-- Category Navbar -->
<!-- Body Content -->
{% block content %}{% endblock %}
<!-- Body Content -->
<!-- Footer and scripts-->
{% include 'footer.html' %}
{% include 'scripts.html' %}
<!-- Footer and scripts-->
</body>
</html>
model.py:
class Category(models.Model):
category_name = models.CharField(max_length=20, default="general")
category_description = models.TextField()
category_picture = models.ImageField(upload_to="categories/images/", blank=True)
slug = models.SlugField()
parent = models.ForeignKey('self',blank=True, null=True ,related_name='children', on_delete=models.CASCADE)
class Meta:
unique_together = ('slug', 'parent',) #enforcing that there can not be two
verbose_name_plural = "categories"
def __str__(self):
full_path = [self.category_name]
k = self.parent
while k is not None:
full_path.append(k.category_name)
k = k.parent
return ' -> '.join(full_path[::-1])
def save(self, *args, **kwargs):
self.slug = slugify(self.category_name)
super(Category, self).save(*args, **kwargs)
答案 0 :(得分:1)
正如丹尼尔·罗斯曼(Daniel Roseman)所说,创建一个自定义模板标签或上下文处理器。
创建自定义模板标签时,有两种方法:
.SelectionChanged
中使用它。category_navbar.html
这是第二种方法的样子:
{% include 'category_navbar.html' %}
将其放置在名为例如的文件中您的一个应用的from django import template
register = template.Library()
@register.inclusion_tag('category_navbar.html', takes_context=True)
def category_navbar(parent_context):
context = {"categories": Category.objects.all()}
context.update(parent_context) # Only necessary if you need any other context variables in `category_navbar.html`
return context
目录中的nav_tags.py
。然后在您的基本模板中:
templatetags
答案 1 :(得分:0)
您可以使用上下文处理器: 在settings.py
duration_to_wait = 2 # in MINUTES
while True:
for index in df.index:
if df.loc[index,'Time flag'] == 1 and df.loc[df.index[index], "Last_email_sent"] == None:
send_email(index)
df.loc[df.index[index], "Last_email_sent"] = datetime.datetime.now()
elif df.loc[index, 'Time flag'] == 1 and datetime.datetime.now() - df.loc[df.index[index], "Last_email_sent"] < datetime.timedelta(minutes=duration_to_wait):
print('Wait')
print(datetime.datetime.now() - df.loc[df.index[index], "Last_email_sent"])
elif df.loc[index, 'Time flag'] == 1 and datetime.datetime.now() - df.loc[df.index[index], "Last_email_sent"] >= datetime.timedelta(minutes=duration_to_wait):
send_email(index)
df.loc[index, "Last_email_sent"] = datetime.datetime.now()
else:
print('false')
time.sleep(10)
创建一个名为TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'myproject.context_processors.get_categories',
],
},
},
]
的文件(在这种情况下,位于context_processors.py
目录中)
myproject
现在,您可以在所有模板中访问def get_categories(request):
return {'categories': Category.objects.all()}
。
categories