[Django] Django development notes (1) Setup and create apps
Setup in Pycharm Community Edition
Firstly, create a project and make sure to select the Virtualenv for the New environment if it is the first Django project.
Then, wait for the creation of the project. Once it is done, open the Terminal and command:
pip install django==the version of django you like
Go to the location of the project and delete main.py, and create a Django project using the terminal by commanding:
django-admin startproject yourProjectName
Then do the following rearrangement to the project files:
From the above one to the below one
Then go to Run/Edit Configurations, delete the main.py and add a new one called ‘Django’, and assign the script path and parameters.
Press the Run button on the right up corner, and the development server link will appear in the terminal. Now the Django project is ready to start.
The link will direct you to a web page like the image below.
Default Apps Installed
The apps can be found in the setting.py. Generally, the ‘Django.contrib.sessions’ is removed for better performance.
Create Own Apps
In the terminal, command the line below:
python manage.py startapp FirstApp
Then a new folder is created
Finally, add the new app to the Installed _Apps list.
Hello World
To develop a basic HelloWorld first is to write the content in the ‘views.py’ to create a basic message function to display on the screen. Then mapping it with the ‘urls.py’.
# in the FirstApp/views.py
from django.http import HttpResponse
def saysth(request):
return HttpResponse('Hello World!')
In the ‘urls.py’, here is to set up the path and function it’s going to call. Before set up the path to the page, go to the project’s ‘urls.py’, add ‘include’ just after the import path, and add the path of the app:
path('FirstApp/', include('FirstApp.urls'))
Now click on Play and the link will appear.
If you directly click the link, it will lead you to this:
Just type in ‘FirstApp/hello/’ after the ‘8000/’, then it should be fine.