[Reprint]django Build Web site in Eclipse environment

Source: Internet
Author: User
Tags central time zone sqlite database

First, create a project
If this is your first time using Django, then you have to do some initial setup. That is, by automatically generating code to build a Django project--a set of settings for a Django project, including database configuration, Django detailed options settings, and Application feature configuration, the following steps are shown below.

1. Create a new Django project


Select SQLite Database

2. Create a website module app

3. Test whether the newly created module is normal

Validating models ... 0 Errors Foundmarch, 2014-10:26:53django version 1.6.2, using Settings ' mysite.settings ' starting development Server A T http://127.0.0.1:8000/Quit the server with Ctrl-break.

After the server starts up, go to the browser to enter the URL: http://127.0.0.1:8000, you will see a delightful, soft light blue "Welcome to Django" page. Indicates that it is working properly!

Let's take a look at what MySite has created:

These files are:
A, the outer mysite directory is just a container for your project. For Django, the directory name is not important; You can rename it to your liking.
B, manage.py: A useful command-line tool that allows you to interact with the Django project in a variety of ways.
C, the inner MySite directory is the actual Python package in your project. The directory name is the Python package name, through which you can import anything inside it. (E.g.import mysite.settings).
D, mysite/__init__.py: an empty file that tells Python that the directory is a python package.
E, mysite/settings.py: setup/configuration for this Django project.
F, mysite/urls.py: The URL statement for the Django project; a Django-driven Web site "directory."
G, mysite/wsgi.py: A portal for a WSGI-compatible Web server to run your project.

Change port number
By default, the development server that is started:d the Jadmin:runserver command listens only to port 8000 on the local IP.

If you want to change the port of the server, pass it as a command line parameter. For example, the following command initiates a server that listens on port 8080:

C:\USERS\D-117>CD F:\workspace\mysite\src\mysite\ #manage the directory where the. py file is located, under the MySite project C:\users\d-117>f:f:\workspace \mysite\src\mysite>python manage.py runserver 8080Validating models ... 0 Errors Foundmarch, 2014-10:31:27django version 1.6.2, using Settings ' mysite.settings ' starting development Server A T http://127.0.0.1:8080/Quit the server with Ctrl-break.

If you want to change the server IP, pass it along with the port number. So, to listen to all public IP addresses (if you want to show off your work on other computers), use:
Python manage.py runserver 0.0.0.0:8000

Database Settings
Now, edit mysite/settings.py. This is a common Python module that contains the module-level variables that represent the Django settings. Change the value of the following key under ' Default ' in databases to match your database connection settings.
A, engine – from ' django.db.backends.postgresql_psycopg2 ', ' django.db.backends.mysql ', ' Django.db.backends.sqlite3 ', ' django.db.backends.oracle ' Select a
B, name – your database name. If you use SQLite, the database will be a file on your computer; In this case,: Setting:name will be a complete absolute path and also contain the name of the file. If the file is not  
exists, it is created automatically the first time the database is synchronized (see below). When you specify a path, you always use a forward slash, even under Windows (for example: ' C:/homes/user/mysite/sqlite3.db ').
C, user – your database user name (not required under SQLite).
D, password – your database password (not required under SQLite).
E, host – your database host address. If it is the same physical machine as your database server, leave it blank (or set to 127.0.0.1) (not required under SQLite).  
If you are creating a new database, we recommend using SQLite only, change the ENGINE to ' Django.db.backends.sqlite3 ' and set the NAME to the place where you want to store the database. SQLite is built into Python, so you don't need to install anything to support your database.

Attention:
If you're using PostgreSQL or MySQL, make sure you've created a database. or through your database interface "CREATE database database_name;" command to do this.
If you use SQLite, you don't need to create anything beforehand-the database file will be created automatically when needed.

When you edit settings.py, change the time_zone to your time zone. The default value is the U.S. Central Time Zone (Chicago). Also, note the Installed_apps settings at the bottom of the file. It saves the current Django instance that has been activated
There is a Django app. Each app can be used by multiple projects, and you can package and distribute it to other people in their projects.

By default,: Setting:installed_apps contains the following apps, which are provided by Django:

django.contrib.auth– authentication system. django.contrib.contenttypes– the content Type framework. Django.contrib.sessions–session frame. django.contrib.sites– Website Management framework. django.contrib.messages– the message frame. django.contrib.staticfiles– static file management framework.

These apps are typically included by default.

Every app in these apps uses at least one database table, so we need to create the tables in the database before using them. To do this, run the following command: Python manage.py syncdb, as described below.

At this point, the project development environment has been established, we can start.

Second, create the model

4. Edit the Code
4.1 Modifying blog.models.py

From django.db import modelsfrom django.contrib import admin# Create your models Here.class blogpost (models. Model):    title = models. Charfield (max_length =)    content = models. TextField ()    timestamp = models. Datetimefield ()    class blogpostadmin (admin. Modeladmin):    list_display = (' title ', ' content ', ' timestamp ') Admin.site.register (blogpost, Blogpostadmin)

We will create a blogpost model that contains the title, content, and timestamp three fields. Each model inherits from the class of the Django.db.models.Model subclass to describe. Each model
Have some class variables, each of which represents a database field.

Each field is represented by an instance of a field-for example, Charfield represents a field of character type and a field Datetimefield represents a datetime type. This will tell Django that each
What type of data is stored in the field.

Each field instance name is the name of the field (for example: title, content, timestamp), and its format belongs to the affinity machine type. This is used in your Python code.
Value, and your database will use this value as the column name for the table.

4.2 Modifying blog.views.py

# Create Your views here.from django.template import loader,contextfrom django.http import Httpresponsefrom Blog.models im Port Blogpostdef Archive (Request):    posts = BlogPost.objects.all ()    t = loader.get_template (' archive.html ')    C = Context ({' posts ': posts})    return HttpResponse (T.render (c))

4.3 Modify mysite.setting.py, find the following section to modify

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

4.4 Modifying mysite.urls.py

From Django.conf.urls import patterns, include, Urlfrom django.contrib import adminadmin.autodiscover () from Blog.views Import archiveurlpatterns = Patterns (",    # Examples:    # URL (r ' ^$ ', ' mysite.views.home ', name= ' home '),    # URL (R ' ^blog/', include (' Blog.urls ')),    URL (r ' ^admin/', include (Admin.site.urls)),    URL (r ' ^blog/', archive),)

5. Create a Style page template
Please add the Templates folder under the Package blog and set up two Web files under Templates: Archive.html and base.html

5.1 Edit archive.html

{% extends "base.html"%}  {% block content%}  {% for post in posts%}  

5.2 Edit Base.html

Third, synchronize the database

Set up your account number and password, for landing blog management background to prepare.

Creating tables ... Creating table django_admin_logcreating table auth_permissioncreating table auth_group_permissionscreating table Auth_ groupcreating table auth_user_groupscreating table auth_user_user_permissionscreating table auth_usercreating table django_content_typecreating table Django_sessionyou just installed Django ' s auth system, which means you don ' t has any Su Perusers defined. Would to create one now? (yes/no): Yesusername (leave blank to use ' d-117 '): Rootemail address: [Email Protected]password:rootpassword (again): RO Otsuperuser created successfully. Installing Custom SQL ... Installing indexes ... Installed 0 object (s) from 0 fixture (s) finished "F:\workspace\mysite\src\mysite\manage.py syncdb" execution.

Four. Run the test
Login interface, login account and password is set when initializing the database.

After successful login jump to the following page:

On this page you can add blog posts:

After the successful release, enter the URL: http://127.0.0.1:8000/blog/for viewing, test success!

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.