关于django:Django之前后端交互

27次阅读

共计 4117 个字符,预计需要花费 11 分钟才能阅读完成。

{% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static'css/reset.css'%}">
#base.html
{% block custom_bread %}
{% endblock %}
#org-list.html
{% block custom_bread %}
<section>
    <div class="wp">
        <ul  class="crumbs">
            <li><a href="{% url'index'%}"> 首页 </a>></li>
            <li> 课程机构 </li>
        </ul>
    </div>
</section>
{% endblock %}
#apps/organizations/views.py
from apps.organizations.models import CourseOrg
class OrgView(View):
    def get(self, request, *args, **kwargs):
        all_orgs = CourseOrg.objects.all()
        return render(request, "org-list.html",{"all_orgs":orgs,})
#templates/org-list.html
{% for org in all_orgs.object_list %}
                <dl class="des difdes">
                    <dt>
                        <a href="{% url'org:home'org.id %}">
                            <img width="200" height="120"  src="{{MEDIA_URL}}{{org.image}}"/>
                        </a>
                    </dt>
                    <dd>
                        <div class="clearfix">
                             <a href="{% url'org:home'org.id%}">
                                 <h1>{{org.org_name}}</h1>
                                 <div class="pic fl">
                                     {% if org.is_auth %}
                                         <img src="{% static'images/authentication.png'%}"/>
                                     {% endif %}
                                     {% if org.is_gold %}
                                         <img src="{% static'images/gold.png'%}"/>
                                     {% endif %}
                                 </div>
                             </a>
                        </div>
                        <ul class="cont">
                            <li class="first"><p class="pic9"> 课程数:<span>{{org.course_nums}}</span></p><p class="c7"> 学习人数:<span>{{org.students}}</span></p></li>
                            <li class="c8" style="padding-left:18px;">{{org.address}}</li>
                            <li class="pic10" style="padding-left:18px;"> 经典课程:{% for course in org.courses %}
                                    <a href="/diary/19/">{{course.cname}}</a>
                                    {% endfor %}
                                    <a href="/diary/19/">c 语言根底 </a>
                                    <a href="/diary/16/"> 数据库根底 </a>

                            </li>
                        </ul>
                    </dd>
                    <div class="buy start_groupbuy jsShowPerfect2" data-id="22"><br/> 分割 <br/> 服务 </div>
                </dl>
            {% endfor %}
#apps/organizations/models.py
class CourseOrg(BaseModel):
    org_name = models.CharField(max_length=50, verbose_name="机构名称")

    desc = models.TextField(verbose_name="形容")
    tag = models.CharField(default="全国出名", max_length=10, verbose_name="机构标签")
    category = models.CharField(default="pxjg", verbose_name="机构类别", max_length=4,
                                choices=(("pxjg", "培训机构"), ("gr", "集体"), ("gx", "高校")))
    click_nums = models.IntegerField(default=0, verbose_name="点击数")
    fav_nums = models.IntegerField(default=0, verbose_name="珍藏数")
    image = models.ImageField(upload_to="org/%Y/%m", verbose_name="logo", max_length=100)
    address = models.CharField(max_length=150, verbose_name="机构地址")
    students = models.IntegerField(default=0, verbose_name="学习人数")

    course_nums = models.IntegerField(default=0, verbose_name="课程数")

    is_auth = models.BooleanField(default=False, verbose_name="是否认证")
    is_gold = models.BooleanField(default=False, verbose_name="是否金牌")

    city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name="所在城市")

    def courses(self):
        from apps.course.models import Course
        courses = Course.objects.filter(course_org=self)
        return courses
    class Meta:
        verbose_name = "课程机构"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.org_name
#apps/course/models.py
from apps.organizations.models import CourseOrg
class Course(BaseModel):
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="讲师")
    course_org = models.ForeignKey(CourseOrg,related_name='org_name+', null=True, blank=True,max_length=50,on_delete=models.SET_NULL, verbose_name="课程机构")
    cname = models.CharField(verbose_name="课程名", max_length=50,default="")
  
    desc = models.CharField(verbose_name="课程形容", max_length=300)
    learn_times = models.IntegerField(default=0, verbose_name="学习时长 ( 分钟数)")
    degree = models.CharField(verbose_name="难度", choices=(("cj", "高级"), ("zj", "中级"), ("gj", "高级")), max_length=2)
    students = models.IntegerField(default=0, verbose_name='学习人数')
    fav_nums = models.IntegerField(default=0, verbose_name='珍藏人数')
    click_nums = models.IntegerField(default=0, verbose_name="点击数")

    notice = models.CharField(verbose_name="课程布告", max_length=300, default="")
    category = models.CharField(default="后端开发", max_length=20, verbose_name="课程类别")
    tag = models.CharField(default="", verbose_name=" 课程标签 ", max_length=10)
    youneed_know = models.CharField(default="", max_length=300, verbose_name=" 课程须知 ")
    teacher_tell = models.CharField(default="", max_length=300, verbose_name=" 老师通知你 ")
    is_classics = models.BooleanField(default=False, verbose_name="是否经典")
    detail = models.TextField(verbose_name="课程详情")
    image = models.ImageField(upload_to="courses/%Y/%m", verbose_name="封面图", max_length=100)
    def courses(self):
        from apps.course.models import Course
        courses = Course.objects.filter(course_org=self)
        return courses
    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cname

正文完
 0