Ubunt14.04开发Django应用

发布时间:2016-03-10 14:04:21 阅读:1042次

转:http://blog.csdn.net/sicexpn/article/details/39475983

http://www.ziqiangxuetang.com/django/django-tutorial.html

http://www.runoob.com/django/django-admin-manage-tool.html

一、Django环境配置

1、Apache安装

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install apache2

2、mysql安装

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install mysql-server mysql-client

3、python安装

4、Django安装

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install python-pip  
  2. sudo pip install Django==1.6.5  
  3. sudo apt-get install python-mysqldb

二、开发步骤-管理网站

1、新建Project

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. django-admin.py startproject mysite

2、新建APP

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. python manage.py startapp polls  

3、数据库配置

修改mysite/mysite/setting.py中的数据库配置
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. DATABASES = {  
  2.     'default': {  
  3.         'ENGINE': 'django.db.backends.mysql',//mysql  
  4.         'NAME': 'djangoDB',//data base  
  5.         'USER': 'root',  
  6.         'PASSWORD': 'root',  
  7.         'HOST': '127.0.0.1',  
  8.         'PORT': '',  
  9.     }  
  10. }
修改mysite/mysite/setting.py中的install APP配置
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. INSTALLED_APPS = (  
  2.     'django.contrib.admin',  
  3.     'django.contrib.auth',  
  4.     'django.contrib.contenttypes',  
  5.     'django.contrib.sessions',  
  6.     'django.contrib.messages',  
  7.     'django.contrib.staticfiles',  
  8.     'polls',//添加的APP,其他默认  
  9.   
  10. )  

4、默认数据库建立

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. python manage.py syncdb  
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mysql> show tables;  
  2. +------------------------+  
  3. | Tables_in_djangoDB     |  
  4. +------------------------+  
  5. | auth_group             |  
  6. | auth_group_permissions |  
  7. | auth_permission        |  
  8. | auth_user              |  
  9. | django_admin_log       |  
  10. | django_content_type    |  
  11. | django_session         |  
  12. | polls_choice           |  
  13. | polls_question         |  
  14. +------------------------+  

5、model和数据库建立

