我将我的网站从PHP转移到Django,现在我必须制作侧边栏。 我希望条目来自数据库,而不是我希望它们成为其他页面的超链接。 我怎么能这样做?
<nav class="menu" id="theMenu">
<div class="menu-wrap" data-spy="scroll">
<h1 class="logo"><a href="index.php">MY BOOKS</a></h1>
<i class="icon-remove menu-close"></i>
{% for question in latest_question_list %}
<a href="{% url 'detail' slug=question.slug %}">{{ question.naslov }}</a>
{% endfor %}
</div>
<div id="menuToggle"><i class="icon-reorder"></i></div>
</nav>
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="/static/font.min.css" rel="stylesheet">
<link href="/static/bootstrap.min.css" rel="stylesheet">
<link href="/static/font-awesome.min.css "rel="stylesheet">
<link href="/static/main.css" rel="stylesheet">
</head>
<body data-spy="scroll" data-offset="0" data-target="#theMenu">
{% include "nav.html" %}
{% include "header2.html" %}
{% block h1 %}{% endblock %}
<script src="/static/jquery.js"></script>
<script src="/static/bootstrap.min.js"></script>
<script src="/static/jquery.isotope.min.js"></script>
<script src="/static/jquery.prettyPhoto.js"></script>
<script src="/static/main2.js"></script>
</body>
</html>
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all()
context = {'latest_question_list': latest_question_list}
return render(request, 'papers/index.html', context)
def detail(request, slug):
question = Question.objects.get(slug=slug)
return render(request, 'papers/detail.html', {'question': question})
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'papers.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<slug>[\w_-]+)/$', 'papers.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
{% extends "master2.html" %}
{% block h1 %}
<div id="g">
<div class="container">
<div class="row">
<h3>{{ question.naslov }}</h3>
<br>
<br>
<div class="col-lg-6 desc "><p>{{ question.opsirnije_text }}</p></div>
<div class="col-lg-4 desc desc-b">
<p>{{ question.opsirnije_text }}</p>
</div>
</div>
</div>
</div>
{% endblock %}
{% block title %} Detail {% endblock %}
答案 0 :(得分:0)
您可能正在寻找for
template tag。
{% for question in latest_question_list %}
<a href="{% url 'detail' slug=question.slug %}">{{ question.naslov }}</a>
{% endfor %}
现在,如果您希望问题列表显示在所有(或几乎所有)页面上,您可以编写一个template context processor,将它们添加到所有上下文中。