美文网首页
Week4 Django WebFramework

Week4 Django WebFramework

作者: 快要没时间了 | 来源:发表于2016-06-07 21:41 被阅读0次

Build virtual environment

  1. Create a new virtual environment by python3

~$ python3 -m venv django_venv

  1. switch to this new venv

~$ source django_venv/bin/activate
(django_venv)~$
this means u have switched to a new venv

  1. install django into venv

pip install django

  1. Check installation

~$ python

(in python console)
import django
django.VERSION

Build a Django Project

(django_venv)~$ django-admin.py startproject mysite

Run a server

switch to the project path

python manage.py runserver

Then the server will be run on http://localhost:8000
if fail, try this:

python manage.py migrate
python manage.py runserver

Create Admin account

python manage.py createsuperuser

Then the server will be run on http://localhost:8000/admin

Add new app

~$ python manage.py startapp newApp

and then add it to mysite/settings.py

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'newApp',]

Add templates

  • Add a directory

mysite$ mkdir templates

  • Edit sittings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\', '/')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
  • Add render function in view.py
# trips/views.py

from datetime import datetime
from django.shortcuts import render


def hello_world(request):
    return render(request, 'hello_world.html', {
        'current_time': datetime.now(),
    })```

# Add Statics
edit settings.py

> STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

edit html file

{% load static %}
<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static 'css/new_blah.css' %}">
</head>
<body>


# Lesson52 Use  Context to modify templates

add a dict for render in views.py

from django.shortcuts import render

Create your views here.

def index(request):
context = {
'title': 'title from context',
}
return render(request, 'index.html', context)


Modify your webpage in templates like this:

![{{}} is use for substitude](https://img.haomeiwen.com/i625787/2dfc6908fe518006.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

### Models and Database
add connect into settings.py for mongodb

from mongoengine import connect
connect('wbsite', host = '127.0.0.1', port=27017)

edit models.py to get item info

相关文章

网友评论

      本文标题:Week4 Django WebFramework

      本文链接:https://www.haomeiwen.com/subject/jstjdttx.html