LNMP Series website 0 Basic development record (II)

Source: Internet
Author: User

[Catalogue]
    1. A prelude to the development of a tear-blowing force
    2. Django Development environment Setup and configuration
    3. Bootstrap Front-end page development
    4. Django app Development
    5. Django Site Management
    6. Python Simple Crawler Development
    7. Nginx&uwsgi Server Configuration
    8. ...
Second, the Django development environment construction and configuration

1. Development environment

The development environment is still Windows, the Django IDE can be developed more, I have only used the following several

    1. Eclipse Free ide, need to download Pydev plugin, code hints function good
    2. Visual Studio needs to download PTVs, combined with the powerful editing features of VS, as C # code is easier to farm, but many of the build in function code does not prompt
    3. Pycharm ReSharper developer of the Python IDE, code hints are very powerful, especially for me this just learned python and used to the VS.

So, we did not hesitate to choose the pycharm.

Python selected 2.7.6, configure environment variables after installation, download Setuptools, run after decompression

x:\setuptools-dir> easy_install.py Install

After installation, at the same time configure add python-dir/scripts to environment variables, convenient to run the Easy_install command, but also can install PIP, two are installed Python package of the sharp weapon, the concrete what difference landlord also did not scrutiny, use the way are as follows

X:\> Easy_install Package-name
x:\> pip Install Package-name

And then you can install the back of the bag in a brain-free way, django-1.6.5.

X:\> Easy_install Django

Next is to install the MySQL5.6, all the way down.

So far, the development environment has been built. Since we are using a Linux version of the development tools, it is also very easy to switch platforms. Let ' s go!

 2. Project configuration

  SETP.1 Open Pycharm, new project, project named Errordev, project type select Django project, click OK

  SETP.2 The app name Errorappin the pop-up new app settings, the other remains the default, so a new Django site has been set up, and SHIFT+F10 can run the site now. Use 8000 port by default

At this point we can see that our project directory structure is as follows,

Errordev----Errorapp
--------__init.py
--------admin.py
--------models.py
--------views.py----Errordev
--------__init__.py
--------settings.py
--------urls.py
--------wsgi.py----Templates----manage.py

Specifically which file has what use not to explain it alone, look at the name to know, Django as there is a good MVC framework and. NET MVC Some differences, its views.py completed the controller function, and templates completed the function of the view.

The default Django templates is placed in the directory of each app, which guarantees the independence and reusability of each app, but is relatively fragmented and not conducive to maintenance, Pycharm provides templates at setp.2 time Folder option, you can change the default path, so that all the app's templates are all together. The settings.py can also be configured directly here

Define the base path at the top of the file (Pycharm is automatically defined)

Import= Os.path.dirname (Os.path.dirname (__file__))

Then modify the address of the templates, Django will be in precedence in the tuple search templates

Template_dirs = (    os.path.join (Base_dir,  'templates'),)

When using Pycharm, the default first app is automatically registered, and if the other IDE may need to register manually under

Installed_apps = (    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Errorapp',)

While configuring the MySQL connection, Django is a multi-database support, a key-value represents a database connection, the model layer using the USE keyword can switch the database, interested students can study under their own.

DATABASES = {    'default': {        'ENGINE':'Django.db.backends.mysql',        'NAME':'Errordev',        'USER':'Root',        'PASSWORD':'123456',        'HOST':'localhost',        'PORT':'3306',    }}

The Language_code is then modified to Chinese, which is used to provide a Chinese interface when using the site management feature.

' ZH-CN '

Finally is the most headache of the static file configuration , Google for a long time, a lot of seemingly useful methods do not know in the landlord why this useless, so, self-reliance, according to the official documents to each test, the main use of three projects

' /media/ '  '/media/'= (    "media" ),)

The first is Static_url, where the main point is to tell Django which paths need to be mapped to static files, but where they are mapped, they are defined by Staticfiles_dirs and can contain multiple absolute paths . Django searches all directories in turn until the mapping is complete. The premise of using these two configurations is the Installed_apps registration Django.contrib.staticfiles, which contains the module by default, and is only guaranteed in

DEBUG = True

Effective when the default Django development server is used in the DEV environment.

[The deployment here will be mentioned] usually PRD environment in the static files are configured in Apache or Nginx, at this time, this two configuration is no use, here will need to introduce static_root, its role at present I only in PRD environment, because the configuration of the static file mapping directly in the Aapche or nginx is completely no problem, but we use the Django site management function, the corresponding static files in the PRD environment will be found in the problem, here is configured STATIC_ After root, we use the shell command (press and hold ctrl+alt+r in Pycharm, enter collectstatic) to automatically collect all the static files into the Static_root directory. Includes static files that we add ourselves at the site and the static files provided by the Django itself management function, so that the problem of managing static files in the PRD environment cannot be solved.

Ps.
    1. Poor expression ability, later slowly updated, while [directory] will be with the new progress at any time to adjust, welcome everyone to shoot bricks!
    2. Site links are provided after the data has been accumulated for some time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.