1.配置url
2.调用logout函数进行登出信息
3.调用HttpResponseRedirect函数进行重定向
url(r'^loginout/$', logout_view, name='user_loginout'),
def logout_view(request):
if request.method == 'GET':
next_page = request.GET.get('next', '/')
logout(request)
return HttpResponseRedirect(next_page) # 转到来时页面
#return render(request, 'index.html', {}) # 登陆成功,由后台渲染跳转至index,并在index中判断,头部显示
退出
点击数,收藏数,未读消息
1.每次调用视图逻辑,即点击数+1
2.开始学习即课程学生人数+1
3.在每个视图函数中埋点,只要调用了该视图函数即为相应的数据点击数实现+1操作
4.课程、教师、机构收藏数
#教师点击量操作
class TeacherDetailView(View):
def get(self,request,teacher_id):
#教师信息
teacher = Teacher.objects.get(id=teacher_id)
#教师的课程
courses = teacher.course_set.all()[:3] #course内有teacher这个外键,利用_set获取与该teacher相关的所有课程
#教师排行榜
all_teacher = Teacher.objects.all()
#收藏状态
teacher_fav = False
#该页面每点击一次,点击量+1
teacher.ClickNumber+=1
teacher.save()
organization_fav = False
if request.user.is_authenticated(): #先判断是否登陆
if UserFavorite.objects.filter(User=request.user.id,FavoriteID=teacher.id,FavoriteType=3):
teacher_fav = True
if UserFavorite.objects.filter(User=request.user.id, FavoriteID=teacher.id, FavoriteType=2):
organization_fav = True
return render(request, 'teacher_detail.html', {'teacher':teacher,'courses':courses,'all_teacher':all_teacher,'teacher_fav':teacher_fav,'organization_fav':organization_fav})
#机构点击量操作
class OrganiztionDetailView(View):
"""
机构首页
"""
def get(self,request,org_id):
organization = CourseOrganization.objects.get(id=int(org_id))
#teacher = Teacher.objects.filter(BelongOrganization_id=int(org_id))
teacher = organization.teacher_set.all()[:1] #根据model的名字和外键的关联,反向取值
#course = Course.objects.filter(Organization_id=int(org_id))
course = organization.course_set.all()[:3] #根据model的名字和外键的关联,反向取值
#该页面每点击一次,点击量+1
organization.ClickNumber+=1
organization.save()
###########是否已收藏#############
has_fav = False #默认未收藏
#先判断登录
if request.user.is_authenticated():
if UserFavorite.objects.filter(User=request.user.id,FavoriteID=organization.id,FavoriteType=2):
has_fav = True
return render(request, 'organization_home.html', {'organization':organization, 'teacher':teacher, 'course':course,'has_fav':has_fav})
1.首页变成动态页面
2.课程加上广告位标志位
3.增加轮播图文件信息
4.利用forloop函数,获取循环的次数
5.模板汇总利用{{forloop|add:2}}进行加法处理
url(r'^$',IndexView.as_view(),name='index'),
class IndexView(View):
def get(self,request):
banners = Banner.objects.all()
courses_banner = Course.objects.filter(Display=True)
courses = Course.objects.filter(Display=False)[:6]
organization = CourseOrganization.objects.all()[:20]
return render(request, 'index.html', {'banners':banners,'courses_banner':courses_banner,'courses':courses,'organization':organization})
首页轮播图数据绑定
{% for banner in banners %}
{% endfor %}
课程轮播图数据绑定
{% for banner in courses_banner %}
{% endfor %}
其他课程展示数据绑定
{% for course in courses %}
{% endfor %}
首页机构数据绑定,在模板中利用{% if forloop.counter|divisibleby:5 %}判断是否能被5整除
{% for org in organization %}
全国知名
{{ org.Organization }}
{% endfor %}
1、在templates中添加404.html
2、修改settings.py
3、修改views.py
4、修改urls.py
在templates目录里,添加一个404.html文件,我们自定义的404错误页面就写在这个文件里。
settings.py
DEBUG =True 改为 DEBUG =False
ALLOWED_HOSTS = [] 改为 ALLOWED_HOSTS = [‘*’]。网上有资料说在可以改为[‘www.example.com’, ‘localhost’, ‘127.0.0.1’],但是在我这里会抛出bad request 400的错误。
修改TEMPLATES 里面的 DIRS
TEMPLATES = [
…
‘DIRS’ = [os.path.join(BASE_DIR, ‘templates’)],
…
]
#404页面
def page_not_found(request):
return render(request, '404.html')
#500页面
def page_error(request):
return render(request, '500.html')
#403页面
def permission_denied(request):
return render(request, '403.html')
在urls.py添加错误页面的显示视图
handler403 = permission_denied
handler404 = page_not_found
handler500 = page_error
DEBUG=false不能看到调试信息,部署的时候要这样设置
默认404可以在urls.py文件里加一行(不要加在urlpatterns里面,加在外面随便什么地方):
handler404 = ‘your_handler’
然后实现这个handler就行了
ALLOWED_HOSTS的说明