编辑polls/model.py
[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. from django.db import models  
  2. from django.utils import timezone  
  3.   
  4. # Create your models here.  
  5. class Question(models.Model):  
  6.         question_text=models.CharField(max_length=200)  
  7.         pub_date=models.DateTimeField('date published')  
  8.         def __str__(self):  
  9.                 return self.question_text  
  10.         def was_published_recently(self):  
  11.                 return self.pub_date >= timezone.now() - datetime.timedelta(days=1)  
  12. class Choice(models.Model):  
  13.         question=models.ForeignKey(Question)  
  14.         choice_text=models.CharField(max_length=200)  
  15.         votes=models.IntegerField(default=0)  
  16.         def __str__(self):  
  17.                 return self.choice_text 
编辑polls/admin.py,注册APP
[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. from django.contrib import admin  
  2. from polls.models import Choice, Question  
  3.   
  4. admin.site.register(Question)  
  5. admin.site.register(Choice)  
  6. # Register your models here.  
model对应的mysql数据库结构如下
[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mysql> desc polls_choice;  
  2. +-------------+--------------+------+-----+---------+----------------+  
  3. | Field       | Type         | Null | Key | Default | Extra          |  
  4. +-------------+--------------+------+-----+---------+----------------+  
  5. | id          | int(11)      | NO   | PRI | NULL    | auto_increment |  
  6. | question_id | int(11)      | NO   | MUL | NULL    |                |  
  7. | choice_text | varchar(200) | NO   |     | NULL    |                |  
  8. | votes       | int(11)      | NO   |     | NULL    |                |  
  9. +-------------+--------------+------+-----+---------+----------------+  
  10. 4 rows in set (0.00 sec)  
  11.   
  12. mysql> desc polls_question;  
  13. +---------------+--------------+------+-----+---------+----------------+  
  14. | Field         | Type         | Null | Key | Default | Extra          |  
  15. +---------------+--------------+------+-----+---------+----------------+  
  16. | id            | int(11)      | NO   | PRI | NULL    | auto_increment |  
  17. | question_text | varchar(200) | NO   |     | NULL    |                |  
  18. | pub_date      | datetime     | NO   |     | NULL    |                |  
  19. +---------------+--------------+------+-----+---------+----------------+  
  20. 3 rows in set (0.00 sec)

6、管理页面

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 超级用户增加  
  2. python manage.py createsuperuser  
  3. 运行  
  4. python manage.py runserver 0.0.0.0:8000 
此时浏览器打开即可访问到管理界面,实现友好的控制(数据库、用户等)

三、开发步骤-public网页

1、html页面

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. root@u21:~/mysites/polls/templates/polls# ls  
  2. detail.html  index.html  
[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. root@u21:~/mysites/polls/templates/polls# cat detail.html   
  2. <h1>{{question.question_text}}</h1>  
  3. <ul>  
  4. {% for choice in question.choice_set.all %}  
  5. <span style="white-space:pre">    </span><li>{{choice.choice_text}}</li>  
  6. {% endfor %}  
  7. </ul>  
[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. root@u21:~/mysites/polls/templates/polls# cat index.html   
  2. {%if latest_question_list %}  
  3.     <ul>  
  4.     {% for question in latest_question_list %}  
  5.         <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>  
  6.     {% endfor %}  
  7.     </ul>  
  8. {% else %}  
  9.     <p>No polls are available.</p>  
  10. {% endif %}  

2、view

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. root@u21:~/mysites/polls# cat views.py  
  2. from django.shortcuts import render  
  3.   
  4. from django.http import HttpResponse,Http404  
  5. from polls.models import Question  
  6. from django.template import RequestContext,loader  
  7. def index(request):  
  8.     latest_question_list = Question.objects.order_by('-pub_date')[:5]  
  9.     template = loader.get_template('polls/index.html')  
  10.     context = RequestContext(request, {  
  11.         'latest_question_list': latest_question_list,  
  12.     })  
  13.     return HttpResponse(template.render(context))  
  14. def detail(request, question_id):  
  15.     try:  
  16.     question = Question.objects.get(pk=question_id)  
  17.     except Question.DoesNotExist:  
  18.         raise Http404  
  19.     return render(request, 'polls/detail.html', {'question': question})   
  20. def results(request, question_id):  
  21.     response = "You're looking at the results of question %s."  
  22.     return HttpResponse(response % question_id)  
  23.   
  24. def vote(request, question_id):  
  25.     return HttpResponse("You're voting on question %s." % question_id)    

3、url匹配

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. root@u21:~/mysites/polls# cat urls.py  
  2.   
  3. from django.conf.urls import patterns, url  
  4.   
  5. from polls import views  
  6.   
  7. urlpatterns = patterns('',  
  8.     # ex: /polls/  
  9.     url(r'^$', views.index, name='index'),  
  10.     # ex: /polls/5/  
  11.     url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),  
  12.     # ex: /polls/5/results/  
  13.     url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),  
  14.     # ex: /polls/5/vote/  
  15.     url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),  
  16. )  
[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. root@u21:~/mysites/mysites# cat urls.py  
  2. from django.conf.urls import patterns, include, url  
  3.   
  4.   
  5. from django.contrib import admin  
  6. admin.autodiscover()  
  7.   
  8.   
  9. urlpatterns = patterns('',  
  10.     # Examples:  
  11.     # url(r'^$', 'mysites.views.home', name='home'),  
  12.     # url(r'^blog/', include('blog.urls')),  
  13.   
  14.   
  15.     url(r'^admin/', include(admin.site.urls)),  
  16.     url(r'^polls/', include('polls.urls')),  
  17. )  
打开页面即可进行测试

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:Ubunt14.04开发Django应用 出自老鄢博客 | 欢迎分享