Ubunt14.04开发Django应用
发布时间:2016-03-10 14:04:21
作者:admin
阅读: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安装
-
sudo apt-get install apache2
2、mysql安装
-
sudo apt-get install mysql-server mysql-client
3、python安装
4、Django安装
-
sudo apt-get install python-pip
-
sudo pip install Django==1.6.5
-
sudo apt-get install python-mysqldb
二、开发步骤-管理网站
1、新建Project
-
django-admin.py startproject mysite
2、新建APP
-
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> 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
-
-
-
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)
-
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
-
运行
-
python manage.py runserver 0.0.0.0:8000
此时浏览器打开即可访问到管理界面,实现友好的控制(数据库、用户等)
三、开发步骤-public网页
1、html页面
-
root@u21:~/mysites/polls/templates/polls
-
detail.html index.html
-
root@u21:~/mysites/polls/templates/polls
-
<h1>{{question.question_text}}</h1>
-
<ul>
-
{% for choice in question.choice_set.all %}
-
<span style="white-space:pre"> </span><li>{{choice.choice_text}}</li>
-
{% endfor %}
-
</ul>
-
root@u21:~/mysites/polls/templates/polls
-
{%if latest_question_list %}
-
<ul>
-
{% for question in latest_question_list %}
-
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
-
{% endfor %}
-
</ul>
-
{% else %}
-
<p>No polls are available.</p>
-
{% endif %}
2、view
-
root@u21:~/mysites/polls
-
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匹配
-
root@u21:~/mysites/polls
-
-
from django.conf.urls import patterns, url
-
-
from polls import views
-
-
urlpatterns = patterns('',
-
-
url(r'^$', views.index, name='index'),
-
-
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
-
-
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
-
-
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
-
)
-
root@u21:~/mysites/mysites
-
from django.conf.urls import patterns, include, url
-
-
-
from django.contrib import admin
-
admin.autodiscover()
-
-
-
urlpatterns = patterns('',
-
-
-
-
-
-
url(r'^admin/', include(admin.site.urls)),
-
url(r'^polls/', include('polls.urls')),
-
)
打开页面即可进行测试
如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术
有疑问联系站长,请联系QQ: