Django - How to Create a Hello World Web Project

        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.

  1. open your project folder
  2. find file named setting and add <appname> into list of INSTALLED_APPS

        And then we need to let app display something

  1. Open the app folder and find view.py
  2. Add a function in view.py (returns a http response)
  3. Create urls.py and add a varibale called urlpatterns of type list
  4. Add an element into urlpatterns: path("", views.<function name>) ["" means nothing(default)]
  5. Add a name of the path (a parmeter of function path) [name= '<...>']
  6. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值