Windows django1.7 +python3.4.2 Build record 1

Source: Internet
Author: User
Tags install django

Python+django is relatively simple to build under Linux, Windows is relatively complex to build, so listed below
First, download the installation
Download the Django Package and execute the command Python setup.py install in the Django-1.7 directory just after unpacking

or enter the command directly in the Scripts folder PIP3 install Django

Test the Django installation, create a new file in the directory, and enter the following text to run

# vim:set fileencoding=utf-8: # Test your Django installation Import Django Print (Django.) VERSION)

Go to a directory first, and then run django-admin.py startproject mydjangosite (if you do not run Django-admin startproject Mydjangosite successfully)
Special Note: 1. You must add the D:\Python34\Lib\site-packages\django\bin value environment variable under path before executing the command.

The

2.python folder default opener is Python.exe. Otherwise, the django-admin.py file is opened when the program finishes executing.
You can create a project named Mydjangosite, where the Web site folder
is successfully created and run under this folder: Python manage.py runserver
Two, link database
In the Python2.7 era, the MYSQLDB connection to MySQL also does not support Python3.4, Pymysql can be used instead of
download plug-ins and then install
in Django databases settings also do not have to make any changes, As in the previous MySQLdb, as follows:

 DATABASES = ' default ':  { ' ENGINE ': ' Django.db.backends.mysql ', #   database engine  ' NAME ': ' Test ', #   ' USER ': ' Root ', 
     
      # 
       ' PASSWORD ': ' Root ', #   password  ' host ': ', #   database port, MySQL defaults to 3306  ' OPTIONS ':  ' autocommit ': true , ' Span style= "color: #000000;" >} ,  

The most critical point, in the site's __init__.py file, we add the following code:

Import pymysql pymysql.install_as_mysqldb ()

Once you've done this, you'll be able to access MySQL in Django.
Third, run Django on multiple versions of the computer
If the system has both Python2 and Python3, when running Django, no matter what the path adds, will run the Pyhton2 version, so before running Runserver, to enter the Python2 installation directory, Rename the Python.exe to Python2.exe
Five, error handling
Sometimes there will be ****python manage.py migrate*** such as hints, directly enter the python manage.py migrate this command and then start the server, the command is used to reconstruct the site
Vi. Creating an Application
Open CMD,CD to the project folder, execute Python manage.py startapp Views (app name can be determined) if it fails, you can omit the. py, and then you have an application folder with the name of the views, the specific page can be placed in the application, such as the homepage
Seven, create the first page
In the settings.py file to fill in the name of the app you just created (whether it is a Web page or client, call the app), take a look at the path, under the same folder, the direct app name is good, do not need to write other paths

Installed_apps = (    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',        ' views',)

Open the folder where the app is stored and write a few words in models.py (directly with the default model), such as

 from Import Models # Create your models here. class Test (models. Model):    = models. TextField ()

and set up the first test page
In the App folder views.py (name can be arbitrary) to write example core code:

 from Import Render,render_to_response  from Import HttpResponse # Create your views here. def Hello (Request):     return HttpResponse (" I am the first example of Django! ")

If the error in Chinese, first replace the Chinese into English test

If the English display is normal, the Chinese cannot output, the error is as follows:

Unicodedecodeerror: ' UTF8 ' codec can ' t decode byte 0xb2 in position 0:invalid start byte

This problem generally occurs on Windows systems, which is caused by the default character of the system utf8 inconsistent with the Python default character set. The workaround is simple, open the views.py file with an editor such as EditPlus or notepad++, then save it as an identical name, just set the character set to UTF8, and refresh the page after saving. Done.

The next step is to bind the mapping relationship inside urls.py:

From Django.conf.urls import patterns,include,Urlfrom Django.Contrib Import Admin#methods for importing view definitionsfrom views Import Hello (note the path of views, may need to add the parent folder name)#Views This name is used hereUrlpatterns = Patterns ("',#Examples:  #URL (r ' ^$ ', ' Django Project. Views.home ', name= ' home '),  #URL (r ' ^blog/', include (' Blog.urls ')),URL (r ' ^admin/',include(Admin.site.urls)),#Hellword-based bindingURL (r ' ^hello/$ ', hello),URL (r' ^myhtml/$ ', myhtml),URL (r' ^cc/$ ', BB),)

Then enter the specified address and page name in the browser, where the binding page is Hello, so the page address is Hello

http://127.0.0.1:8000/hello/

Windows django1.7 +python3.4.2 Build record 1

Related Article

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.