首页 > 代码库 > Django学习

Django学习

from django.conf.urls import url
from django.contrib import admin
from blog.views import index,student,student1,video

urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^index/, index),
    url(r^student/,student),
    url(r^student1/,student1),
    url(r^video-(?P<direction_id>\d+)-(?P<class_id>\d+)-(?P<level_id>\d+).html,video),
]

simpletag

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django import template
from django.utils.safestring import mark_safe

register = template.Library()


@register.simple_tag
def my_simple_time(v1, v2, v3):
    return v1 + v2 + v3

@register.simple_tag
def detail1(item,counter,allcount,remainder):
    temp = ‘‘‘
    <div style="width: 205px;">
            <img style="width: 205px;height: 220px" src="http://www.mamicode.com/%s">
            <p>%s</p>
            <p>%s</p>
            <p>%s</p>
            <p>%s</p>
    </div>
    ‘‘‘
    if counter%allcount ==remainder:
        temp = temp % (
            item[student__pic],
            item[student__name],
            item[student__salary],
            item[student__company],
            item[litter_thanks],
        )
        return mark_safe(temp)
    else:
        return ‘‘

@register.filter
def detail2(value,args):
    allcount,remainder = args.split(,)
    allcount = int(allcount)
    remainder = int(remainder)
    if value%allcount ==remainder:
        return True
    return False

@register.simple_tag
def action(current_url,item,index):
    url_list = current_url.split(-)
    if index ==3:
        if str(item[id]) ==url_list[index].split(.)[0]:
            temp = "<a href=http://www.mamicode.com/%s class=‘active‘>%s"
        else:
            temp = <a href=http://www.mamicode.com/%s>%s
        url_list[index] = str(item[id])+.html
    else:
        if str(item[id]) == url_list[index]:
            temp = "<a href=http://www.mamicode.com/%s class=‘active‘>%s"
        else:
            temp = <a href=http://www.mamicode.com/%s>%s
        url_list[index] = str(item[id])
    url_str = -.join(url_list)
    temp = temp%(url_str,item[name])
    return mark_safe(temp)

@register.simple_tag
def action_all(current_url,index):
    url_list = current_url.split(-)
    if index ==3:
        if url_list[index]==0.html:
            temp = "<a href=http://www.mamicode.com/%s class=‘active‘>全部"
        else:
            temp = "<a href=http://www.mamicode.com/%s >全部"
        url_list[index]= 0.html
    else:
        if url_list[index] == 0:
            temp = "<a href=http://www.mamicode.com/%s class=‘active‘>全部"
        else:
            temp = "<a href=http://www.mamicode.com/%s >全部"
        url_list[index] = 0

    href = -.join(url_list)
    temp = temp % (href,)
    return mark_safe(temp)

admin

from django.contrib import admin
from blog import models
# Register your models here.
admin.site.register(models.BxSlider)
admin.site.register(models.Classification)
#admin.site.register(models.Course)
admin.site.register(models.Direction)
#admin.site.register(models.Notice)
#admin.site.register(models.Recruit)
admin.site.register(models.Student)
admin.site.register(models.Studentdetail)
admin.site.register(models.Video)

models

from django.db import models

# Create your models here.
class BxSlider (models.Model):
    status_choice = (
        (0, 下线),
        (1, 上线),
    )
    status = models.IntegerField(choices=status_choice,default=1)
    name = models.CharField(max_length=32,db_index=True,unique=True)
    img = models.ImageField(upload_to=./static/imgs/focus/)
    href = models.CharField(max_length=256)
    creat_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = BxSlider
        verbose_name_plural = 首页轮播
    def __str__(self):
        return self.name

class Notice(models.Model):
    status_choice=(
        (0,不显示),
        (1,显示),
    )
    status = models.IntegerField(verbose_name=状态,choices=status_choice,default=0)
    weight = models.IntegerField(verbose_name=权重(按从大到小排列),default=0)
    title = models.CharField(verbose_name=标题,max_length=32)
    content = models.CharField(verbose_name=简介,max_length=256)
    detail = models.TextField(verbose_name=公告详细,null=True,blank=True)
    class Meta:
        db_table=Notice
        verbose_name_plural=最新公告(开班信息等)
    def __str__(self):
        return self.title

class Course(models.Model):
    status_choice=(
        (0,下线),
        (1,上线),
    )
    status = models.IntegerField(verbose_name=状态,choices=status_choice,default=1)
    weight = models.IntegerField(verbose_name=权重(按从大到小排列),default=0)
    icon = models.ImageField(verbose_name=图标,upload_to=./static/imgs/icon,null=True,blank=True)
    name = models.CharField(verbose_name=名称,max_length=32,db_index=True,unique=True)
    summary = models.CharField(verbose_name=简介,max_length=46,default=summary)

    class Meta:
        db_table=Course
        verbose_name_plural=课程
    def __str__(self):
        return self.name

