Python3 learning the second Django build

Source: Internet
Author: User

Strictly speaking, this should be the sequel of the previous article, this is also the environment to build: Build a Web development environment.

1, download the latest Django, the latest is the current 1.8.2. So I just under this version, download down is a GZ package django-1.8.2.tar.gz, in fact, whether it is Windows7 or CentOS7 can download this version, extracted out on it.

I unzipped it to the PYTHON34 installation directory. Then execute at the command line:

Python setup.py Install

After the automatic installation is complete. Go to scripts. Then create a project to get a perceptual look

Python django-admin-script. py Startproject MySite CD Mysitepython manage. PY Runserver 2222

Last line if you do not enter a port number of 2222, Django will use 8000 by default.

Open in the browser to test and observe

Depending on the browser's prompt, enter

Python manage.py Startapp Blog

To create an app app, a blog. Since our application is under MySite This project, we can open the mysite below the urls.py file for URL settings, which is similar to the MVC route. Add a sentence to the urls.py

URL (r'^blog/index$'blog.views.index'),

This tells the browser, if someone entered the URL is Blog/index then I directed to the blog directory under the views.py file index method to go. So next we go to the blog directory of the views.py inside to join

 from Import HttpResponse # Create your views here. def     Index (req):    return httpresponse ('')

Start

Python manage.py runserver 2222

The effect that you see in the browser.

But

The above is directly written out, the real situation can not be changed every time this method, so Django has the concept of template.

According to the Convention, all the template should be saved under the Templates directory under the App app, so where to create a index.html file in Blog\templates. The content is simple input

<!DOCTYPE HTML><HTML>    <Head>        <title>First Django</title>    </Head>    <Body>        <H1>Hello,world. --from template.</H1>    </Body></HTML>

Then change the index method in the views.py

 from Import HttpResponse  from Import Loader,context # Create your views here. def     Index (req):    t=loader.get_template ('index.html')    c=Context ({})    return HttpResponse (T.render (c))

And of course there's one way

 from Import Render_to_response   def    Index (req):   return render_to_response ('index.html', {} )

At this point, if you start the browser and then test it, you will be told that you cannot find the template file. The solution is to add a blog app with a back-end management system from Django, which is to open the mysite/mysite/settings.py file, with a blog at the back of the Installed_apps block. Such as

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

Then start again to see our content.

To summarize:

1, python setup.py install2,python django-admin.py startproject MySite3,python django-admin.py Startapp Blog4, vim settings.py add App:blog5, Vim urls.py url (r"','Blog.views.index')6,vim blog/views.py fromDjango.httpImportHttpResponsedefIndex (req):returnHttpResponse ('')7,python manage.py Runserver 2222

2,centos7

Download the django-1.8.2.tar.gz package on the same website, then unzip and use the auto-install command

Install

Specifically, as I upgraded the Python to 3.4.3 version in the previous article, but other related ones were not upgraded, and then found that Yum could not be used, baidu,bing find some Web pages found to solve the problem, because when I installed the RPM, Say that there is no dependency, but these dependencies are going to be installed with Yum, which is a washout. So I had to back it up again. Use the 2.7.5 version that comes with it. I'll use it before I go back.

Whereis python

Locate the path to the 2.7.5, and then execute

Ln -sf/usr/bin/python2. 7 /usr/bin/python

So that we can use

 #  1, create project, and change setting.py and urls.py  django-admin startproject mysitecd mysite / cd mysite /#  2, create application app  django -#   #2-1, create a template for the application  mkdir TEMPLATESCD Templatesvim index.html  # 2-2, create a call method for the template  CD. Vim views.py  #  3, starting development server  CD. Python manage.py runserver  2222 
Simple_django_sample

browser to see our homemade templates.

Python3 learning the second Django build

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.