[关闭]
@breakerthb 2016-06-06T07:31:30.000000Z 字数 5307 阅读 1349

WebServer(Python+Django)

Django Apache Python


Django环境配置

1. Apache安装

$ sudo apt-get install apache2  

2. mysql安装

MySQL安装

3.python安装

Python安装

4.python mysql包安装

$ sudo apt-get install python-mysqldb  

4.Django安装

Django安装

推荐使用pip安装

开发步骤-管理网站

1.新建Project

创建一个名为“mysite”的网站目录

$ django-admin.py startproject mysite  

运行后,生成下面目录格式:

2.新建APP

APP名为“polls”

$ cd mysite
	$ python manage.py startapp polls  

3.数据库配置

修改mysite/mysite/setting.py中的数据库配置

DATABASES = {  
    'default': {  
        'ENGINE': 'django.db.backends.mysql', #mysql  
        'NAME': 'djangoDB', #data base  
        'USER': 'root',  
        'PASSWORD': 'root',  
        'HOST': '127.0.0.1',  
        'PORT': '',  
    }  
}  

修改mysite/mysite/setting.py中的install APP配置

INSTALLED_APPS = (  
    'django.contrib.admin',  
    'django.contrib.auth',  
    'django.contrib.contenttypes',  
    'django.contrib.sessions',  
    'django.contrib.messages',  
    'django.contrib.staticfiles',  
    'polls', #添加的APP,其他默认
)  

4.默认数据库建立

进入mysql客户端,创建一个新数据库djangoDB

$ mysql -u root -p

> create database djangoDB;
> quit

创建数据表

$ python manage.py syncdb  

在Django 1.9及未来的版本中使用migrate代替syscdb.

$ python manage.py migrate

过程中会需要录入密码

ubuntu :    159357

进入mysql客户端:

mysql> show tables;  
+------------------------+  
| Tables_in_djangoDB     |  
+------------------------+  
| auth_group             |  
| auth_group_permissions |  
| auth_permission        |  
| auth_user              |  
| django_admin_log       |  
| django_content_type    |  
| django_session         |  
| polls_choice           |  
| polls_question         |  
+------------------------+  

5.model和数据库建立

编辑polls/model.py

from django.db import models  
from django.utils import timezone  

# Create your models here.  
class Question(models.Model):  
        question_text=models.CharField(max_length=200)  
        pub_date=models.DateTimeField('date published')  
        def __str__(self):  
                return self.question_text  
        def was_published_recently(self):  
                return self.pub_date >= timezone.now() - datetime.timedelta(days=1)  
class Choice(models.Model):  
        question=models.ForeignKey(Question)  
        choice_text=models.CharField(max_length=200)  
        votes=models.IntegerField(default=0)  
        def __str__(self):  
                return self.choice_text  

编辑polls/admin.py,注册APP

from django.contrib import admin  
from polls.models import Choice, Question  

admin.site.register(Question)  
admin.site.register(Choice)  
# Register your models here.  

创建对应的数据表:

mysql>
create table polls_choice
(
         id int(11) unsigned not null auto_increment primary key,
         question_id int(11) not null,
    choice_text varchar(200) not null,
    votes int(11) not null
);

mysql>
create table polls_question
(
         id int(11) unsigned not null auto_increment primary key,
         question_text varchar(200) not null,
    pub_date datetime not null
);

model对应的mysql数据库结构如下

mysql> desc polls_choice;  
+-------------+--------------+------+-----+---------+----------------+  
| Field       | Type         | Null | Key | Default | Extra          |  
+-------------+--------------+------+-----+---------+----------------+  
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |  
| question_id | int(11)      | NO   | MUL | NULL    |                |  
| choice_text | varchar(200) | NO   |     | NULL    |                |  
| votes       | int(11)      | NO   |     | NULL    |                |  
+-------------+--------------+------+-----+---------+----------------+  
4 rows in set (0.00 sec)  

mysql> desc polls_question;  
+---------------+--------------+------+-----+---------+----------------+  
| Field         | Type         | Null | Key | Default | Extra          |  
+---------------+--------------+------+-----+---------+----------------+  
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |  
| question_text | varchar(200) | NO   |     | NULL    |                |  
| pub_date      | datetime     | NO   |     | NULL    |                |  
+---------------+--------------+------+-----+---------+----------------+  
3 rows in set (0.00 sec)  

6.管理页面

超级用户增加

$ python manage.py createsuperuser  

root : root

运行

    $ python manage.py runserver 0.0.0.0:8080  

此时浏览器打开: http://127.0.0.1:8080/admin

可以看到下面页面:

在这里通过刚才创建的超级用户登录,进行站点管理。

开发步骤-public网页

1.html页面

在mysite/polls/templates/polls目录下添加如下两个文件:

2. view

修改mysites/polls/views.py

from django.shortcuts import render  

from django.http import HttpResponse,Http404  
from polls.models import Question  
from django.template import RequestContext,loader  
def index(request):  
    latest_question_list = Question.objects.order_by('-pub_date')[:5]  
    template = loader.get_template('polls/index.html')  
    context = RequestContext(request, {  
        'latest_question_list': latest_question_list,  
    })  
    return HttpResponse(template.render(context))  
def detail(request, question_id):  
    try:  
        question = Question.objects.get(pk=question_id)  
    except Question.DoesNotExist:  
        raise Http404  
    return render(request, 'polls/detail.html', {'question': question})   
def results(request, question_id):  
    response = "You're looking at the results of question %s."  
    return HttpResponse(response % question_id)  

def vote(request, question_id):  
    return HttpResponse("You're voting on question %s." % question_id)    

3. url匹配

给数据库中加入一条记录:

myslq> insert into polls_question values('ABC', localtime());

打开页面即可进行测试

$ python manage.py runserver 0.0.0.0:8080

打开浏览器: http://127.0.0.1:8080/polls

[from]
http://blog.csdn.net/sicexpn/article/details/39475983

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注