class Recruit(models.Model):
    status_choice=(
        (0,已过期),
        (1,招聘中),
    )
    status = models.IntegerField(verbose_name=状态,choices=status_choice,default=1)
    weight = models.IntegerField(verbose_name=权重(按从大到小排列),default=0)
    title = models.CharField(verbose_name=标题,max_length=32)
    salary = models.CharField(verbose_name=薪水,max_length=25)
    conpany = models.CharField(max_length=20)
    detail = models.TextField(verbose_name=详细)
    deadline=models.DateField()
    class Meta:
        db_table=Recruit
        verbose_name_plural=招聘信息
    def __str__(self):
        return self.title

class Student(models.Model):
    status_choice = (
        (0, 不显示),
        (1, 显示),
    )
    status = models.IntegerField(verbose_name=状态,choices=status_choice,default=0)
    weight = models.IntegerField(verbose_name=权重(按从大到小排列),default=0)
    name = models.CharField(verbose_name=名称,max_length=32,db_index=True,unique=True)
    pic = models.ImageField(verbose_name=头像,upload_to=./static/imgs/stu_pic/,null=True)
    company = models.CharField(verbose_name=就业单位, max_length=32)
    salary = models.CharField(verbose_name=薪水, max_length=25)
    class Meta:
        db_table=Student
        verbose_name_plural=学生信息
    def __str__(self):
        return self.name

class Studentdetail(models.Model):
    student = models.OneToOneField(Student)
    weight = models.IntegerField(verbose_name=权重(按从大到小排列),default=0)
    litter_thanks= models.CharField(verbose_name=感谢信,max_length=256)
    class Meta:
        db_table=Studentdetail
        verbose_name_plural=学生更多信息
    def __str__(self):
        return self.student.name

class Classification(models.Model):
    weight = models.IntegerField(verbose_name=权重(按从大到小排列), default=0)
    name = models.CharField(verbose_name=名称, max_length=32)
    class Meta:
        db_table=Classification
        verbose_name_plural=课程视频分类
    def __str__(self):
        return self.name

class Video(models.Model):
    status_choice=(
        (0,下线),
        (1,上线),
    )
    level_choice=(
        (1,初级),
        (2,中级),
        (3,高级),
    )
    status = models.IntegerField(verbose_name=状态,choices=status_choice,default=1)
    level = models.IntegerField(verbose_name=状态, choices=level_choice, default=1)
    weight = models.IntegerField(verbose_name=权重(按从大到小排列),default=0)
    img = models.ImageField(verbose_name=图像,upload_to=./static/imgs/video/,null=True,blank=True)
    name = models.CharField(verbose_name=名称,max_length=32,db_index=True,unique=True)
    summary = models.CharField(verbose_name=简介,max_length=46,default=summary)
    title = models.CharField(verbose_name=标题, max_length=32)
    href = models.CharField(max_length=256)
    creat_date = models.DateTimeField(auto_now_add=True)
    classification = models.ForeignKey(Classification,null=True,blank=True)
    class Meta:
        db_table=Video
        verbose_name_plural=课程视频
    def __str__(self):
        return self.title

class Direction(models.Model):
    weight = models.IntegerField(verbose_name=权重(按从大到小排列), default=0)
    name = models.CharField(verbose_name=名称, max_length=32)
    classification = models.ManyToManyField(Classification)
    class Meta:
        db_table=Direction
        verbose_name_plural=视频方向
    def __str__(self):
        return self.name

views

from django.shortcuts import render,HttpResponse
from blog import models
import json
# Create your views here.
def index(request):
    queryset_dict = models.BxSlider.objects.filter(status=1).values(img,href,name)
    return render(request,index.html,{queryset_dict:queryset_dict})

def student(request):
    #student
    # studentdetail
    detail_list = models.Studentdetail.objects.filter(student__status=1).values(litter_thanks,student__name,student__company,student__salary,student__pic)
    print(detail_list)
    return render(request,student.html,{detail_list:detail_list})

def student1(request):
    if request.method ==POST:
        detail_list = models.Studentdetail.objects.filter(student__status=1).values(litter_thanks, student__name,
                                                                                    student__company,
                                                                                    student__salary, student__pic)
        detail_list = list(detail_list)
        return HttpResponse(json.dumps(detail_list))
    #student
    # studentdetail
    #detail_list = models.Studentdetail.objects.filter(student__status=1).values(‘litter_thanks‘,‘student__name‘,‘student__company‘,‘student__salary‘,‘student__pic‘)

    return render(request,student1.html)

