Django project 1

Source: Internet
Author: User

Preparation [plain] django-admin.py startproject web02 [Create a web02 project] django-admin.py startapp blog [Create an application named blog] then create a folder under the blog, templates, place the html template. Python manage. py runserver [start django Development Server] Go to settings. py and register the newly created application. [Python] INSTALLED_APPS = ('django. contrib. auth ', 'django. contrib. contenttypes ', 'django. contrib. sessions ', 'django. contrib. sites ', 'django. contrib. messages ', 'django. contrib. staticfiles ', 'blog',) first configure views. py View File [python] # encoding: utf8 from django. http import HttpResponse from django. template import loader, Context class Person (object): def _ init _ (self, name, age, sex): self. name = name self. age = Age self. sex = sex def say (self): return self. name + ''+ str (self. age) + ''+ self. sex def index (req): tsf-loader.get_template('index.html ') # load the template file name = 'Tom 'age = 23 sex = 'male' title = 'emp' marry = True booklist = ['python', 'php ', 'java'] booktuple = ('A', 'B', 'C') books = {'A': 'python', 'B ': 'C ++ '} zhangsan = Person ('hangsan', 23, 'male') c = Context ({# Load file content 'name': name, 'age': age, 'sex': sex, 'title': title, 'Marry': marry, 'booklist': booklist, 'bookuple': booktuple, 'books ': books, 'zhangsan': zhangsan,}) html = t. render (c) # return HttpResponse (html) with the template rendering content # return the rendered content def add (req): # the previous index is the focus of the description, the following three are the simplest cases: return HttpResponse ('add list') def dele (req): return HttpResponse ('delete list') def select (req ): return HttpResponse ('select list') and then configure urls. py, after the configuration, these pages can be accessed. [Python] # coding: utf8 from django. conf. urls. defaults import patterns, include, url urlpatterns = patterns ('', url (R' ^ blog/index/$ ', 'blog. views. index'), # If the first half matches the regular expression, the second half calls the index Method url (R' ^ blog/add/$ ', 'blog. views. add'), url (R' ^ blog/dele/$ ', 'blog. views. dele'),) urlpatterns + = patterns ('blog. views ', # This can be used as the prefix. In addition, patterns can overwrite URLs (R' ^ blog/select/$', 'select'). Here, index is used as an example, edit the index in the templates folder. ht Ml file, focusing on rendering [html] <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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.