5.设置应用程序的样式

安装django-bootstrap3

# untitled/untitled/settings.py
# ··· INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles', # 第三方应用程序
'bootstrap3', # 我的应用程序
'learning_logs',
'users'
] # ··· BOOTSTRAP3 = {
'include_jquery' : True
}
<!-- untitled/templates/learning_logs/base.html -->
{% load bootstrap3 %} <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"> <title>Learning Log</title> {% bootstrap_css %}
{% bootstrap_javascript %} </head> <body> <!-- Static navbar -->
<nav class="navbar navbar-default navbar-static-top">
<div class="container"> <div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
</button>
<a class="navbar-brand" href="{% url 'learning_logs:index' %}">
Learning Log</a>
</div> <div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="{% url 'learning_logs:topics' %}">Topics</a></li>
</ul> <ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li><a>Hello, {{ user.username }}.</a></li>
<li><a href="{% url 'users:logout' %}">log out</a></li>
{% else %}
<li><a href="{% url 'users:register' %}">register</a></li>
<li><a href="{% url 'users:login' %}">log in</a></li>
{% endif %}
</ul>
</div><!--/.nav-collapse --> </div>
</nav> <div class="container"> <div class="page-header">
{% block header %}{% endblock %}
</div>
<div>
{% block content %}{% endblock %}
</div> </div> <!-- /container --> </body>
</html>

使用jumbotron设置主页的样式。

<!-- untitled/templates/learning_logs/index.html -->
{% extends "learning_logs/base.html" %} {% block header %}
<div class='jumbotron'>
<h1>Track your learning.</h1>
</div>
{% endblock %} {% block content %}
<h2>
<a href="{% url 'users:register' %}">Register an account</a> to make
your own Learning Log, and list the topics you're learning about.
</h2>
<h2>
Whenever you learn something new about a topic, make an entry
summarizing what you've learned.
</h2>
{% endblock %}

6.部署应用程序到Heroku

我们将项目部署到Heroku的服务器并对其进行管理。

  1. 首先需要到官网https://dashboard.heroku.com注册账号。
  2. 接着安装Heroku客户端,请访问https://devcenter.heroku.com/articles/heroku-cli

注意:访问Heroku官网需要VPN,注册账号需要用国外的邮箱,我试过outlook邮箱可以注册。

6.1 安装必要的包

PyCharmTerminal窗口逐个执行以下几个命令:

  • pip install dj-database-url
  • pip install dj-static
  • pip install static3
  • pip install gunicorn

6.2 创建requirements.txt

执行pip freeze > requirements.txt命令,将项目当前安装的所有包的名称都写入到文件requirements.txt中。

dj-database-url==0.5.0
dj-static==0.0.6
Django==2.1.7
django-bootstrap3==11.0.0
gunicorn==19.9.0
pytz==2018.9
static3==0.7.0
psycopg2>=2.6.1

我们在包列表中添加psycopg2,它帮助Heroku管理活动数据库。

6.3 指定Python版本

runtime.txt

python-3.6.3

6.4 修改settings.py

# untitled/untitled/settings.py

# ···

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# Heroku settings
if os.getcwd() == '/app':
import dj_database_url DATABASES = {
'default': dj_database_url.config(default='postgres://localhost')
} # Honor the 'X-Forwarded-Proto' header for request.is_secure().
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') # Only allow heroku to host the project.
ALLOWED_HOSTS = ['learning-log-final.herokuapp.com']
DEBUG = True # Static asset configuration
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

注意STATIC_ROOT = os.path.join(BASE_DIR, 'static')这行代码,它写在if测试外面。

6.5 创建启动进程的Procfile

Procfile

web: gunicorn learning_log.wsgi --log-file -

6.6 修改wsgi.py

# untitled/untitled/wsgi.py
import os from dj_static import Cling
from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'untitled.settings') application = Cling(get_wsgi_application())

6.7 创建用于存储静态文件的目录

untitled/untitled/static/placeholder.txt

This file ensures that learning_log/static/ will be added to the project.
Django will collect static files and place them in learning_log/static/.

6.8 使用Git跟踪项目文件

我们无需让Git跟踪项目中的每个文件,因此将让Git忽略一些文件。

.gitignore

venv/
__pycache__/
*.sqlite3

提交项目



推送到Heroku

项目源码地址:https://github.com/gorgeous-h/learning_log.git

