Django+Xadmin打造在线教育系统(八)
首页和全局404,500配置
- 轮播图
- 公开课
- 授课机构
新建view
## 首页view
class IndexView(View):
def get(self,request):
# 取出轮播图
all_banner = Banner.objects.all().order_by('index')[:5]
# 正常位课程
courses = Course.objects.filter(is_banner=False)[:6]
# 轮播图课程取三个
banner_courses = Course.objects.filter(is_banner=True)[:3]
# 课程机构
course_orgs = CourseOrg.objects.all()[:15]
return render(request, 'index.html', {
"all_banner":all_banner,
"courses":courses,
"banner_courses":banner_courses,
"course_orgs":course_orgs,
})
为Course添加字段: isbanner
is_banner = models.BooleanField(default=False, verbose_name="是否轮播")
CourseOrg添加一个字段
tag = models.CharField(max_length=10, default= "国内名校",verbose_name="机构标签")
前端页面
{% block content %}
<div class="banner">
<div class="wp">
<div class="fl">
<div class="imgslide">
<ul class="imgs">
{% for banner in all_banner %}
<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>
<section>
<div class="wp">
<ul class="feature">
<li class="feature1">
<img class="pic" src="{% static "images/feature1.png" %}">
<p class="center">专业权威</p>
</li>
<li class="feature2">
<img class="pic" src="{% static "images/feature2.png" %}">
<p class="center">课程最新</p>
</li>
<li class="feature3">
<img class="pic" src="{% static "images/feature3.png" %}">
<p class="center">名师授课</p>
</li>
<li class="feature4">
<img class="pic" src="{% static "images/feature4.png" %}">
<p class="center">数据真实</p>
</li>
</ul>
</div>
</section>
<section>
<div class="module">
<div class="wp">
<h1>公开课程</h1>
<div class="module1 eachmod">
<div class="module1_1 left">
<img width="228" height="614" src="{% static "images/module1_1.jpg" %}">
<p class="fisrt_word">名师授课<br/>专业权威</p>
<a class="more" href="{% url 'course:list' %}">查看更多课程 ></a>
</div>
<div class="right group_list">
<div class="module1_2 box">
<div class="imgslide2">
<ul class="imgs">
{% for cours in banner_courses %}
<li>
<a href="{% url 'course:course_detail' cours.id %}">
<img width="470" height="300" src="{{ MEDIA_URL }}{{ cours.image }}">
</a>
</li>
{% endfor %}
</ul>
</div>
<div class="unslider-arrow2 prev"></div>
<div class="unslider-arrow2 next"></div>
</div>
{% for cours in courses %}
<div class="module1_{{ forloop.counter|add:2 }} box">
<a href="{% url 'course:course_detail' cours.id %}">
<img width="233" height="190" src="{{ MEDIA_URL }}{{ cours.image }}">
</a>
<div class="des">
<a href="{% url 'course:course_detail' cours.id %}">
<h2 title="{{ cours.name }}">{{ cours.name }}</h2>
</a>
<span class="fl">难度:<i class="key">{{ cours.get_degree_display }}</i></span>
<span class="fr">学习人数:{{ cours.students }}</span>
</div>
<div class="bottom">
<span class="fl"
title="{{ cours.course_org.name }}">{{ cours.course_org.name }}</span>
<span class="star fr">{{ cours.fav_nums }}</span>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</div>
</section>
<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 course_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>
{% endblock %}
{% block custom_js %}
<script type="text/javascript" src="{% static "js/index.js" %}"></script>
{% endblock %}
说明1:课程
课程分is_banner=False
和is_banner=True
,两种课程的class属性不一样
is_banner=True
的class是class="module1_2 box"
is_banner=False
的class是class="module1_3 box"
,
所以这里要
class="module1_{{ forloop.counter|add:2 }}
说明2:课程机构
课程机构的class分为class=""
和class="five"
这里要做个判断,
class="{% if forloop.counter|divisibleby:5 %}five{% endif %}
divisibleby
过滤器:能不能整除
配置全局404和500
关于全局404和500,
如果不进行配置,Django会自定调用templates下的404.html和500.html
https://docs.djangoproject.com/en/2.1/ref/urls/#handler404
根目录下的urls.py文件MxOnline/urls.py
# 全局404页面配置
handler404 = 'users.views.pag_not_found'
# 全局500页面配置
handler500 = 'users.views.page_error'
# 404对应处理view
def page_not_found(request):
from django.shortcuts import render_to_response
response = render_to_response("404.html", {
})
# 设置response的状态码
response.status_code = 404
return response
Debug = True 时 404是不起作用的
ALLOWED_HOSTS = ['*']
在debug为false情况下。
我们在访问media的时候配置过用serve来取
告诉它访问media的时候去哪个路径下找
debug为True
会自动前往STATICFILES_DIRS
取文件的
一旦debug改为false,django就不会代管你的静态文件,
一般静态文件通过第三方http服务器代理转发。
nignx 和 Apache都会自动代理这些静态文件
方法:我们自己url响应我们的static
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
re_path(r'^static/(?P<path>.*)', serve, {"document_root": STATIC_ROOT }),
# 全局500处理函数
def page_error(request):
from django.shortcuts import render_to_response
response = render_to_response('500.html', {})
response.status_code = 500
return response