• 白手起家搭建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
  • 相关阅读:
    使用MySQL存储过程连续插入多条记录
    为什么编程语言以及数据库要从1970年1月1日开始计算时
    关于shtml页面include问题解决方案
    简单实用的FTP操作类
    js实现完美身份证号有效性验证
    .htaccess文件的建立和rewrite_module的启用
    php冒泡排序
    php实现汉诺塔问题(递归)
    简单的mysql数据库备份程序
    选择排序的php实现
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/3775509.html
Copyright © 2020-2023  润新知