Create and use django project in pycharm

Posted May 27, 20202 min read

Create django project in pycharm


After the creation is complete, we can see if the django project can be started
Enter the command in Terminal
python runserver
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



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

There are no mysql related modules, so starting django will report an error, install the mysqlclient library.

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

Configure the path in settings to make it easier for us to call between file modules in the future

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>')
    for guy in sample(guys, 3):
        content.write(f '\ t <li> {guy} </li>')
    return HttpResponse(content.getvalue())

Configure the mapping of this function in urls
Then click on the local URL
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

Put a data list in the view, use render to return the data to display the data

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