python3 django学习
环境信息
C:\Users\Administrator>python -m django --version
2.0.6
F:\python\web\web2>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
1、进入准备建立网站项目的文件页面,django-admin startproject mysite #mysite是自己命名的,可以随意命名
2、进到mysite目录 python manage runserver
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
August 22, 2018 - 19:58:23
Django version 2.0.6, using settings 'mybook.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
使用 http://172.0.0.1:8000/ 登录
3、创建应用 python manage startapp blog
首先配置以下视图文件
app中的views文件内容
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request,'index.html')
这个文件中有一个index.html文件,此文件创立在app目录下的templates文件夹中,任意点击一个网页点击右键查看网页源代码保存下来的html格式文件
此处需要修改setting文件TEMPLATES中的'DIRS': ['templates'],以及INSTALLED_APPS中添加''blog'',加入新加的app名称
然后添加url路由,在mysite的urls文件中
urls.py文件内容
from django.contrib import admin
from django.urls import path
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index),
]
这里和其他版本不同,写为正则表达的时候,程序运行错误
4、在models.py 写数据模型
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class BlogArticles(models.Model):
title = models.CharField(max_length=300)
author = models.ForeignKey(User,related_name='blog_posts',on_delete = models.CASCADE,)
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
开始报TypeError: __init__() missing 1 required positional argument: 'on_delete'错误,查询解决https://www.cnblogs.com/phyger/p/8035253.html
5、https://www.cnblogs.com/0bug/p/8193356.html