项目目录下配置urls
from users.views import IndexView
UsersApp下编写View视图函数
class IndexView(View): """ X.xx在线网首页 """ def get(self, request): # 取出轮播图 all_banners = Banner.objects.all().order_by('index') courses = Course.objects.filter(is_banner=False)[:6] banner_courses = Course.objects.filter(is_banner=True)[:3] courses_orgs = CourseOrg.objects.all()[:15] return render(request, 'index.html', { 'all_banners': all_banners, 'courses': courses, 'banner_courses': banner_courses, 'courses_orgs': courses_orgs })
配置首页显示:
轮播图显示:
<div class="banner"> <div class="wp"> <div class="fl"> <div class="imgslide"> <ul class="imgs"> {% for banner in all_banners %} <li> <a href="{{ banner.url }}"> <img width="1200" height="478" src="{{ MEDIA_URL }}{{ banner.image }}" /> a> li> {% endfor %} ul> div> <div class="unslider-arrow prev">div> <div class="unslider-arrow next">div> div> div> div>
首页课程轮播显示:
<div class="right group_list"> <div class="module1_2 box"> <div class="imgslide2"> <ul class="imgs"> {% for banner_course in banner_courses %} <li> <a href="{% url 'course:course_detail' banner_course.id %}"> <img width="470" height="300" src="{{ MEDIA_URL }}{{ banner_course.image }}" /> a> li> {% endfor %} ul> div> <div class="unslider-arrow2 prev">div> <div class="unslider-arrow2 next">div> div>
{% for course in courses %} <div class="module1_{{ forloop.counter|add:2 }} box"> <a href="{% url 'course:course_detail' course.id %}"> <img width="233" height="190" src="{{ MEDIA_URL }}{{ course.image }}"/> a> <div class="des"> <a href="course-detail.html"> <h2 title="django入门">{{ course.name }}h2> a> <span class="fl">难度:<i class="key">{{ course.get_degree_display }}i>span> <span class="fr">学习人数:{{ course.students }}span> div> <div class="bottom"> <span class="fl" title="{{ course.course_org.name }}">{{ course.course_org.name }}span> <span class="star fr">{{ course.fav_nums }}span> div> div> {% endfor %} div>
首页课程机构显示:
<section> <div class="module greybg"> <div class="wp"> <h1>课程机构h1> <div class="module3 eachmod"> <div class="module3_1 left"> <img width="228" height="463" src="/static/images/module3_1.jpg"/> <p class="fisrt_word">名校来袭<br/>权威认证p> <a class="more" href="{% url 'org:org_list' %}">查看更多机构 >a> div> <div class="right"> <ul> {% for org in courses_orgs %} <li class="{% if forloop.counter|divisibleby:5 %}five{% endif %}"> <a href="{% url 'org:org_home' org.id %}"> <div class="company"> <img width="184" height="100" src="{{ MEDIA_URL }}{{ org.image }}"/> <div class="score"> <div class="circle"> <h2>{{ org.tag }}h2> div> div> div> <p><span class="key" title="{{ org.name }}">{{ org.name }}span>p> a> li> {% endfor %} ul> div> div> div> div> section>
在退出重新登录之后页面显示不正常需要重新编写LoginView函数:
from django.core.urlresolvers import reverse
class LoginView(View): """ 基础类的登录方法 """ def get(self, request): # 重写View的get方法 return render(request, 'login.html', {}) def post(self, request): # 实例化LoginForm传值request.POST(是字典形式)会先传入forms进行验证 login_form = LoginForm(request.POST) # is_valid()方法会先进行判断用户名密码 如果errors为空则进行下一步 if login_form.is_valid(): user_name = request.POST.get('username', '') pass_word = request.POST.get('password', '') # authenticate方法只进行验证 user = authenticate(username=user_name, password=pass_word) if user is not None: if user.is_active: # login方法实现登录 login(request, user) return HttpResponseRedirect(reverse('index')) else: return render(request, 'login.html', { 'msg': '用户未激活', }) else: return render(request, 'login.html', { 'msg': '用户名或密码错误', }) else: return render(request, 'login.html', { 'login_form': login_form, })主要是这句
return HttpResponseRedirect(reverse('index'))