Create and use django project in pycharm

Posted May 27, 20202 min read

Create django project in pycharm

6-20.png

After the creation is complete, we can see if the django project can be started
Enter the command in Terminal
python manage.py runserver
Snipaste_2020-05-27_10-19-48.png
You can see the interface to prove that the startup is successful Snipaste_2020-05-27_10-20-40.png

You will definitely use the database during the project
So we need to enter settings to change the configuration of the database

DATABASES = {
    'default':{
        'ENGINE':'django.db.backends.mysql',
        'NAME':"django_test",
        'USER':"root",
        'PASSWORD':"mysql",
        'HOST':"127.0.0.1",
        'POST':'3306',
        'OPTIONS':{'charset':'utf8mb4'},
        'ATOMIC_REQUESTS':True
    }
}

Snipaste_2020-05-27_10-32-24.png

In this way, the project can use the local database
The premise is to have this database, user name, password unified

Use Navicat Premium to create a django \ _test database
7-23.png

There are no mysql related modules, so starting django will report an error, install the mysqlclient library.
05-27_10-41-29.png

Then start django to see if an error will be reported. If no error is reported, the database can be run.

Then create several folders to store related content
-21.png

Configure the path in settings to make it easier for us to call between file modules in the future
Snipaste_2020-05-27_10-56-36.png

Write a small demo to play
Write a function in the views view of the user file

def index(request):
    guys = ['Guo Fucheng', 'Liu Dehua', 'Dawn', 'Jiang Long', 'Li Lianjie', 'Yang Ziqiong', 'Zhao Benshan']
    content = StringIO()
    content.write('<h2> List of handsome guys </h2>')
    content.write('<hr>')
    content.write('<ul>')
    for guy in sample(guys, 3):
        content.write(f '\ t <li> {guy} </li>')
    content.write('</ul>')
    return HttpResponse(content.getvalue())

Configure the mapping of this function in urls
Snipaste_2020-05-27_11-23-37.png
Then click on the local URL
Snipaste_2020-05-27_11-24-07.png
Click refresh to display different data

You can also use the html page to display more convenient and beautiful

Create an html file index.html in templates
Compile with django's template syntax
-07.png

Put a data list in the view, use render to return the data to display the data
Snipaste_2020-05-27_11-37-34.png

Then click on the local address, the effect is the same.
Using template syntax effect efficiency will be faster.