Django1.7 configuration demo tutorial (Environment setup)

Source: Internet
Author: User
Tags install django pip install django

Recently, I used Django to create a simple project. I have never used it for more than a year. It is reasonable to say that there is no problem.

The following is a simple demo process:


Prerequisites: You have prepared python2.7 for installation.


1. Build a Django Environment
Download https://bootstrap.pypa.io/ez_setup.py


Save local
Run Python ez_setup.py


2. Install Pip
C: \ python27 \ scripts> easy_install.exe Pip


3. Install diango
PIP install Django = 1.7


3. Create a Django Project
C: \ python27 \ Lib \ Site-packages \ Django \ bin \ django-admin.py startproject bluescf


4. Run Python manage. py runserver in the project folder.
Open the browser: http: // 127.0.0.1: 8000/


5. Create an app + Model
Python manage. py Startapp demosite
Note: A bluescf app has been created by default.




6. Join? Template path
Add settings. py? The following code

Import OS. Path


Template_dirs = (
OS. Path. Join (OS. Path. dirname (_ file _), 'templates'). Replace ('\\','/'),
)




7. Joining templates? HTML file, pause: index.html

8. Create views. py

from django.http import HttpResponsefrom django.shortcuts import render_to_responsefrom django.template import RequestContext, loader    #return HttpResponse("Hello, world. You‘re at the poll index.")  def index(request):# View code here...    t = loader.get_template(‘index.html‘)    c = RequestContext(request, {‘foo‘: ‘bar‘})    return HttpResponse(t.render(c),        content_type="application/xhtml+xml")

9. Configure URLs. py

# Coding = UTF-8 from Django. conf. URLs import patterns, include, urlfrom Django. contrib import adminimport viewsurlpatterns = patterns ('', # examples: # URL (R' ^ $ ', 'bluescf. views. home ', name = 'home'), # URL (R' ^ blog/', include ('blog. URLs '), URL (R' ^ admin/', include (Admin. site. URL (R' ^ $ ', views. index, name = 'home'), # The index method of views is directly entered by default)


10. Open the browser: http: // 127.0.0.1: 8000/to preview the effect. If everything is normal, it will be OK.


In fact, the index method in my views. py was not written like this at the beginning. It was written as follows:

def index(request):return render_to_response(‘index.html‘)

An error is returned,


Never go to Baidu or Google to search for _ init _ () got an unexpected keyword argument 'mimetype '. Useless, a bunch of useless information will come out, it is your blessing to search out my article, ^ _ ^.

This is obviously because the old method used for API upgrade (I wrote this before django1.3)
So we need to check the API: http://django.readthedocs.org/en/latest/topics/http/shortcuts.html#django.shortcuts.render_to_response


Are there any problems with this?

In fact, there is still a problem,

def index(request):# View code here...    t = loader.get_template(‘index.html‘)    c = RequestContext(request, {‘foo‘: ‘bar‘})    return HttpResponse(t.render(c),        content_type="text/xml")


In the above example, your index.html only contains strings or incomplete HTML (you will certainly use some template inheritance), or your content_type ="
application/xhtml+xml
"

Sorry, that's unfortunate. You will be prompted:


This page contains the following errors: Error on line 9 at Column 1: Extra content at the end of the documentbelow is a rendering of the page up to the first error. In fact, Django parses your HTML page based on content_type. Instead of in-depth research, you only need to change it to content_type = "text/html" to display HTML normally.

Do not copy samples on the Internet around the page. It is very important to know some details.


If you have any questions, please contact me (csdn Technology Group QQ group:221057495).

Django1.7 configuration demo tutorial (Environment setup)

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.