Skip to content

Commit

Permalink
login app MTV模型组织,涉及修改setting文件,添加js静态文件,登陆用户持久化
Browse files Browse the repository at this point in the history
  • Loading branch information
qq516249940 committed Nov 20, 2022
1 parent 04c2b9c commit b56873b
Show file tree
Hide file tree
Showing 21 changed files with 146 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/__pycache__

vess.text
.vscode/settings.json
Binary file added 2022-11-19-12-45-31.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2022-11-19-12-48-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ from collections.abc import Iterable
cmdb模型图
![](2022-11-05-15-04-23.png)

一般Web框架的架构
![](2022-11-19-12-45-31.png)

Django的MTV模型组织,分别是models,views,Templates
![](2022-11-19-12-48-50.png)

学习地址:
[CMDB资产管理系统](https://www.liujiangblog.com/course/django/118) 内容有点旧,django 2.2的
Expand Down
4 changes: 3 additions & 1 deletion cmdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,6 @@ def __str__(self):

class Meta:
verbose_name = '事件纪录'
verbose_name_plural = "事件纪录"
verbose_name_plural = "事件纪录"


Binary file modified db.sqlite3
Binary file not shown.
Empty file added login/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions login/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions login/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class LoginConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'login'
22 changes: 22 additions & 0 deletions login/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.1 on 2022-11-20 18:38

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='UserInfo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user', models.CharField(max_length=32)),
('pwd', models.CharField(max_length=32)),
],
),
]
Empty file added login/migrations/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions login/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models

# Create your models here.
## 练习
class UserInfo(models.Model):
user = models.CharField(max_length=32)
pwd = models.CharField(max_length=32)
3 changes: 3 additions & 0 deletions login/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions login/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from . import views

urlpatterns = [
path(r'', views.index, name='index'),
]
19 changes: 19 additions & 0 deletions login/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.shortcuts import render,HttpResponse
from login import models
# Create your views here.

user_list = []

def index(request):
# return HttpResponse("hello world welcome login")
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
print(username, password)
# temp = {'user': username, 'pwd': password}
# user_list.append(temp)
# 将数据保存到数据库
models.UserInfo.objects.create(user=username, pwd=password)
# 从数据库中读取所有数据,注意缩进
user_list = models.UserInfo.objects.all()
return render(request, 'index.html',{'data': user_list})
15 changes: 13 additions & 2 deletions pyecharts_django_demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from pathlib import Path

import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -25,7 +27,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']


# Application definition
Expand All @@ -39,6 +41,7 @@
'django.contrib.staticfiles',
'demo', # <---
'cmdb',
'login',
]

MIDDLEWARE = [
Expand All @@ -56,7 +59,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down Expand Up @@ -119,6 +122,14 @@

STATIC_URL = 'static/'

# this is directory paths where you have to put your project level static files
# you can put multiple folders here
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
# this is directory name where collectstatic files command will put your app level static files
# STATIC_ROOT = 'staticfiles'
# STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static')


# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

Expand Down
4 changes: 2 additions & 2 deletions pyecharts_django_demo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

urlpatterns = [
path('admin/', admin.site.urls),
path(r'demo/', include('demo.urls')) # <---

path(r'demo/', include('demo.urls')), # <---
path(r'login/',include('login.urls')) # <---login app
]
5 changes: 5 additions & 0 deletions static/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1.txt
1.txt
1.txt
1.txt
1.txt
Binary file added static/django.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions static/js/jquery-3.2.1.min.js

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% comment %} <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<h1 style="background-color: antiquewhite;color: black">Hello World!</h1>
<script src="/static/js/jquery-3.2.1.min.js"></script>
</body>
</html> {% endcomment %}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>用户输入:</h1>
<form action="/login/" method="post">
{% csrf_token %} <!--加入这行 -->
用户名:<input type="text" name="username" /><br />
密码:<input type="password" name="password" /><br />
<input type="submit" value="提交" />
</form>

<h1>用户展示:</h1>
<table border="1">
<thead>
<tr>用户名</tr>
<tr>密码</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{ item.user }}</td>
<td>{{ item.pwd }}</td>
</tr>
{% endfor %}
</tbody>
</table>


</body>
</html>

0 comments on commit b56873b

Please sign in to comment.