Django学习笔记1

What is Django?

Django is a web framework which encourages rapid development and clean and pragmatic design.

Start with Django→ →

1. download django package

I encountered a trouble when using pip3 install django in the terminal. It left an error saying that read time out. I've tried so many times but it just got this error.No idea why???

Then I looked into the django website and followed the tutorial using python -m pip install Django. It worked! Try import django. Noooo error. Yeah!

 

2. create a project and app

Cuz I'm not a professional python programmer and I don't have different version packages to work with, I skip the step of creating the virtual environment. 

Then work with the terminal!!! 

By coding django-admin startproject mysite, we can create a project with all the files we need. It's a great leisure that we will not bother to create those troubling files.  

Django学习笔记1

I'm so confused with all these files until now. But I think it's ok when u only have little concept about those things. Our goal is to set a web, so those things are all set for it. Utter nonsense, right? Neglect them at this stage anyway. Catch the bus, at least not be stuck here.

We can use the manage.py inside to setup what the project need. An app!!! Right!!! python manage.py startapp polls will create an APP named polls. But wait, let's do some verification of the server before the execution. As we now, if we want to visit a web, we need to send the request via the browser to the server. But our django is for web framework so the server will be set in advance that we can focus on framework. Let's test the server by running python manage.py runserver. Congratulations! A localhost:8000 is for us!!!

Django学习笔记1

 When we create an app, we have a directory like this:

Django学习笔记1

Django学习笔记1

Still neglect it for the god's sake.

 

3. enjoy request and response

Let's pull our thoughts together. By entering the URL(Uniform Resource Locator) on the browser, the http client will send request to the http server(follow the http protocol). The server will retrieve the corresponding url and response the content of the url on the browser. At this point, we should set urls.py in the app directory so that our app can be retrieved. We've noticed that in the project directory, we also have a urls.py. Why do we just use this one since the developers didn't set the urls.py for us in advance? That's because we can have many apps in a project, and our apps can be used in different projects. It will be muchhhh more important if we have urls.py in each app. 

Before we operate on the app. Let's look into the urls.py in the project. 

Django学习笔记1

It already has one site. Go into the localhost:8000/admin/, we will see an administration page for the administrator. But we will need a username and password to get in. Just leave it there.

Django学习笔记1

So now we know how the url goes. Go for the app Ya~!!!

The content we want to show on the web page is written in views.py, use the HttpResponse class to return everything. The easiest way is to return the text type directly. And then let's link up views.py with urls.py. Cuz it's in the root directory of the app, the route can be empty. If you add a route like 'noki', then the url will be like localhost:8000/noki/noki/. Last link the app/urls.py with project/urls.py by using the include function. They are all shown below. (It has a strange problem. While I have created the app link, the localhost:8000 is invalid, poping out the 404 error. Why???? )

Django学习笔记1

Django学习笔记1

Django学习笔记1 

Django学习笔记1

 

Django学习笔记1

 

Here comes the outcome~

Django学习笔记1 

Seems like we've successed a great step,right? Since we've create a easy website!!!

But be careful, we have to manipulate the database and the page design!!!

Create a website is not a easy thing /Ö」

 

(ps: writing in English to practice English hiahia)

 

上一篇:Django路由转发


下一篇:netcore2.2升级到3.1发布端口启动不生效