Django----A tutorial on making a simple Web page (for beginners)

Source: Internet
Author: User

This text is suitable for just learning Django classmate, if the comparison is not done to see.

The following are the deployment scenarios on Windows;

Get ready:

1, python3.6

2, Pycharm Profession (Professional Edition)

3. Installing the Django Module

The above installation will not speak, relatively simple, there are many tutorials on the web. All go to the official website to download the installation.

Objective:

Learning the Django framework is actually learning its file directory, there are some necessary modules and packages, of course, you can also create your own directories or modules and packages.

* * We open pycharm to create a new Django project (can also be created directly under terminal with instructions django-admin startproject XXX)

File---New project**

Built well will automatically generate a directory framework. So to understand the role of the file in this framework.

II. Preparation of business logic

After you create a successful project, the views.py file is empty and you need to write your own business logic.

from django.shortcuts import HttpResponse #导入HttpResponse模块def index(request):#request是必须带的实例。类似class下方法必须带self一样    return HttpResponse("Hello World!!")#通过HttpResponse模块直接返回字符串到前端页面

Third, configure URL routing

from laomomo import views#导入views模块from django.conf.urls import urlurlpatterns=[    url(r‘^index/‘,views.index)#配置当访问index/时去调用views下的index方法]

Iv. running server and accessing

Terminal executes the Python manage.py runserver so the default path is 127.0.0.1:8080

Specify the port or address and write it back, such as: Python manage.py runserver 127.0.0.1:8888

Then the browser accesses http://127.0.0.1:8888

Here's Hello world!! The index method is returned to the front end of the views.py file by HttpResponse.

The question comes, so it feels a little bit simple, if you want to show some data? What to do? Then we need to write the HTML file to carry it.

Five, modify the index method in views.py as follows

from django.shortcuts import render#导入render模块#先定义一个数据列表,当然后面熟了可以从数据库里取出来list = [{"name":‘good‘,‘password‘:‘python‘},{‘name‘:‘learning‘,‘password‘:‘django‘}]def index(request):    return render(request,‘index.html‘,{‘form‘:list})#通过render模块把index.html这个文件返回到前端,并且返回给了前端一个变量form,在写html时可以调用这个form来展示list里的内容

Vi. Editing HTML files

After the new successful project templates file directory is empty, we need to create a new HTML file in this directory to display the content to the front

<!DOCTYPE html>

Use {{}} when you want to write an if or for statement in HTML, and use {{}} brackets when calling a variable

We rerun the server and the browser should show the data in the list.

Well, here we can show the background set of data to the front end. It doesn't seem to be very friendly either. Let's do a front end. You can enter a user name and password, and then display the user and password you entered.

Seven, rewrite the HTML file

<! DOCTYPE html>

Here add the user name and password of the input box and a Submit button, set the method of test post, below we need to write the index method under views.py to get the post data, and then return to the front-end display.

Eight, modify the index method under views.py:

from django.shortcuts import render#导入render模块#先定义一个数据列表,当然后面熟了可以从数据库里取出来list = [{"name":‘good‘,‘password‘:‘python‘},{‘name‘:‘learning‘,‘password‘:‘django‘}]def index(request): #获取前端post过来的用户名和密码 name = request.POST.get(‘name‘,None) password = request.POST.get(‘password‘,None) #把用户和密码组装成字典 data = {‘name‘:name,‘password‘:password} list.append(data) return render(request,‘index.html‘,{‘form‘:list})#通过render模块把index.html这个文件返回到前端,并且返回给了前端一个变量form,在写html时可以调用这个form来展示list里的内容

Running the server again through the browser will have the effect of presenting the data in real time.

Here we have completed a simple Web page production process, there are questions to ask me.

Django----A tutorial on making a simple Web page (for beginners)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.