Django & Ajax Part01

Source: Internet
Author: User

Background Requirements Analysis

Use Django+ajax background scenarios when users fill out a very detailed registration application form! The most worrying thing for users is to be disturbed during the filling of the form, followed by the hope to fill out the table after the submission of any mistakes, otherwise you have to new! Therefore, the first thing we think of is the possibility of using Ajax to deal with. The introduction of Ajax is usually based on the PHP background writing mode, I have never used PHP to write the server, so looking for a job for me is a nightmare ah!

Development environment Preparation

Here I'll show you how to build and configure our Django environment, and the various codes we write later, if not specifically stated, are based on this project environment!

Python 2.7.101.8.6

Development Steps Demo

1) Build Projects & Applications

CD src
django-admin.py Startproject Ajaxsample
CD Ajaxsample
django-admin.py Startapp Poll

The basic configuration directory structure is as follows:

SRC    ajaxsample        ajaxsample        poll        manage.py        db.sqlite3

2) Create a static file

mkdir src/static_in_env_ajaxsample/mkdir src/static_in_env_ajaxsample/static_root/mkdir src/ ajaxsample/static_in_pro/mkdir src/ajaxsample/static_in_pro/our_static/mkdir src/ ajaxsample/static_in_pro/our_static/js/mkdir src/ajaxsample/static_in_pro/our_static/css/

3) Configure static files (src/ajaxsample/ajaxsample/settings.py)

Add app poll to your project

#application DefinitionInstalled_apps= (    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Poll',)

Set the static file path

#Static files (CSS, JavaScript, Images)#https://docs.djangoproject.com/en/1.8/howto/static-files/Static_url='/static/'Static_root= Os.path.join (Os.path.dirname (Base_dir),"Static_in_env_ajaxsample","Static_root") Staticfiles_dirs=[Os.path.join (Base_dir,"Static_in_pro","our_static"),]media_url='/media/'Media_root= Os.path.join (Os.path.dirname (Base_dir),"Static_in_env_ajaxsample","Media_root")

4) running the Django component

Python manage.py collectstatic

Next you will be asked to check the path without errors directly select Yes

5) test each configuration is normal

#1由于我们没有配置templates文件, so we set up a templates directly under the poll directory

mkdir poll/templates/
CD templates

#2先在你已经到templates文件目录下啦! You can start writing hello.html files!

<HTML><Head><title>Welcome</title></Head><Body><H1>Hello world!</H1></Body></HTML>

#3写一个视图 (src/ajaxsample/poll/views.py)

def Hello (Request):     return render (request,"hello.html", {})

#4创建一个urls. PY (src/ajaxsample/poll/urls.py)

 from Import  fromimport     = [url (r ' ^hello/$ ', ' Poll.views.hello ' ),]

#5为了让项目知道你创建了一个urls. py configuration file, you need to configure the urls.py in the project

 from Import  fromimport      = [url (r ' ^admin/')  , include (Admin.site.urls)),    URL (r', include ('poll.urls' ) )),]

#6现在可以运行开发服务器啦!

Python manage.py runserver

#7到你的浏览器看看吧! (http://127.0.0.1:8000/hello/)

Summarize

The first part is over, let me know if you have any suggestions.

Thank you!

Django & Ajax Part01

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.