Skip to content

Commit

Permalink
测试温习,更新demo03,异步刷新实现区域变化
Browse files Browse the repository at this point in the history
  • Loading branch information
cjb0721 committed Jun 22, 2019
1 parent 7df3c99 commit cd1e578
Show file tree
Hide file tree
Showing 41 changed files with 9,876 additions and 19 deletions.
5 changes: 5 additions & 0 deletions demo01/booktest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
urlpatterns = [
# url('index/', views.index),
# url('index/$', views.index),

url(r'^$', views.index, name='index'),

url(r'login/$', views.login, name='login'),
url(r'logout/$', views.logout, name='logout'),

url(r'list/$', views.list, name='list'),
url(r'detail/(\d+)/$', views.detail, name='detail'),
url(r'addbook/$', views.addbook, name='addbook'),
Expand Down
68 changes: 56 additions & 12 deletions demo01/booktest/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
from django.shortcuts import render
from django.shortcuts import render, redirect, reverse, get_object_or_404, get_list_or_404
from django.http import HttpResponse, HttpResponseRedirect
from .models import Book,Hero
from django.template import loader
from datetime import timedelta

# Create your views here.


def login(request):
if request.method == 'GET':
return render(request, 'booktest/login.html')
elif request.method == 'POST':
username = request.POST['username']
# password = request.POST['password']
request.session['username'] = username
# request.session['password'] = password
request.session.set_expiry(3600)
# request.session.set_expiry(timedelta(days=5)) 报错

return redirect(reverse('booktest:index'))


def logout(request):
res = render(request, 'booktest/index.html')
# res.delete_cookie('username')

request.session.delete('username')
# request.session.delete('password')

return res


def index(request):
print("请求", request)
# return HttpResponse("首页")
Expand All @@ -16,7 +41,15 @@ def index(request):
# result = indexpage.render(body)
# # 3、返回模板
# return HttpResponse(result)
return render(request, 'booktest/index.html', {"username": "ggb"})

# print(dir(request.COOKIES))
# print(request.COOKIES.get('username'))
# return render(request, 'booktest/index.html', {"username": request.COOKIES.get('username')})

# print(dir(request.session))
return render(request, 'booktest/index.html', {"username": request.session.get('username')})

# return render(request, 'booktest/index.html', {"username": "ggb"})


def list(request):
Expand All @@ -27,14 +60,16 @@ def list(request):

def detail(request, id):
# return HttpResponse("详情页"+str(id))
book = Book.objects.get(pk=id)
return render(request, 'booktest/detail.html', {'book': book})

# try:
# book = Book.objects.get(pk=int(id))
# return HttpResponse(book)
# except:
# return HttpResponse("请输入正确ID")
# book = Book.objects.get(pk=id)
# return render(request, 'booktest/detail.html', {'book': book})

try:
book = Book.objects.get(pk=int(id))
except:
# book = get_object_or_404(Book, pk=id)
book = get_list_or_404(Book, pk=id)
return render(request, 'booktest/detail.html', {'book': book})


def addbook(request):
Expand All @@ -50,7 +85,8 @@ def addbookhander(request):
b = Book()
b.book_title = bname
b.save()
return HttpResponseRedirect('/booktest/list/', {'booklist': b})
# return HttpResponseRedirect('/booktest/list/', {'booklist': b})
return HttpResponseRedirect(reverse('booktest:list'), {'booklist': b})


def delete(request, id):
Expand All @@ -60,7 +96,8 @@ def delete(request, id):
# 使用render没有刷新请求URL
# return render(request, 'booktest/list.html', {'booklist': book})
# 使用HttpResponseRedirect重定向
return HttpResponseRedirect('/booktest/list/', {'booklist': book})
# return HttpResponseRedirect('/booktest/list/', {'booklist': book})
return HttpResponseRedirect(reverse('booktest:list'), {'booklist': book})
except:
return HttpResponse("删除失败")

Expand All @@ -78,7 +115,8 @@ def modifybookhander(request, id):
b = Book.objects.get(pk=id)
b.book_title = bname
b.save()
return HttpResponseRedirect('/booktest/list/', {'booklist': b})
# return HttpResponseRedirect('/booktest/list/', {'booklist': b})
return HttpResponseRedirect(reverse('booktest:list'), {'booklist': b})


def addhero(request, id):
Expand All @@ -95,6 +133,7 @@ def deletehero(request, hid):
book = result.book
result.delete()
return HttpResponseRedirect('/booktest/detail/'+str(book.id)+'/', {'book': book})
# return HttpResponseRedirect(reverse('booktest:detail')+str(book.id)+'/', {'book': book})
except:
return HttpResponse("添加失败")

Expand All @@ -121,7 +160,10 @@ def modifyherohander(request, id):
h.content = hcontent
h.book = b
h.save()
# print(str(bid))
# print(reverse('booktest:detail')+str(bid)+'/')
return HttpResponseRedirect('/booktest/detail/'+str(bid)+'/', {'book': b})
# return HttpResponseRedirect(reverse('booktest:detail', str(bid)), {'book': b})


def addherohander(request):
Expand All @@ -142,7 +184,9 @@ def addherohander(request):
h1.content = hskill
h1.book = book
h1.save()

return HttpResponseRedirect('/booktest/detail/'+str(bid)+'/', {"book": book})
# return HttpResponseRedirect(reverse('booktest:detail')+str(bid)+'/', {"book": book})

# return HttpResponse("提交成功")

Expand Down
Binary file modified demo01/db.sqlite3
Binary file not shown.
8 changes: 8 additions & 0 deletions demo01/demo01/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# DEBUG = False

ALLOWED_HOSTS = []
# ALLOWED_HOSTS = ['*', ]


# Application definition
Expand Down Expand Up @@ -137,3 +139,9 @@
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

SESSION_ENGINE = 'redis_sessions.session'
SESSION_REDIS_HOST = 'localhost'
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 0
SESSION_REDIS_PASSWORD = ''
SESSION_REDIS_PREFIX = 'session'
19 changes: 13 additions & 6 deletions demo01/templates/booktest/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@
</head>
<body>
<div class="container">

{# 单行注释 #}
{% comment %}
多行注释
{% endcomment %}

{% if username %}
<h2>{{username}}</h2>
<h2>欢迎您:<small>{{username}}</small></h2>
<a href="{% url 'booktest:logout' %}">退出登录</a><br>
{# 超级链接硬编码 #}
{#<a href="/booktest/list">进入列表页</a>#}
{#去除硬编码#}
<a href=" {% url 'booktest:list' %} ">进入列表页</a>
{% else %}
<h2>没有登录</h2>
<h2>没有登录,请前往 <a href="{% url 'booktest:login' %}">登录</a></h2>
{% endif %}

{# <img src="/static/img/bg1.png" alt="" width="1000px"> #}
<img src="{% static 'img/bg1.png' %}" alt="" width="1000px"><br>

{# 超级链接硬编码 #}
{#<a href="/booktest/list">进入列表页</a>#}
{#去除硬编码#}
<a href=" {% url 'booktest:list' %} ">进入列表页</a>
</div>

</body>
Expand Down
16 changes: 16 additions & 0 deletions demo01/templates/booktest/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h2>用户登录</h2>
<form action="{% url 'booktest:login' %}" method="post">
用户: <input type="text" name="username" required><br>

<input type="submit" value="登录">
</form>
<a href="{% url 'booktest:index' %}">返回</a>
</body>
</html>
Binary file modified demo02/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion demo02/demo02/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
Expand Down
1 change: 1 addition & 0 deletions demo02/templates/polls/addchoice.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<div class="container">
<h2>添加选项</h2>
<form action="{% url 'polls:addchoicehander' %}" method="post">
{% csrf_token %}
选项名称:<input type="text" name="item" required><br>
投票数:<input type="number" name="vote" required><br>
<input type="hidden" name="qid" value="{{qs.id}}">
Expand Down
1 change: 1 addition & 0 deletions demo02/templates/polls/addquestion.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<div class="container">
<h2>热门话题添加</h2>
<form action="{% url 'polls:addquestionhander' %}" method="post">
{% csrf_token %}
标题:<input type="text" name="title" required>
<input type="submit" value="添加">
</form>
Expand Down
1 change: 1 addition & 0 deletions demo02/templates/polls/modifychoice.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<div class="container">
<h2>编辑选项</h2>
<form action="{% url 'polls:modifychoicehander' sl.id %}" method="post">
{% csrf_token %}
选项:<input type="text" name="item" placeholder="{{sl.item}}" required>
票数:<input type="number" name="vote" placeholder="{{sl.vote}}" required>
<input type="submit" value="保存">
Expand Down
1 change: 1 addition & 0 deletions demo02/templates/polls/modifyquestion.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<div class="container">
<h2>话题修改</h2>
<form action="{% url 'polls:modifyquestionhander' qt.id %}" method="post">
{% csrf_token %}
标题:<input type="text" name="title" required placeholder="{{qt.text}}">
<input type="submit" value="保存">
</form>
Expand Down
2 changes: 2 additions & 0 deletions demo02/templates/polls/vote.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ <h2>问题:{{qs.text}}</h2>
<!--{# <form action="/polls/detail/" method="post"> #}-->
<form action="{% url 'polls:detail' %}" method="post">
<ul>
{# 跨站请求伪造保护,一般用在表单post请求中 #}
{% csrf_token %}
{% for i in qs.select_set.all %}
<li>
<input type="radio" name="vote" value="{{i.id}}" required>{{i.item}}
Expand Down
Binary file added demo03/db.sqlite3
Binary file not shown.
Empty file added demo03/demo03/__init__.py
Empty file.
Loading

0 comments on commit cd1e578

Please sign in to comment.