2017年9月8日 星期五

[Django] Hello world

 Python    Django  


Introduction


Django is a free and open-source web framework for Python.
We are going to install it and create a Hello world with Django.

Environment


Python 3.6.2
Django 1.11.5





Implement



Install Django

Before installing Django, let’s create a virtual environment with venv.

$ mkdir Django
$ cd Django
$ python -m venv venv/ShopCart
$ cd venv/ShopCart
$ scripts\activate.bat


Then, lets install Django into the virtual environment.

$ python -m pip install Django



Create a new Django project

$ django-admin startproject shopcart


The project’s default files are as following.






__init__.py

This file maks Python treat the directory which has it as a module. It's the first file being loaded in a module, so we can run some codes when it's loaded.

settings.py

See the details on settings.py
For example, SECRET_KEY = 'xxxxxxxxxxxxxxxxxxx' is described as following...





urls.py

We can define Url Patterns in this file.

wsgi.py


An entry-point for WSGI-compatible web servers to serve your project. For more information on WSGI(Web Server Gateway Interface), see WIKI.



Start it

Use the following command to start the web server.

$ python shopcart\manage.py runserver











Github





Reference




沒有留言:

張貼留言