def video(request,*args,**kwargs):
    current_url = request.path
    q = {}
    q[status] = 1
    drection_list = models.Direction.objects.all().values(id,name)
    if kwargs.get(direction_id)==0:
        classifi_list = models.Classification.objects.all().values(id, name)
        if kwargs.get(class_id)==0:
            pass
        else:
            q[classification_id__in] = [kwargs.get(class_id),]
    else:
        if kwargs.get(class_id)==0:
            obj = models.Direction.objects.get(id = int(kwargs.get(direction_id)))
            classifi_list = obj.classification.all().values(id, name)
            id_list = list(map(lambda x: x[id], classifi_list))
            q[classification_id__in]=id_list
        else:
            obj = models.Direction.objects.get(id=int(kwargs.get(direction_id)))
            classifi_list = obj.classification.all().values(id, name)
            id_list = list(map(lambda x:x[id],classifi_list))
            q[classification_id__in] = [kwargs.get(class_id),]
            if int(kwargs.get(class_id)) in id_list:
                pass
            else:
                url_part_list = current_url.split(-)
                url_part_list[2]=0
                current_url = -.join(url_part_list)
    level_id = int(kwargs.get(level_id))
    if level_id ==0:
        pass
    else:
        q[level]= level_id
    video_list = models.Video.objects.filter(**q).values(title,summary,img,href)

    ret = map(lambda x:{id:x[0],name:x[1]},models.Video.level_choice)
    leve_list =list(ret)
    print(kwargs)

    return render(request, video.html,{drection_list:drection_list,
                                         classifi_list:classifi_list,
                                         leve_list:leve_list,
                                         current_url:current_url,
                                         video_list:video_list})

video.html

{% load xxx %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
            padding: 5px;
            display: inline-block;
        }
        .active{
            background-color: orange;
            color: white;
        }
        .item{
            display: inline-block;
            width: 205px;
            height: 240px;
        }
        .item img{
            border: 0;
            width:205px;
            height:240px;
        }
    </style>
</head>
<body>
<h3>选择:</h3>
<hr/>
<div>
    {% action_all current_url 1 %}
    {% for item in drection_list %}
        {% action current_url item 1 %}
    {% endfor %}
</div>
<div>
    {% action_all current_url 2 %}
    {% for item in classifi_list %}
        {% action current_url item 2 %}
    {% endfor %}
</div>
<div>
    {% action_all current_url 3 %}
    {% for item in leve_list %}
        {% action current_url item 3 %}
    {% endfor %}
</div>
<h3>视频:</h3>
<hr/>
{% for item in video_list %}
<a class="item" href="{{ item.href }}">
    <img src="/{{ item.img }}"/>
    <p>{{ item.title }}</p>
    <p>{{ item.summary }}</p>
</a>
{% endfor %}
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="/static/plugins/bxslider/jquery.bxslider.css" rel="stylesheet"/>
</head>
<body>
<ul class="bxslider">
    {% for item in queryset_dict %}
    <li>
        <a href="{{ item.href }}">
            <img src="/{{ item.img }}" alt="{{ item.name }}">
        </a>
    </li>
    {% endfor %}
</ul>
<script src="/static/js/jquery-3.2.1.min.js"></script>
<script src="/static/plugins/bxslider/jquery.bxslider.min.js"></script>
<script>
    $(document).ready(function(){
    $(.bxslider).bxSlider();
});
</script>
</body>
</html>

student

{% load xxx %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    .clearfix:after{
        content: ‘.‘;
        visibility:hidden;
        height: 0;
        clear: both;
        display: block;
    }
    </style>
</head>
<body>

<div style="margin: 0 auto;width: 980px" class="clearfix">
    <div style="width: 245px;float:left;">
        {% for item in detail_list %}
            {% detail1 item forloop.counter 4 1 %}
        {% endfor %}
    </div>
    <div style="width: 245px;float:left;">
        {% for item in detail_list %}
            {% detail1 item forloop.counter 4 2 %}
        {% endfor %}
    </div>
    <div style="width: 245px;float:left;">
        {% for item in detail_list %}
            {% detail1 item forloop.counter 4 3 %}
        {% endfor %}
    </div>
    <div style="width: 245px;float:left;">
        {% for item in detail_list %}
            {% if forloop.counter|detail2:‘4,0‘ %}
            <div style="width: 205px;">
                <img style="width: 205px;height: 220px" src="/{{ item.student__pic }}">
                <p>{{ item.student__name }}</p>
                <p>{{ item.student__salary }}</p>
                <p>{{ item.student__company }}</p>
                <p>{{ item.litter_thanks }}</p>
            </div>
            {% endif %}
        {% endfor %}
    </div>
</div>

</body>
</html>

 

Django学习