Django Project 15

Source: Internet
Author: User

1. preparations. SQL, project, app, admin, (templates, static) are all placed inside the app. 2. settings [python] DATABASES = {'default': {'Engine ': 'django. db. backends. sqlite3 ', 'name': 'My. db ', }} TIME_ZONE = 'Asia/Shanghai' using age_code = 'zh-cn' MIDDLEWARE_CLASSES = ('django. middleware. common. commonMiddleware ', 'django. contrib. sessions. middleware. sessionMiddleware ', # 'django. middleware. csrf. csrfViewMiddleware ', 'django. contrib. auth. middleware. authenticationMiddleware ', 'django. contr Ib. messages. middleware. messageMiddleware ',) INSTALLED_APPS = ('django. contrib. auth ', 'django. contrib. contenttypes ', 'django. contrib. sessions ', 'django. contrib. sites ', 'django. contrib. messages ', 'django. contrib. staticfiles ', 'django. contrib. admin ', 'blog',) 3. urls [python] from django. conf. urls. defaults import patterns, include, url from django. contrib import admin. autodiscover () urlpattern S = patterns ('', url (R' ^ regist/$ ', 'blog. views. regist '), url (R' ^ fileup/$', 'blog. views. fileup'), url (R' ^ fileup2/$ ', 'blog. views. fileup2 '), url (R' ^ admin/', include (admin. site. urls),) views [python] # coding: utf8 from blog. models import User from django. http import HttpResponse from django. shortcuts import render_to_response from django import forms class UserForm (forms. modelForm): class Meta: model = User def regist (req): if req. method = 'post': userform = UserForm (req. POST, req. FILES) # It is both a database object and a form object if userform. is_valid (): userform. save () return HttpResponse ('OK') else: userform = UserForm () return render_to_response('regist.html ', {'userform': userform}) def fileup (req): f = req. FILES. get ('bihead') # obtain the file handle s = f. read () s = '<pre>' + s + '</pre> 'fp = file ('. /note2013 ', 'wb') # Save fp in binary mode. write (s) fp. c Lose () return HttpResponse (s) class MyForm (forms. form): name = forms. charField () note = forms. fileField () def fileup2 (req): if req. method = 'post': form = MyForm (req. POST, req. FILES) if form. is_valid (): name = req. POST. get ('name') note = req. FILES. get ('note') s = note. read () fp = file ('note01', 'wb') fp. write (s) fp. close () return HttpResponse ('OK') else: form = MyForm () return render_to_response ('f Ileup2.html ', {'form': form}) models [python] from django. db import models class User (models. model): name = models. charField (max_length = 20) sex = models. charField (choices = ('M', 'male'), ('F', 'female '), max_length = 1) myfile = models. fileField (upload_to = 'upfile/') def _ unicode _ (self): return self. name static/f.html [html] <body> <form method = "post" enctype = "multipart/form-data" >{{ form. as_p }}< input typ E = "submit"/> </form> </body> templates/fileup2.html &regist.html [plain] view plaincopyprint? <Body> <form action = "/fileup/" method = "post" enctype = "multipart/form-data"> <input type = "file" name = "bighead "/> <br/> <input type = "submit"/> </form> </body> [html] <body> <form method = "POST" enctype = "multipart/ form-data "> {userform. as_p }}< input type = "submit" value = "register"> </form> </body>

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.