b1: tao file project:
virtual --no-site-packages ten
b2: source /thu muc chua project/bin/activate
b3: install Django
easy_install Django
b4: cd /bin
b5: django-admin.py startproject ten project
de chay thu : python manager.py runserver
tao db: python manager.py syncdb . dien user va pass de login vao trang admin
chinh sua file setup.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/home/kevinkien/Desktop/django/bin/polls/db.sqlite3',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
}
}
Mo file model.py va them code:
from django.db import models
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('data published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
Sua file admin.py de co the thay duoc Poll, Choice
from django.contrib import admin
from polls.models import Poll, Choice
admin.site.register(Poll)
admin.site.register(Choice)
Tiep theo de hien thi dong helloword, mo file view.py :
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
Tao 1 file urls.py va them noi dung:
from django.conf.urls import patterns, url
from poll import views
urlpatterns = patterns('',
url(r'^view_1/$', views.index, name='index'),
)
sua file urls.py o polls: from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^poll/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Bay gio ban chay python manager.py runserver: vao trang 127.0.0.1/poll/view_1