django后台开发怎么样
比较简单,可以一试。
1.通过命令行生成项目和应用:
django-admin startproject demo1 和 python manage.py startapp novel
2.建立templates和static文件夹
3.形成如下结构:
4.在setting中进行如下配置:
5.Navicat中必须提前建立上述数据库novel
6.将所需要的html与静态文件分别放在相对应的文件夹,然后对html代码进行修改(只列举关键部分):
7.url.py
[python] view plain copy
from django.conf.urls import url
from django.contrib import admin
from novel import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.index),
]
from __future__ import unicode_literals
from django.db import models
class Novel(models.Model):
novelid = models.AutoField(primary_key=True)
type = models.IntegerField()
novelname = models.CharField(max_length=100)
novelimg = models.CharField(max_length=100)
description = models.TextField()
state = models.CharField(max_length=20)
author = models.CharField(max_length=20)
def __unicode__(self):
return self.novelname
class Author(models.Model):
author_name = models.CharField(max_length=10)
author_age = models.IntegerField()
def __unicode__(self):
return self.author_name
from django.shortcuts import render
from django.http import HttpResponse
from models import Novel
def index(request):
nov = Novel.objects.filter().order_by('?')[:4]
context = {
'novels':nov
}
return render(request,'index.html',context=context)
8.models.py
[python] view plain copy
写完models.py时需要在命令行下输入:
python manage.py makemigrations 和 python manage.py migrate
这样就可以在数据库中生成对应的表
9.生成表之后,在Navicat中随便输入一些数据,然后通过sql语句将其数量倍增,如:
INSERT INTO 表名(字段)SELECT 字段 FROM 表名
****在这里需要注意的是图片的网址不是保存在本地的,如下:
10.views.py
[python] view plain copy
11.运行python manage.py runserver 访问,如下(刷新一次可出现不同的小说内容):
OK!