chapter 11 细节

目录

 

11.1 用户退出当前登录,并回到首页

11.2 index 前端页面配置

11.3

11.4全局 404, 500页面配置


11.1 用户退出当前登录,并回到首页

后台代码

user.views.py

from django.urls import reverse
from django.http import HttpResponse,HttpResponseRedirect


class LogoutView(View):
    def get(self,request):
        Logout(request)#退出登陆
        '''
        重定向到首页
        HttpResponseRedirect 是网页重定向
        reverse(from django.urls import reverse)将网页名称反解成网页地址
        '''
        return HttpResponseRedirect(reverse('index'))

urls.py

path('logout/',LogoutView.as_view(), name="logout"),#退出登录

11.11修改当前页面数据:将所有页面的跳转链接,click-nums,fav-nums,等数据配置好

配置课程数据信息

class CourseDetail(View):
    def get(self,request,course_id):
        course = Course.objects.get(id = int(course_id))
        course.click_nums+=1
        fav_count = UserFavorate.objects.filter(fav_type = 1,fav_id=course_id ).count()#当前课程的被收藏次数
        course.fav_nums = fav_count
        '''
        #当前课程的学习人数
        '''
        count = 0
        student_nums = UserCourse.objects.all()
        for student in student_nums:
           if student.course.id == course_id:
               count +=1
        course.student = count
        course.save()#记录点击课程的次数

        course_has_fav = False
        org_has_fav = False
        if request.user.is_authenticated:
            username = request.user
            user = UserProfile.objects.get(Q(username=username) | Q(email=username))
            user_id = user.id
            if UserFavorate.objects.filter(user_id=user_id, fav_id=course.id, fav_type=1):#从数据库中查询
                course_has_fav = True
            if UserFavorate.objects.filter(user_id = user_id ,fav_id = course.course_org.id,fav_type = 2):
                org_has_fav = True
        else:
            username = '未登录,请点击登陆'

        tag = course.tag
        if tag:
            relate_course = Course.objects.filter(tag = tag)
            relate_course = relate_course.exclude(id = course.id)[:1]#將當前課程排除在推薦課程之外
        else:
            relate_course = []
        return render(request,'course-detail.html',{
            'course':course,
            'username': username,
            'relate_course':relate_course,
            'course_has_fav':course_has_fav,
            'org_has_fav':org_has_fav
        })

将用户消息置为已读,即进入message页面就代表用户消息已读(逻辑是不是太简单了一点,我以为还要写ajax)

class UserMsgView(LoginRequiredMixin,View):
    def get(self,request):
        all_messages = UserMessage.objects.filter(user = request.user.id)
        all_unread = UserMessage.objects.filter(user = request.user.id,has_read = False)
        for message in all_unread:
            message.has_read = True
            message.save()

11.2 index 前端页面配置

后台代码

class IndexView(View):
    def get(self,request):
        course_banners = all_courses = Course.objects.all().order_by('-click_nums')[:2]
        all_courses = Course.objects.all().order_by('-click_nums')[:6]
        all_orgs = CourseOrg.objects.all().order_by('-click_nums')[:15]
        banners = Banner.objects.all().order_by('index')[:5]#只要五张

        return render(request,'index.html',{
            'all_courses':all_courses,
            'all_orgs':all_orgs,
            'banners':banners,
            'course_banners':course_banners
        })

首页的View进行了修改

path('', IndexView.as_view(), name="index"),#首页

前端代码

重点代码:

{% for course in all_courses %}
     
{% for org in all_orgs %}
  • 11.3

    修改全局views.py中的 render的index页面,因为从其他地方登陆的首页,拿不到之前首页配置的代码

    原代码
    return render(request, "index.html", {'name': user_name})
    
    
    修改后的代码
    return HttpResponseRedirect(reverse('index'))
    
    
    

    前端判断用户是否登陆

     {% if request.user.is_authenticated %}

     {% if request.user.is_authenticated %}
                            
                            {% else %}
                            
                        {% endif %}

    11.4全局 404, 500页面配置

    出错配置:

     

    django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

    ERRORS:
    ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.

    后台:

    STATIC_URL = '/static/'
    STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]
    
    
    #添加图片路径
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR,'media')
    
    STATIC_ROOT = [os.path.join(BASE_DIR, "static"),]

    这里的全局STATIC_ROOT和上面的STATICFILES_DIR重合了,所以就会出现这样的错

     

    正确的全部配置

    一、sitting里面的配置

    1、将Debug = True 改为Debug= False

    2、配置访问端口  ALLOWED_HOSTS = ['*'],当然还可以有其他的配置方式

    3、STATIC_ROOT就不用配了,直接用STATICFILES_DIR[0],就OK了

    二、工程文件下的urls.py

    from .settings import MEDIA_ROOT,STATICFILES_DIRS
    
    re_path(r'^static/(?P.*)$', serve, { 'document_root': STATICFILES_DIRS[0],}),#配置静态文件路径

    在urls的最下面配置

    #全局404,500页面配置
    handler404 = 'users.views.page_not_found'
    handler500 = 'users.views.page_error'

    user.views.py

    #全局404处理函数
    def page_not_found(request):
        from django.shortcuts import render_to_response
        response = render_to_response('404.html',{})
        response.status_code = 404
        return response
    
    
    #500处理页面
    def page_error(request):
        from django.shortcuts import render_to_response
        response = render_to_response('500.html',{})
        response.status_code = 500
        return response

     

     

     

     

     

     

     

     

    你可能感兴趣的:(Django项目实战)