Before the Poe test, I learned how to use Django to establish a web program. However, I forgot everything I learned...... So, it is the reason that I wrote this blog to help you and me to memorise how to use Django.
TYPE beginner
MasterOfDjango: BOOLEAN
Errors: INTEGER
ENDTYPE
DECLARE Author: beginner
Author.MasterOfDjango <-- FALSE
Author.Errors <-- RAND(999)
OUTPUT "This blog is only suitable for the people who first touch with django"
1. Start project
The all you need is the name of the project and insert the name into the command. (sounds like easy)
command: django-admin startproject <projectname>
tip: command shoud be typed in your terminal or command prompt
If you use command "code ." to figure out what happened, you will see serveral files has been created in the path of your project.
- review: Project folder
- db.sqlite3:It is a database document
- manage.py : A python file with important operations you will use (you may not open it)
If you want to have a look of your webpage now, you can use a breif command below and open the corresponding URL.
command: python manage.py runserver
The last picture maens you successfully created the project and th project runs well.
However, it doesn't meet your requirement. You want to your webpage have various function and unique apperance. Don't rush! I will teach you step by step.
2. Start application
To make your webpage more intersting, you need some apps. Just like create a new project, you can use a simple command to acheive that. Have you remembered manage.py? Now it does work!
command: python manage.py startapp <appname>
Then you need to tell the project you add a applicationa and you want it appears in your web project.
- open your project folder
- find file named setting and add <appname> into list of INSTALLED_APPS
And then we need to let app display something
- Open the app folder and find view.py
- Add a function in view.py (returns a http response)
- Create urls.py and add a varibale called urlpatterns of type list
- Add an element into urlpatterns: path("", views.<function name>) ["" means nothing(default)]
- Add a name of the path (a parmeter of function path) [name= '<...>']
- Remeber to import the library at first (from . import views)
In addition, if you are a master of html and you may wonder how to return a html file. (If you really a master of html, this article will be meanningless to you)
The render function has been imported for you (how nice django is!) just use the function render and the parameters should be request, and the path of template.
def greeting(request, name):
return render(request, '<application name>/greeting.html')
You should be aware of one thing. The acutal path should be:
<application name>/template/<application name>/greeting.html
The html in django also provide varible function.
def greeting(request, name):
return render(request, "<application name>/greeting.html", {"name":name})
This function can make your html dynamic instead of static.
What you should do next is to tell the project where your application is. If you are a scrupulous person, it is obvious that a file named urls.py in your porject folder (not the apllication one)
"URLS are kind of interesting thing in my opinion. It just like a tree that could have a variety of branches from the trunk (urls.py in project folder) to each branches in applications. URL stands for univesal resource locator. Like its name, it is used to location where the html is or the response it should give."
Open it, a list of urlpatterns is shown in front of you.
And add the path of urls.py you have established in application folder . The format is
urlpatterns = [......, path("<app anme>/", include("<app name.urls>"))]
If you want your urls path more interesting you can pass through a varibale into the function you defined in views. In the urlpatterns in application folder, you need to add a element like this:
path("<str:name>", views.<function name>)
Correspondingly, there should be a parameter in a function in views called name or whatever the name of parameter is.