django视图不显示任何内容

时间:2020-09-24 11:35:59

标签: python django

我正在努力在Django工作中创建一个事件页面。 尽管我尝试加载首页时(从Exhibition_view.html)编写了获取数据库数据的代码,但仅加载了Exhibition.view_html且没有文章返回,也未在该页面中显示。 奇怪的是,workshop_view和Exhibition_view相同,而且workshop_view确实有效! 我为解决该问题做了很多尝试,但不知道发生了什么! 下面是我的代码:

Model.py

from django.db import models
from django.utils import timezone
from tinymce.models import HTMLField


class EventType(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Event Types"


def get_deleted_event_type():
    return EventType.objects.get(name='no-category')


class Event(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    EVENT_CHOICES = (
        ('seminar', 'seminar'),
        ('exhibition', 'exhibition'),
    )
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=250,
                            unique_for_date='publish', allow_unicode=True, unique=True)
    body = HTMLField()
    publish = models.DateTimeField(default=timezone.now)
    created_on = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)
    events = models.ForeignKey(EventType, on_delete=models.SET(get_deleted_event_type))
    event_type = models.CharField(max_length=15,
                                  choices=EVENT_CHOICES,
                                  default='seminar')
    status = models.CharField(max_length=10,
                              choices=STATUS_CHOICES,
                              default='draft')
    event_index = models.IntegerField()

    class Meta:
        ordering = ('-publish',)
        verbose_name_plural = "Events"

    def __str__(self):
        return self.title

Views.py

from django.shortcuts import render
from .models import Event
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q

def seminar_detail(request, slug):
    seminars = Event.objects.get(slug=slug)
    context = {
        'seminars': seminars
    }
    return render(request, "seminar_detail.html", context)

def exhibition_detail(request, events, slug):
    cr1 = Q(events='2')
    cr2 = Q(slug=slug)
    exhibitions = Event.objects.get(cr1 & cr2)
    context = {
        'exhibitions': exhibitions
    }
    return render(request, "exhibition_detail.html", context)


def seminar_view(request):
    criterion1 = Q(status='published')
    criterion2 = Q(event_type="seminar")
    seminars = Event.objects.filter(criterion1 & criterion2).order_by('event_index')
    context = {
        'seminars': seminars
    }
    return render(request, "seminar_view.html", context)

def exhibition_view(request):
    criterion3 = Q(status='published')
    criterion4 = Q(event_type="exhibition")
    exhibitions_view = Event.objects.filter(criterion3 & criterion4).order_by('event_index')
    context = {
        'exhibitions_view': exhibitions_view
    }
    return render(request, "exhibition_view.html", context)

exhibition_view.html

{% extends 'base.html' %}
{% load static %}
<head>
    <title>{% block title %}exhibitions{% endblock %}</title>
</head>
{% block page_content  %}
    <div class="page" id="page-container" >
        <div class="top-bread">
        <div class="breadcrumbs">
            <ul>
                <li class="home">
                    <a href="{% url 'homepage:home' %}" title="go to home">home</a>
                    &nbsp;<span>&raquo;</span>&nbsp;
                </li>
                <li class="socialvoice">
                    <strong>exhibitions</strong>
                </li>
            </ul>
        </div>
    </div>
    </div>
    <div class="page">
        <div class="main-container col1-layout">
            <div class="main">
                <div class="col-main">
                    <div class="shopbybrand-list">
                        <div class="brands">
                            <ul class="col">
                                {% for myevent in exhibitions_view %}
                                    <li class="box">
                                        <span>
                                            <a href="{% url 'events:exhibition_view' event.slug %}">
                                                <b>
                                                    <h2>{{ myevent.title }}<br/>{{ myevent.created_on.date }}</h2>
                                                </b>
                                            </a>
                                        </span>
                                        <b>
                                            <p>
                                            <p style="text-align: justify;">{{ myevent.body | safe | slice:":400" }}</p>
                                            </p>
                                            <a class="button" href="{% url 'events:exhibition_view' myevent.slug %}">
                                                <span>
                                                    <span>ادامه مطلب</span>
                                                </span>
                                            </a>
                                        </b>
                                    </li>
                                {% endfor %}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END page -->
{% endblock %}

和我的应用urls.py:

from django.urls import path
from . import views
from django.urls import re_path

app_name = 'events'

urlpatterns = [
    path('seminar/', views.seminar_view, name="seminar_view"),
    path('exhibition/', views.exhibition_view, name="exhibition_view"),
    re_path(r'seminar/(?P<slug>[-\w]+)/', views.seminar_detail, name="seminar_detail"),
    re_path(r'exhibition/(?P<slug>[-\w]+)/$', views.exhibition_detail, name="exhibition_detail"),
]

0 个答案:

没有答案