1.创建django项目、创建django应用
2.定义模型类
Country(国家):国家名、国家的描述
city(城市): 城市名、城市的特点、所属国家(外键)、人口数量
首页展示所有国家,点击国家展示该国家的所有城市
点击城市名展示该城市详细信息,在详细信息后有删除和修改的操作
一、创建好项目,配置文件(具体操作见我的上上篇文章)
二、定义模型类
在models.py文件里创建模型类
from django.db import models
# Create your models here.
class Country(models.Model):
name = models.CharField(max_length=10,verbose_name='国家名')
miaoshu = models.CharField(max_length=100,verbose_name='国家的描述')
class Meta:
db_table = 'country'
verbose_name_plural = '国家'
def __str__(self):
return self.name
class City(models.Model):
name = models.CharField(max_length=10,verbose_name='城市名')
tese = models.CharField(max_length=50,verbose_name='城市的特点')
country = models.ForeignKey(to=Country,on_delete=models.CASCADE,verbose_name='所属国家id')
num = models.IntegerField(verbose_name='人口数量')
class Meta:
db_table = 'city'
verbose_name_plural = '城市'
def __str__(self):
return self.name
三、 生成迁移文件,执行迁移文件,可以在设置超级用户添加数据(具体操作见我的上上篇文章)
四、写视图
from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from a1.models import Country,City
# Create your views here.
class CountryView(View):
def get(self,request):
coun=Country.objects.all()
dict = {'coundata':coun}
return render(request,'country.html',dict)
class CityView(View):
def get(self,request,pk):
city = City.objects.filter(country_id=pk)
dict = {'citydata':city}
return render(request,'city.html',dict)
class DeleteVeiw(View):
def get(self,request,pk):
City.objects.filter(id=pk).delete()
return redirect('/country/')
class UpdataView(View):
def get(self,request,pk):
return render(request,'updata.html')
def post(self,request,pk):
name1 =request.POST.get('uname')
tese1 = request.POST.get('utese')
country_id1 = request.POST.get('ucountry_id')
num1 = request.POST.get('unum')
City.objects.get(id=pk).update(name=name1,tese=tese1,country_id=country_id1,num=num1)
return redirect('/city/%s'%country_id1)
#country.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>国家</h3>
<table width="300px" border="1px">
<tr>
<th>序号</th>
<th>国家名</th>
<th>国家的描述</th>
</tr>
{% for foo in coundata %}
<tr>
<th>{{ foo.id }}</th>
<th><a href="/city/{{ foo.id }}/">{{ foo.name }}</a></th>
<th>{{ foo.miaoshu }}</th>
</tr>
{% endfor %}
</table>
</body>
</html>
#city.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>城市</h3>
<table width="500px" border="1px">
<tr>
<th>城市名</th>
<th>城市的特点</th>
<th>所属国家id</th>
<th>人口数量</th>
<th>操作</th>
</tr>
{% for foo in citydata %}
<tr>
<th>{{ foo.name }}</th>
<th>{{ foo.tese }}</th>
<th>{{ foo.country_id }}</th>
<th>{{ foo.num }}</th>
<th>
<a href="/update/{{ foo.id }}/">修改</a>
<a href="/del/{{ foo.id }}/">删除</a>
</th>
</tr>
{% endfor %}
</table>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>修改信息</h3>
<form method="post">
{% csrf_token %}
城市名:<input type="text" name="uname">
城市的特点:<input type="text" name="utese">
所属国家:<input type="text" name="ucountry_id">
人口数量:<input type="text" name="unum">
<input type="submit" value="修改">
</form>
</body>
</html>
五、配置路由文件
from django.urls import path
#导包导入views
from a1 import views
urlpatterns = [
path('country/',views.CountryView.as_view()),
path('city/<int:pk>/',views.CityView.as_view()),
path('del/<int:pk>/',views.DeleteVeiw.as_view()),
path('update/<int:pk>/',views.UpdataView.as_view())
]
from django.contrib import admin
from django.urls import path,include
from a1 import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include(urls))
]