Django 笔记

2017-10-07  本文已影响31人  yangweigbh

Anacoda里创建环境:

conda create --name django

切换环境

source activate django

安装django

conda install -c anaconda django

检查django是否安装

python -m django --version

创建工程,一个工程由多个app组成

django-admin startproject myproject

生成的工程目录结构

image.png

运行自带的服务器

python manage.py runserver

创建app

django-admin startapp boards

image.png

将创建的app添加到settings.py

image.png

在urls.py里添加处理函数

image.png

boards/views.py

from django.http import HttpResponse

def home(request):
    return HttpResponse('Hello, World!')

定义模型,每个模型对应数据库中的table
boards/models.py

from django.db import models
from django.contrib.auth.models import User


class Board(models.Model):
    name = models.CharField(max_length=30, unique=True)
    description = models.CharField(max_length=100)


class Topic(models.Model):
    subject = models.CharField(max_length=255)
    last_updated = models.DateTimeField(auto_now_add=True)
    board = models.ForeignKey(Board, related_name='topics')
    starter = models.ForeignKey(User, related_name='topics')


class Post(models.Model):
    message = models.TextField(max_length=4000)
    topic = models.ForeignKey(Topic, related_name='posts')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True)
    created_by = models.ForeignKey(User, related_name='posts')
    updated_by = models.ForeignKey(User, null=True, related_name='+')

提交修改:

python manage.py makemigrations

python manage.py migrate

打开shell可以experiment模型

python manage.py shell

模型的一些操作

image.png

创建template文件夹存放htnl模板

image.png image.png

创建static文件夹存放静态文件

image.png

settings.py添加

image.png

使用:

image.png

部署到正式环境时修改settings.py中的STATIC_URL

创建超级用户

python manage.py createsuperuser

访问管理界面http://127.0.0.1:8000/admin/

管理界面添加Board的访问

boards/admin.py

from django.contrib import admin
from .models import Board

admin.site.register(Board)

url(r'^(?P<username>[\w.@+-]+)/$', views.user_profile, name='user_profile')

username作为参数传入user_profile

{% url %}引用urls.py中name指向的url

{% extends 'base.html' %} 继承template,重写{% block %}

表单:

创建forms.py

boards/forms.py

from django import forms
from .models import Topic

class NewTopicForm(forms.ModelForm):
    message = forms.CharField(widget=forms.Textarea(), max_length=4000)

    class Meta:
        model = Topic
        fields = ['subject', 'message']
if  request.method == 'POST':
    form = NewTopicForm(request.POST)
    if form.is_valid():
        topic = form.save()
        return redirect('board_topics', pk=board.pk)
else:
    form = NewTopicForm()
return render(request, 'new_topic.html', {'form': form})

html template中使用form
{{ form.as_p }} or {{ form.as_table}} or {{ form.as_ul }}


创建用户(sign up):

UserCreationForm

from django.contrib.auth import login as auth_login
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            auth_login(request, user)
            return redirect('home')
    else:
        form = UserCreationForm()
    return render(request, 'signup.html', {'form': form})

sign out:

from django.contrib.auth import views as auth_views

url(r'^logout/$', auth_views.LogoutView.as_view(), name='logout'),

settings.py添加

image.png

login in:

url(r'^login/$', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
image.png
上一篇 下一篇

猜你喜欢

热点阅读