The opening

Learn Python also have a period of time, always want to do something, it’s time to do things slowly, see a lot of data and blog, also decided to personally make a slowly, before learn always feel to note down the free summary together, found that the effect is not good, today just to create a new project, to start from scratch to do while on the recent study to summarize.

To prepare

PyCharm editor Django 1.8.2 PyCharm editor PyCharm editor Django 1.8.2 PyCharm editor PyCharm editor

Create projects and apps

Open PyCharm to create a project as shown below





Create a project





Set project name

The directory structure is as follows





Directory structure.png

Wsgi, the container for the Blog project.pyThe Python server gateway is the interface urls between python applications and web servers.pyUrl configuration files in Django projects require us to configure their addresses to link to setting.pyThe total configuration file for the project contains the database Web and other configuration BASE_DIR = OS.path.dirname(os.path.dirname(os.path.abspath(__file__)) The root directory of the project SECRET_KEY ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$95jg0m@'Create a security code generated by the project DEBUG = True for debugging purposes but in actual projects do not open ALLOWED_HOSTS = [] Only allow the url added here to access the configuration STATIC_URL = of the TEMPLATES page'/static/'Static file address DATABASES Sets the data to create an applicationarticleJoin the setting.pyIn the INSTALLED_APPSCopy the code

Create a new APP before we type it

python manage.py startapp articleCopy the code

After the command, you can see the related files added to the PyCharm directory as shown below





To create the APP. PNG

In the figure above

Article Migrations data migration module automatically generates admin.pyBackground configuration file apps of the application.pyApply some django configurations1.9Models will be generated automatically later.pyData modules use the ORM framework similar to MVC's Model module Tests.pyAutomatic test module View.pyThe main code editing area in the logical code module project that executes the responseCopy the code

Now we need to add the new APP to setting.py and run the project as shown below

python manage.py runserver   # Start the development server in DjangoCopy the code

We can also set our own port number

runserver 8002If a port is used, you can use another port:python manage.py runserver 8001
python manage.py runserver 9999Copy the code




Add a project startup server

Put the url on the left command line into your browser and see it running





Effect of the web

In fact, our commands to create projects and apps can be found on the command line

 django-admin.pyStartprojec tXXXX # Create project Python Manage.pyStartapp XXXX # Create app Python manage.pyRunserver # Starts the development server in DjangoCopy the code

We create the APP based on the project manage.py and so on, so let me take a look at manage.py and all the other things you can do when you type in terminal you can see its other commands

python manage.pyCopy the code




Manafe.py features 01.png





Manafe.py features 02.png

Creating a database

Set the corresponding database field in models.py

python manage.py makemigrations
python manage.pyMigrate # Command line Run this commandCopy the code

The running results are as follows:





Built the data table

Then we open the database and see that our newly added Article table already exists as shown below





Database list display

In the software we can do a series of operations on the database, let’s add some data, and then go to the background to look at them and add some data





Adding data Information

The back end of a website

As a website, we need to log in and upload the content we want from the background, rather than every time we use the database. Django has a very powerful background that allows us to use it with simple processing. We can then view from the background what we just manually added to the database when we created the project. The default configuration of connecting to the background is already set, but I need to associate our database with it, otherwise we will enter the background and not display our article/admin.py data set as follows

# import models
from article.models import Article

# Register your models here.
admin.site.register(Article)Copy the code

All we need to do is create a super user and execute the command to open the background

python manage.py createsuperuserCopy the code

Enter the account, email and password as shown below (the username cannot be the same as the email and the password should not be too simple to allow) and then run the background server

python manage.py runserverCopy the code

Type in the address bar: http://127.0.0.1:8000/admin





Create a superuser and open the background

You can go into the background and see what we’ve created





Into the background

Click on the Article we created to see the Article information we added manually





Specific article information

You can see that there are still many bugs, don’t hurry step by step, first of all when the English background, the rhythm is not right ah, first change to Chinese hey hey. LANGUAGE_CODE = ‘en-us’ in setting.py is changed to Chinese LANGUAGE_CODE = ‘zh_Hans’ in new Versions of Django, the values of ‘zh_Hans’ and ‘zh_Hant’ are retained Then refresh the page to see the changed page.





Site Chinese modification

Isn’t that much better? Ha ha. Of course, if we want to beautify the background interface, we can introduce bootstrap and use the framework to beautify the background display. Django-admin-bootstrap uses PIP installation

pip install bootstrap-adminCopy the code

Then modify INSTALLED_APPS in setting.py to add ‘bootstrap_admin’ and run the background server and refresh the screen





Bootstrap_admin optimizes the interface

Uh, uh, it’s even uglier, so let’s show it the first time, finish the general logic first, and leave a hole here. So far, there are several things to be optimized here. First, let me mention and record. One is the optimization of the style, uploading the article in the background. The name of the background article is in models.py if python3

 def __str__(self):
        return self.titleCopy the code

Modified display effect





Show article name

Afterword.

Today’s content is mainly to create the project and create the relevant database, and modify the content, and then in the background data display, and modify the effect of the background display, the next article is mainly to write about the front-end page display and interactive logic, but I am not familiar with, there are more headache HTML writing method, It’s time to go out and find a bunch of front-end templates that have been written on the web and learn about them. Let’s stop here today, May Day, the end, tomorrow to go to the company to catch up on the project…