Django Project Process

Source: Internet
Author: User

Create a Django project using eclipse

Open settings.py Add:

Static_root = Os.path.join (Os.path.dirname (__file__), ' static ') Staticfiles_dirs = ((' CSS ', Os.path.join (Static_root, ' CSS '). replace (' \ \ ', '/'), (' JS ', Os.path.join (static_root, ' JS '). replace (' \ \ ', '/'), (' Images ', Os.path.join (STAT Ic_root, ' images '). replace (' \ \ ', '/'), (' Upload ', Os.path.join (static_root, ' upload '). replace (' \ \ ', '/'),) TEMPLATE _dirs = (Os.path.join (Os.path.dirname (__file__), ' Templates '). replace (' \ \ ', '/'), Upload_dirs = (Os.path.join (OS.PA Th.dirname (__file__), ' upload '). replace (' \ \ ', '/'),


Create templates, Static,upload directory, structure under project


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/58/02/wKioL1SnwC7jpWghAAC1WZGdJNk257.jpg "title=" Qq20150103180733.png "alt=" Wkiol1snwc7jpwghaac1wzgdjnk257.jpg "/>


Create the view.py input under the project:

From django.shortcuts import render_to_responsedef main_index (Request): Return Render_to_response (' index.html ', {})

To open the urls.py configuration URL:

URL (r ' ^$ ', Main_index),


Create index.html under the Templates directory

Css,js,images calls are written as:

<link rel= "stylesheet" type= "Text/css" href= "/static/css/easyui.css" ><script type= "Text/javascript" src= "/ Static/js/jquery.min.1.8.2.js "></SCRIPT>.CS-NORTH-BG {width:100%;height:100%;background:url (/static/ images/header_bg.png) Repeat-x;}

Other HTML access and so on



MySQL database access: (Mysqltool already installed, not installed please Baidu) and there is a database

Set the following: Open settings.py:

databases = {     ' Default ': {          ' ENGINE ':  ' django.db.backends.mysql ',          ' NAME ':   ' Py_zsh ',                                ' USER ':  ' root ',                                  ' PASSWORD ':  ',                              ' HOST ':  ' 127.0.0.1 ',                                      ' PORT ':  ' 3306 ',                            #           ' OPTIONS ': {#               ' autocommit ': true,#          },    }} #在最后添加你的项目INSTALLED_APPS  =  (     ' Django.contrib.admin ',     ' Django.contrib.auth ',     ' Django.contrib.contenttypes ',     ' django.contrib.sessions ',     ' Django.contrib.messages ',     ' django.contrib.staticfiles ',     ' py_zsh ' ,)


Created under Project: models.py

#coding =utf-8from django.db Import models# User table class users (models. Model): Username = models. Charfield (max_length=50) #用户名 password = models. Charfield (max_length=50) #密码 type = models. Charfield (max_length=10) #类型

Run manage.py using the command line or with the run as parameter under Eclipse:

Command: Python manage.py syncdb

The system automatically creates the table



When setting the upload form, you will be prompted with the following error:

Forbidden(403)

CSRF verification failed. Request aborted.

You is seeing this message because this site requires a CSRF cookie when submitting forms. This cookie was required for security reasons, to ensure that your browser was not being hijacked by third parties.

If you had configured your browser to disable cookies, please re-enable them, at least for this site, or for ' Same-origin ' Requests.


Set the following: Open settings.py:

middleware_classes = (' Django.contrib.sessions.middleware.SessionMiddleware ', ' Django.middleware.common.CommonMiddleware ', # ' Django.middleware.csrf.CsrfViewMiddleware ', ' Django.contrib.auth.middleware.AuthenticationMiddleware ', ' Django.contrib.auth.middleware.SessionAuthenticationMiddleware ', ' Django.contrib.messages.middleware.MessageMiddleware ', ' Django.middleware.clickjacking.XFrameOptionsMiddleware ' , ' Django.middleware.security.SecurityMiddleware ',)

The pound sign can be commented out


Upload the Excel file to the upload directory:

#coding =utf-8from django.shortcuts import render_to_responseimport py_zsh.settingsfrom  datetime import datetimeimport timedef index (Request):     if  request.method== "GET":         return render_to_response (' Doc/index.html ',  {})     else:        try:             data = request. Post            datafile = request. Files.get (' File_stu ',  none)             #/* Discriminant is not an. xls file, discriminant is not an Excel file */                file_types = datafile.name.split ('. ')             file_type = file_typEs[len (file_types)  - 1];              print file_type            if file _type.lower ()   !=  "xlsx"  and file_type.lower ()!=  "xls":                  return render_to_response (' Error.html ',  {"message": "Please upload a properly formatted Excel file"})              file = handle_uploaded_file (datafile)              return render_to_response (' main.html ',  {})          except:            return  Render_to_response (' error.html ',  {}) #表单提交后写入文件def  handle_uploaded_file (f):     Filename = py_zsh. settings. upload_dirs[0]+ "\ \". replace (' \ \ ', '/') +time.strftime ('%y%m%d%h%m%s ') +f.name    with  Open (filename,  ' wb+ ')  as info:        for chunk  in f.chunks ():             info.write ( Chunk)     return filename




This article is from the "Anaf" blog, make sure to keep this source http://anngle.blog.51cto.com/5542868/1598761

Django Project Process

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.