Dowemo

I & tall and configure a django

I & tall the Django package using the pip package manager:

# pip install Django

After the Django package is installed, the django-admin tool is used to manage the django project.

Create a project

Create a new django project using the django-admin tool:

$ django-admin startproject [项目名称]

An empty django project basic file structure is as follows:

项目名称


├── 项目名称


│ ├── __init__.py


│ ├── settings.py # 项目设置


│ ├── urls.py # 路由设置


│ └── wsgi.py


└── manage.py # 项目的管理脚本


A project 's root directory is a project management script manage.py that can be used to complete the creation of applications, running projects, and so on.

A subdirectory of the project under the root directory of the project will also have a subdirectory with the same as the project name, which saves some configuration of the project.

Create application

Unlike traditional web development frameworks, Django uses App to divide the web site into multiple functional modules, and a project is composed of multiple App, and a App can be reused by multiple projects.

Also use the django-admin tool to create a App in the project directory:

$ django-admin startapp [应用名称]

You can also perform project management script manage.py to create a new App:

$./manage.py startapp [应用名称]

A newly created django application base file structure looks like this:

应用名称


├── admin.py


├── apps.py


├── __init__.py


├── migrations


│ └── __init__.py


├── models.py


├── tests.py


└── views.py


An application needs to add a reference name to the INSTALLED_APPS list in the project profile settings.py, as follows:

# file: [项目名称]/settings.py



INSTALLED_APPS = [


 'django.contrib.admin',


 'django.contrib.auth',


 'django.contrib.contenttypes',


 'django.contrib.sessions',


 'django.contrib.messages',


 'django.contrib.staticfiles',


 '[应用名称]'


]


Run project

Execute the project management script manager.py, and use the runserver parameter to run the project:

$. /manager. Py runserver.

By default, the project ru & on the 8000 port, using 127.0.0.1:8000 to access the running project, and then the runserver parameter can add port numbe & to specify the project run port:

$./manager.py runserver [端口号]

By default, projects launched with the application server that come with django can only be accessed by native computers ( unlike Tomcat, Jetty ).
If you need to make the project accessible to other machines in the intranet, use 0.0.0.0 as an ip when it's started:

$./manager.py runserver 0.0.0.0:[端口号]

At the same time, add the allowed host ip in the ALLOWED_HOSTS column table in the project profile settings.py, allowing all host access to be configured as follows:

# file: [项目名称]/settings.py



ALLOWED_HOSTS = [


 '*'


]


About Couldn't import Django... erro &

An Couldn't import Django... error is output when the ./manager.py runserver directive is executed, if the Django module is already installed correctly, you need to check the system default Python ve & ion.
If the system default uses Python with the Python version of Django, you should explicitly use the specified version of the Python interpreter to execute the manager.py script.

As an example of Python 3.6, the command is as follows:

sh
python3.6 manager.py runserver

Developing a intellij project in a idea

A Python is installed in IntelliJ IDEA for Django project development.

Create a project

Support for creating Django projects directly in IntelliJ IDEA, check Django support when creating Python projects.

Import project

Choose Project from Existing Sources... Option, select the root directory of the Djangp project that you created.

After performing the project import step, convert the Django project to a standard IDEA project.
Completing the imported Django project doesn't execute immediately, and you need to configure the Django project information in the following path:

File => Project Structure => Project Settings => Facets => Django

  • Django project root project root path
  • Configuration files for Settings projects ( 项目名称settings.py )
  • Manage script management script ( manage.py )

After you create the project 's run configuration ( Django server ), you can execute the project.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs