django入门与实践 - 关于升级到django 3.7,三种模板超链接配置(编辑中)
第一种方法:
在myblog/urls.py模块中:
from django.contrib import admin
from django.urls import path, include urlpatterns = [
path('admin/', admin.site.urls),
path('blog1/', include(('blog1.urls', 'a'), namespace='blogg')), #'a'可以为任意字符,但不能为空
]
myblog/urls.py
在blog/urls.py模块中:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), # 这是路由模式
path('article/<int:article_id>', views.article_page, name='article_detai'), # path中的组名必须和参数名一致
]
blog/urls.py
在blog/templates/index.html模板中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
<a href="">添加新文章</a>
</h1>
{% for article in articles %}
<h2>
<a href="{% url 'blogg:article_detai' article.id %}">{{ article.title }}</a>
</h2>
{% endfor %}
</body>
</html>
blog/templates/index.html
在blog/view.py模块中:
from django.shortcuts import render
from blog1 import models # 方法:index()
# 参数:request:
# 功能:将Article表中的数据取出,在页面上显示所有文章
def index(request): articles = models.Article.objects.all()
return render(request, 'blog1/index.html', {'articles': articles}) # 方法:article_page()
# 参数:request:
# article_id:文章id,唯一标识符
# 功能:通过接收article_id,显示对应文章的详细内容
def article_page(request, article_id): article = models.Article.objects.get(pk=article_id)
return render(request, 'blog1/article_detail.html', {'article': article})
blog/view.py
第二种方法:
django入门与实践 - 关于升级到django 3.7,三种模板超链接配置(编辑中)的更多相关文章
- Django入门与实践 17-26章总结
Django入门与实践-第17章:保护视图 Django 有一个内置的视图装饰器 来避免它被未登录的用户访问: 现在如果用户没有登录,将被重定向到登录页面: 现在尝试登录,登录成功后,应用程序会跳转到 ...
- Django入门项目实践(下)
5.设置应用程序的样式 安装django-bootstrap3. # untitled/untitled/settings.py # ··· INSTALLED_APPS = [ 'django.co ...
- Django入门项目实践(中)
4.用户账户 4.1 让用户能够输入数据 添加新主题 # untitled/learning_logs/forms.py from django import forms from .models i ...
- Django入门项目实践(上)
项目结构 1.建立项目 File -->> New Project... 第一个Location是项目所在的目录,第二个Location是项目独立的Python运行环境,我们称之为Virt ...
- Django入门与实践-第14章:用户注册(完结)
http://127.0.0.1:8000/signup/ django-admin startapp accounts INSTALLED_APPS = [ 'accounts', ] # mypr ...
- Django入门与实践-第13章:表单处理(完结)
http://127.0.0.1:8000/boards/1/ http://127.0.0.1:8000/boards/2/ http://127.0.0.1:8000/boards/3/ http ...
- Django入门与实践-第24章:我的账户视图(完结)
http://127.0.0.1:8000/settings/account/ #好的,那么,这部分将是我们最后的一个视图.之后,我们将专心来改进现有功能. #accounts/views.py fr ...
- Django入门与实践-第23章:分页实现(完结)
http://127.0.0.1:8000/boards/1/ #从现在起,我们将在 board_topics 这个视图中来操作. python manage.py shell from django ...
- Django入门与实践-第22章:基于类的视图
http://127.0.0.1:8000/boards/1/topics/2/posts/2/edit/ http://127.0.0.1:8000/ #boards/views.py from d ...
随机推荐
- CSS怎么在项目里引入自定义字体(@font-face)
前言: 以前我一直用内置的默认字体给文字设置字体,直到一天UI妹纸给了我下面的字体 当时我是蒙蔽的,这个字体的效果如下 默认字体并无该字体,直接设置是没有效果的,这时就需要用到自定义字体了 下面 ...
- [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [Swift]LeetCode283. 移动零 | Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [Swift]LeetCode337. 打家劫舍 III | House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- Swagger2 添加HTTP head参数
大家使用swagger往往会和JWT一起使用,而一般使用jwt会将token放在head里,这样我们在使用swagger测试的时候并不方便,因为跨域问题它默认不能自定义head参数.然后自己去网上找, ...
- Linux自启动执行脚本方法
1. 在/etc/rc.d/init.d/下创建脚本,要遵守service script的标准: 例如: vi /etc/rc.d/init.d/gfs #!/bin/bash#case " ...
- Python内置函数(56)——set
英文文档: class set([iterable]) Return a new set object, optionally with elements taken from iterable. s ...
- 1K star+ 的项目是如何炼成的?
前言 首先标题党一下,其实这篇文章主要是记录我的第二个过 1K star 的项目 Java-Interview,顺便分享下其中的过程及经验. 需求选择 Java-Interview 之所以要做这个项目 ...
- Visual Studio 2017中使用SourceLink调试ASP.NET Core源码
背景 当我们在学习ASP.NET Core或者调试ASP.NET Core程序的时候,有时候需要调试底层代码,但是当我们在Visual Studio中调试程序的时候,由于一些基础库或者第三方库缺少pd ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...