Build a Django app from scratch

Source: Internet
Author: User

$django-admin.py startproject web2$cd web2/$python manage.py startapp blog$vim web2

Note settings.py The following two sections, the first to configure the database, followed by the Installed_app to add the new 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 is 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',)

Next Edit models.py

$vim blog/models.py

 from Import Models

In MySQL, the corresponding data is created.

Mysql> CREATE DATABASE dj_db01 default CharSet UTF8;

You can now automatically generate various tables.

$ python manage.py syncdb

Now edit the config for the following URL

$vim web1/urls.py
 fromDjango.conf.urlsImportpatterns, include, url#uncomment the next 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

and copy it from somewhere else, and here I copy the img2.jpg.

Next Edit the page

$mkdir blog/Templates$vim Blog/templates/index.html

/static/images/img2.jpg'/>

Run for a minute

Python manage.py runserver 3900

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.