Using Python+django to develop Web sites in the Eclipse environment

Source: Internet
Author: User
Tags auth central time zone sessions sqlite sqlite database create database


First, create a project
If this is the first time you use Django, then you have to make some initial settings. That is, by automatically generating code to create a Django project-a setup set of Django projects that includes database configuration, Django detail Options settings, and application feature configurations, as shown in the following steps.



1. New Django Project


Select SQLite Database



2. Create Web site module app



3. Test the new module is normal


Validating models

... 0 errors found
March, 2014-10:26:53
Django version 1.6.2, using Settings ' mysite.settings ' starting
devel Opment Server at Http://127.0.0.1:8000/
Quit the server with Ctrl-break.


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

Let's see what MySite has created:

These files are:
A, the outer mysite directory is just a container for your project. This directory name is not important for Django; 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 of this Django project.
F, mysite/urls.py: The Django Project URL statement, a Django-driven Web site "directory."
G, mysite/wsgi.py: a WSGI-compliant Web server entry to run your project.



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



If you want to change the port of the server, pass it as a command-line argument. For example, the server started by the following command will listen on port 8080:


C:\USERS\D-117>CD F:\workspace\mysite\src\mysite\ #manage. py file directory, that is, MySite project
c:\users\d-117>f:
F : \workspace\mysite\src\mysite>python manage.py runserver 8080
validating models ...

0 errors found
March, 2014-10:31:27
Django version 1.6.2, using Settings ' mysite.settings ' starting
devel Opment Server at 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. Therefore, to monitor 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 generic Python module that contains module-level variables that represent 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 ' Choose a
b, name– your database name. If you use SQLite, the database will be a file on your computer, in which case: Setting:name will be a complete absolute path and also contain the name of the file. If the file does not
exists, it is automatically created 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 username (not required under SQLite).
D, password– your database password (not required under SQLite).
E, host– your database host address. If you are the same physical machine as your database server, leave this blank (or set to 127.0.0.1) (not required under SQLite).
If you are creating a new database, we recommend that you use SQLite only, change the ENGINE to ' Django.db.backends.sqlite3 ' and set NAME to the place where you want to store the database. SQLite is built into Python, so you don't have to install anything to support your database.



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



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
has Django applications. Each application can be used by multiple projects, and you can package and distribute it to others for use in their projects.



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


django.contrib.auth– authentication system.
django.contrib.contenttypes– Content Type framework.
django.contrib.sessions–session Framework.
django.contrib.sites– Web site management framework.
django.contrib.messages– message Framework.
django.contrib.staticfiles– static file management framework.


These applications are typically included by default.



All of these applications use at least one database table for each application, so we need to create 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 set up, we can start.



Second, create the model



4. Edit Code
4.1 Modification blog.models.py


From django.db import models to
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'll create a blogpost model that contains title, content, timestamp three fields. Each model inherits from the class of the Django.db.models.Model subclass to describe it. Each model
Have some class variables, each of which represents a database field.

Each field is represented by an instance of a field-such as Charfield fields that represent character types and Datetimefield fields that represent date-time. This will tell Django that each
What type of data is stored in the field.

Each field instance's name is the name of the field (for example: title, content, timestamp), which is in the form of affinity machine. You're going to use this in your Python code.
Value, and your database will use that value as the column name for the table.



4.2 Modification blog.views.py


# Create your views here.
From django.template import Loader,context to
django.http import HttpResponse from
blog.models Import Blogpost

def 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 Modification mysite.urls.py


From Django.conf.urls import patterns, include, URLs from

django.contrib import admin
admin.autodiscover ()

From blog.views Import archive

urlpatterns = 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 create two Web files under the templates: Archive.html and base.html



5.1 Editor Archive.html



{% extends "base.html" %}  
{% block content %}  
{% for post in posts %}  
<h1>{{ post.title}}</h1>  
<p>{{ post.content }}</p>
<p>{{ post.timestamp|date:"1, F jS"}}</p>
{% endfor %}  
{% endblock %}




5.2  edit base.html 用Python+Django在Eclipse环境下开发web网站


<html>  
  <style type="text/css">  
    body { color: #edf; background: #453; padding: 0 5em; margin:0 }  
    h1 { padding: 2em lem; background:#675 }  
    h2 { color: #bf8; border-top: 1px dotted #fff; margin-top: 2em }  
    p { margin: lem 0 }  
  </style>  
  <body>  
    <h1><center>Alexia's Blog</center></h1>  
    {% block content %}  
    {% endblock %}  
  </body>  
</html>



Third, the synchronization database
Set your account and password to prepare for login to the management background of the blog. Develop web sites in Eclipse with Python + Django

Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (Yes / no): yes
Username (leave blank to use 'd-117'): root
Email address: root@qq.com
Password: root
Password (again): root
Superuser 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.
Develop web sites in Eclipse with Python + Django  





Four. Run the test
Login interface, login account and password are set when initializing the database.
After successful login, go to the following page:
You can add blog posts on this page:
After publishing successfully, enter the URL: http://127.0.0.1:8000/blog/ to view and test successfully.
Develop web sites in Eclipse with Python + Django

http://www.cnblogs.com/linjiqin/p/3595891.html



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.