+ Django hands-on introductory tutorial 1- projects create real Pycharm

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/baidu_39459954/article/details/90231230

Foreword

Before reading this article best to have some basic python, Django is a Web framework written by Python, Python environment dependent, so the need to install Python interpreter in advance. About Python installation, not repeat them here.

Install the latest version of Python3, because from the beginning Django2.0 no longer supported Python2.7.
Conduct Python + Django Web development, the environment is the best IDE Pycharm, please install itself, it is recommended to use more than 2018 versions.

This paper environment is Win10 + Pycharm 2019.1.1 + python 3.7 + Django2.2

Course objectives

Through this tutorial, we will take you to create a basic poll application.
It consists of two parts:

  • Allow people to view and vote a public site.
  • A man you can add, modify and delete voting site management.

Project Creation

Open Pycharm and create a new project
Here Insert Picture Description
selection Django, write the name of the project mysite, pycharm will automatically create a virtual operating environment in which projects subordinate directory venv
Here Insert Picture Description
Here Insert Picture Description
project initial file structure:
Here Insert Picture Description
each file and directory explanation:

外层的mysite/目录与Django无关,只是你项目的容器,可以任意重命名。
manage.py:一个命令行工具,用于与Django进行不同方式的交互脚本,非常重要!
内层的mysite/目录是真正的项目文件包裹目录,它的名字是你引用内部文件的包名,例如:mysite.urls。
mysite/__init__.py:一个定义包的空文件。
mysite/settings.py:项目的主配置文件,非常重要!
mysite/urls.py:路由文件,所有的任务都是从这里开始分配,相当于Django驱动站点的内容表格,非常重要!
mysite/wsgi.py:一个基于WSGI的web服务器进入点,提供底层的网络通信功能,通常不用关心。

Running the Project

Click on the top right corner of the run button to run the initial project.
The buttons perform the following command:

"C:\Program Files\JetBrains\PyCharm 2019.1.1\bin\runnerw64.exe" E:\workspace\PycharmProjects\mysite\venv\Scripts\python.exe E:/workspace/PycharmProjects/mysite/manage.py runserver 127.0.0.1:8000

Here Insert Picture Description
Here Insert Picture Description

Note: Ignore the warnings about not using the latest database migration, database we deal with later.

你刚刚启动的是 Django 自带的用于开发的简易服务器,它是一个用纯 Python 写的轻量级的 Web 服务器。我们将这个服务器内置在 Django 中是为了让你能快速的开发出想要的东西,因为你不需要进行配置生产级别的服务器(比如 Apache)方面的工作,除非你已经准备好投入生产环境了。

现在是个提醒你的好时机:千万不要将这个服务器用于和生产环境相关的任何地方。这个服务器只是为了开发而设计的。(我们在 Web 框架方面是专家,在 Web 服务器方面并不是。

现在,服务器正在运行,浏览器访问 https://127.0.0.1:8000/。
你将会看到一个“祝贺”页面,随着一只火箭发射,服务器已经运行了。
Here Insert Picture Description

修改网站IP及端口号

如果你想修改IP和端口号,点击Pycharm右上角的Edit Configurations,进行配置修改
Here Insert Picture Description
Here Insert Picture Description

修改项目配置文件 mysite/settings.py
ALLOWED_HOSTS = []
修改为
ALLOWED_HOSTS = [‘192.168.88.200’] #只允许IP为192.168.88.200的机器访问我们的网站
或者ALLOWED_HOSTS = [’*’] # 允许所有人访问
Here Insert Picture Description

Otherwise they will encounter the following error:
Invalid HTTP_HOST header:.. '192.168.88.200' by You May need to the Add '192.168.88.200' to allowed_hosts
Here Insert Picture Description
attentive friends will find our website welcome interface is in English, Django provides a global configuration, only the project configuration file mysite / settings.py
in LANGUAGE_CODE can
we modify the following three parameters LANGUAGE_CODE, TIME_ZONE, USE_TZ

LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = False

Note:
LANGUAGE_CODE language
TIME_ZONE time zone
USE_TZ default is True, because Mysql storage time can not be set flexibly zone, unlike datetime object has an argument specifically designated time zone, so in order to unify the world of time, you must use the international standard time UTC, otherwise will be hell broke loose. Therefore, the time before stored in the database must be converted to UTC time. For example GMT 8:00, 0:00 stored into mysql (UTC).

Our application will not consider globalization, therefore the USE_TZ set to False, this time will be stored in the database and the current time consistent.
After the modification restart the service, you will see the familiar picture, I suggest you click on the link below to see the official Chinese documents and voting application Chinese tutorial .
Here Insert Picture Description

Next: + Django hands-entry application creates Pycharm combat tutorial 2-

Guess you like

Origin blog.csdn.net/baidu_39459954/article/details/90231230