$django-admin.py startproject web2
$cd web2/
$python manage.py startapp blog
$vim web2/settings.py
注意settings.py以下两个部分,首先要配置数据库,其次要在INSTALLED_APP里添加刚才新建的app
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘, # Add ‘postgresql_psycopg2‘, ‘mysql‘, ‘sqlite3‘ o
‘NAME‘: ‘dj_db01‘, # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
‘USER‘: ‘root‘,
‘PASSWORD‘: ‘‘,
‘HOST‘: ‘localhost‘, # Empty for localhost through domain sockets
‘PORT‘: ‘‘, # Set to empty string for default.
}
}
INSTALLED_APPS = (
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.sites‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘blog‘,
# Uncomment the next line to enable the admin:
‘django.contrib.admin‘,
# Uncomment the next line to enable admin documentation:
‘django.contrib.admindocs‘,
)
接下来编辑models.py
$vim blog/models.py
from django.db import models
在mysql里新建对应的数据里
mysql> create database dj_db01 default charset utf8;
现在可以自动生成各种表啦
$ python manage.py syncdb
现在编辑以下url的config
$vim web1/urls.py
from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()urlpatterns = patterns(‘‘,
# Examples:
# url(r‘^$‘, ‘web1.views.home‘, name=‘home‘),
# url(r‘^web1/‘, include(‘web1.foo.urls‘)),# Uncomment the admin/doc line below to enable admin documentation:
# url(r‘^admin/doc/‘, include(‘django.contrib.admindocs.urls‘)),# Uncomment the next line to enable the admin:
# url(r‘^admin/‘, include(admin.site.urls)),
url(r‘^blog/$‘,‘blog.views.index‘),
)
$mkdir blog/static/images
然后从别的地方拷贝过来一张图,这里我拷贝过来的img2.jpg
接下来编辑一下页面
$mkdir blog/templates
$vim blog/templates/index.html
<h1>hello world</h1>
<img src=‘/static/images/img2.jpg‘/>
运行一下
python manage.py runserver 3900
白手起家搭建django app
时间: 2024-10-13 01:15:13