白手起家搭建django app

 

 

$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,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。