1)用pycharm创建django的project

2)打开pycharm中的Terminal输入python manage.py startapp host_manage创建一个app.

3)创建完需要把project同名下settings.py设置一下,INSTALLED_APPS需要添加已创建的app名称.

把MIDDLEWARE中'django.middleware.csrf.CsrfViewMiddleware'注释掉,DATABASES需要设置自己电脑上面
数据库名称.在settings.py文件中最后需要添加STATICFILES_DIRS=((os.path.join(BASE_DIR,'static')),).
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'NODEDB',
'USER':'root',
'PASSWORD':'buzhidao',
'HOST':'127.0.0.1',
'PORT':'3306',
}
}

4)Project的urls中需要以下设置:
from django.conf.urls import url
from django.contrib import admin
from host_manage import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/',views.login),
url(r'^home/',views.home),
url(r'^detail-(\d+).html/',views.detail),
# url(r'^orm/',views.orm),#这个url主要是为往数据添加数据临时用.
]
5)效果图如下:

详细代码如下:
models.py
from django.db import models

# Create your models here.


class Node_Info(models.Model):
uid=models.AutoField(primary_key=True)
node_name=models.CharField(max_length=32,null=True)
node_type=models.CharField(max_length=32,null=True)
node_rbsid=models.CharField(max_length=32,null=True)
node_ip=models.CharField(max_length=64,null=True)
node_addr=models.CharField(max_length=64,null=True)
node_rnc=models.CharField(max_length=64,null=True)

urls.py
"""homework01 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from host_manage import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/',views.login),
url(r'^home/',views.home),
url(r'^detail-(\d+).html/',views.detail),
# url(r'^orm/',views.orm),
]


views:
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import redirect
from host_manage import models

def login(request):
error_msg=''
if request.method=='GET':
return render(request,'login.html')
elif request.method=='POST':
u=request.POST.get('user')
p=request.POST.get('pwd')
if u=='root' and p=='buzhidao':
return redirect('/home')
else:
error_msg='用户名或密码错误'
return render(request,'login.html',{'error_msg':error_msg})


def home(request):
data=models.Node_Info.objects.all()
node_info=[]
for row in data:
temp={'uid':row.uid,'node_name':row.node_name,
'node_type':row.node_type,'node_id':row.node_rbsid}
node_info.append(temp)
return render(request,'home.html',{'node_info':node_info})





def detail(request,nid):
data = models.Node_Info.objects.all()
node_info={}
detail_info={}
for row in data:
node_info[row.uid]={'node_name': row.node_name,
'node_type': row.node_type,'node_id': row.node_rbsid,
'node_ip':row.node_ip,'node_addr':row.node_addr,
'node_rnc':row.node_rnc}
detail_info=node_info.get(int(nid)) #这里必须要传整数,int转换一下。这是一个大坑.
return render(request,'detail.html',{'detail_info':detail_info})

'''
def orm(request):
#往数据库添加数据
models.Node_Info.objects.create(
node_name='WBJ00001',node_rbsid='1',
node_ip='1.1.1.1',node_type='RBS3206',
node_addr='FS',node_rnc='ECR02')
models.Node_Info.objects.create(
node_name='WBJ00002', node_rbsid='2',
node_ip='2.2.2.2', node_type='RBS3418',
node_addr='DX',node_rnc='ECR02')
models.Node_Info.objects.create(
node_name='WBJ00003', node_rbsid='3',
node_ip='3.3.3.3', node_type='RBS6601',
node_addr='CP',node_rnc='ECR03')
return HttpResponse('数据库成功添加数据')
'''

detail.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Detail</title>
</head>
<body>
<h1>详细信息</h1>
<h3>站号:{{ detail_info.node_name }}</h3>
<h3>站型:{{ detail_info.node_type }}</h3>
<h3>RNC:{{ detail_info.node_rnc }}</h3>
<h3>IP:{{ detail_info.node_ip }}</h3>
<h3>站址:{{ detail_info.node_addr }}</h3>
<h3>RBSID:{{ detail_info.node_id }}</h3>
</body>
</html>

home.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<ul>
{% for row in node_info %}
<li><a target="_blank" href="/detail-{{ row.uid }}.html">{{ row.node_name }}</a></li>
{% endfor %}
</ul>
</body>
</html>


login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<style>
span{
color:red;
font-size:16px;
font-family:'楷体';
font-weight:bolder;
}
</style>
</head>
<body>
<form action="/login/" method="post">
<p>
<input type="text" name="user" placeholder="用户名"/>
</p>
<p>
<input type="password" name="pwd" placeholder="密码"/>
</p>
<input type="submit" value="提交">
<span>{{ error_msg }}</span>
</form>
</body>
</html>