mysite/manage.pymysite/__init__.pysettings.pyurls.pywsgi.py
polls/__init__.pyadmin.pymigrations/__init__.pymodels.pytests.pyviews.py
INSTALLED_APPS = ('django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','polls',)
DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR, 'mysitedb.sqlite3'),}}
// 如果使用Mysql,需要先在Mysql中创建相应的数据库,才能连接成功DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysitedb.mysql',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '3306', // Mysql默认本地数据库服务port
}
}
The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file and the database migrations shipped with the app .
By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.
Migrations are how Django stores changes to your models (and thus your database schema) - they’re just files on disk.
from django.http import HttpResponse
def index(request):return HttpResponse("Hello, world. You're at the polls index.")
from django.conf.urls import patterns, urlfrom polls import views
urlpatterns = patterns('',# ex: /polls/url(r'^$', views.index, name='index'),# ex: /polls/5/url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),)
from django.conf.urls import patterns, include, urlfrom django.contrib import admin
urlpatterns = patterns('',url(r'^polls/', include('polls.urls')), // host之后“polls”对应的URL就被定位到'polls.urls'中去url(r'^admin/', include(admin.site.urls)),)
from django.contrib import admin
# Register your models here.from polls.models import Choice, Question
class QuestionAdmin(admin.ModelAdmin):fieldsets = [(None, {'fields': ['question_text']}),('Level', {'fields': ['question_level']}),]list_display = ('question_text', 'pub_date')
admin.site.register(Question, QuestionAdmin)
from django.shortcuts import get_object_or_404, renderfrom django.http import HttpResponseRedirectfrom django.core.urlresolvers import reversefrom django.views import generic
from polls.models import Choice, Question
class IndexView(generic.ListView):template_name = 'polls/index.html'context_object_name = 'latest_question_list'
def get_queryset(self):"""Return the last five published questions."""return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):model = Questiontemplate_name = 'polls/detail.html'
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',url(r'^$', views.IndexView.as_view(), name='index'),url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),)
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
body {background: white url(" images/background.gif") no-repeat right bottom;}