我正在学习Django,我添加了一个新应用,
链接可以显示在friendly_link.html中,
代码如下:
from django.contrib import admin
from .models import FriendlyLink
admin.site.register(FriendlyLink)
from django.db import models
class FriendlyLink(models.Model):
title = models.CharField(max_length=100, default='', verbose_name='title')
url = models.CharField(max_length=100, default='', verbose_name='url')
class Meta:
verbose_name_plural = 'links'
def __str__(self):
return self.title
from django.shortcuts import render
from .models import FriendlyLink
def friendly_link(request):
friendly_link = FriendlyLink.objects.order_by('title')
context = {'friendly_link': friendly_link}
return render(request, 'friendly_link.html', context)
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^links$', views.friendly_link, name='friendly_link'),
]
{% extends "base.html" %}
{% block title %}Links{% endblock %}
{% block content %}
<ul>
{% for link in friendly_link %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% empty %}
{% endfor %}
</ul>
{% endblock %}
{% load i18n static %}<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
...
{% block content %}{% endblock %}
<footer>
<ul>
<!--This code doesn't work-->
{% for link in friendly_link %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% empty %}
{% endfor %}
</ul>
</footer>
...
</body>
我想将链接放在base.html中的<footer>
元素内。如何更改代码?谢谢。
答案 0 :(得分:0)
不确定我是否正确回答了这个问题。您是否要将链接附加到现有的base.html而不是覆盖它?
{% block content %}
{{ block.super }}
...
{% endblock %}
您也可以使用{% include "base.html" %}
代替{% extends "base.html" %}
如果您只想将链接直观地粘贴到页面底部,则可以简单地将CSS与带有stylesheets
或extra_head
块的position属性一起使用