注意:这个项目是在Windows环境下做的。

参考资料:《Python编程从入门到实践》—【美】Eric Matthes 著

Django入门项目实践(下)的更多相关文章

  1. Django入门项目实践(上)

    项目结构 1.建立项目 File -->> New Project... 第一个Location是项目所在的目录,第二个Location是项目独立的Python运行环境,我们称之为Virt ...

  2. Django入门项目实践(中)

    4.用户账户 4.1 让用户能够输入数据 添加新主题 # untitled/learning_logs/forms.py from django import forms from .models i ...

  3. Django入门与实践 17-26章总结

    Django入门与实践-第17章:保护视图 Django 有一个内置的视图装饰器 来避免它被未登录的用户访问: 现在如果用户没有登录,将被重定向到登录页面: 现在尝试登录,登录成功后,应用程序会跳转到 ...

  4. Django 入门项目案例开发(上)

    关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. Django 入门案例开发(中) http://www.cnblogs.com/focusBI ...

  5. Django入门与实践

    安装: 1.https://www.djangoproject.com/查找最新版本 2.pip install Django==1.10.6安装Django   创建项目: 1.打开命令行,进入想要 ...

  6. django入门与实践(开)

    1.什么是Django? 基于python的高级web开发框架 高效 快速 免费 开源 正常上网流程 浏览器浏览网页的基本原理 请求响应过程 开发环境搭建 Python Django pip inst ...

  7. Django 入门项目案例开发(下)——创建项目应用及模型类

    关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. 前面两章是在已经开发好的项目上用来描述环境和业务,这一章创建一个全新的项目来用作开发,你可以跟 ...

  8. 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 ...

  9. Django入门与实践 1-16章总结

    注意事项:随时备份.随时记录.从宏观到微观 不闻不若闻之,闻之不若见之,见之不若知之,知之不若行之:学至于行之止矣 安装 Python 3.6.2 pip install django==1.11.4 ...

随机推荐

  1. python基础2之字符串、列表、字典、集合

    内容概要: 一.python2 or 3 二.字符串拼接 三.字符串 四.列表.元祖 五.字典 六.集合 七.练习 一.python2 or python3 目前大多使用python2.7,随着时间的 ...

  2. face_recognition环境配置及命令行工具测试

    由于某种不可抗力(又是它!)我写了这篇博客,主要目的是记录. face_recognition是啥子? face_recognition号称世界上最简单的人脸识别库,可使用 Python 和命令行进行 ...

  3. 20155338《网络对抗》Exp3 免杀原理与实践

    20155338<网络对抗>Exp3 免杀原理与实践 实验过程 一.免杀效果参考基准 Kali使用上次实验msfvenom产生后门的可执行文件,上传到老师提供的网址http://www.v ...

  4. 通用shellcode

    所有 win_32 程序都会加载 ntdll.dll 和 kernel32.dll 这两个最基础的动态链接库.如果想要 在 win_32 平台下定位 kernel32.dll 中的 API 地址,可以 ...

  5. Postman安装与入门使用

    Postman官方下载地址:https://www.getpostman.com/apps Postman 是一个很强大的 API调试.Http请求的工具.我们可以用来很方便的模拟get或者post或 ...

  6. PAT-1003 Emergency(Dijkstra)

    1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...

  7. Git多人协作工作流程

    前言 之前一直把Git当做个人版本控制的工具使用,现在由于工作需要,需要多人协作维护文档,所以去简单了解了下Git多人协作的工作流程,发现还真的很多讲解的,而且大神也已经讲解得很清楚了,这里就做一个简 ...

  8. 全局最小割StoerWagner算法详解

    前言 StoerWagner算法是一个找出无向图全局最小割的算法,本文需要读者有一定的图论基础. 本文大部分内容与词汇来自参考文献(英文,需***),用兴趣的可以去读一下文献. 概念 无向图的割:有无 ...

  9. 【SE】Week2 : 个人博客作业

    1. 是否需要有代码规范 对于是否需要有代码规范,请考虑下列论点并反驳/支持: Statement1 :  这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 这样的 ...

  10. 四则运算APP,团队项目之需求

    队名:IG.Super 成员:范铭祥,曾威,刘恒,黄伟俊. 一.程序功能需求 程序可以出带括号的正整数四则运算,支持分数,除法保留两位小数,如:(1/3+1)*2 = 2.67,特别注意:这里是2.6 ...