簡體   English   中英

Django 2.1將變量傳遞給模板,

[英]Django 2.1 passing a variable to template,

這里有一個關於將變量傳遞到Django模板中的問題。 目標是根據攝影類型過濾出一組照片。 我最初想從S3和它所在的文件夾中執行此操作,但是此刻超出了我的技能范圍。 我只是為此創建了不同的URL而已。 我遇到的問題是,我想將變量傳遞到擴展base_layout.html的模板中,但不會為該變量呈現任何內容。 我只是想念怎么做嗎?

Model.py

from django.db import models


# Create your models here.
class Gallery(models.Model):
    title = models.CharField(max_length = 50)
    body = models.TextField(max_length = 500)
    created = models.DateTimeField(auto_now_add = True)
    thumb = models.ImageField(default = 'default.jpg', blank = True)
    slug = models.SlugField(blank = True)
    order = models.CharField(max_length = 2, blank = True)

    def __str__(self):
        return self.title

    def body_preview(self):
        return self.body[:50]

class photoUrl(models.Model):
    url = models.CharField(max_length = 128)
    uploaded_on = models.DateTimeField(auto_now_add = True)

class Photos(models.Model):
    title = models.CharField(max_length = 50)
    picture = models.ImageField(blank = True)
    created = models.DateTimeField(auto_now_add = True)
    catagory = models.CharField(max_length=256, choices=[('wedding', 'wedding'), ('portrait', 'portrait'), ('landscape', 'landscape'), ('boudoir', 'boudoir'),], blank = True)

    def __str__(self):
        return self.title

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
from . models import Photos

# Create your views here.


def photo_wedding(request):
    photo_list = Photos.objects.filter(catagory = 'wedding').order_by('created')
    photoCat = 'Wedding'
    return render(request, 'gallery/gallery.html', {'photo_list' : photo_list}, {'photoCat' : photoCat})

urls.py

from django.contrib import admin
from django.urls import path
from . import views

app_name='gallery'

urlpatterns = [
    path('wedding/', views.photo_wedding, name='wedding'),
    path('portrait/', views.photo_portrait, name='portrait'),
    path('landscape/', views.photo_landscape, name='landscape'),
    path('boudoir/', views.photo_boudoir, name='boudoir'),
]

gallery.html

{% extends 'gallery/base_layout.html' %}

{% load static %}

{% block gallery %}

    <div class="gallery" id="gallery">
        <div class="container">
            <div class="w3l-heading">

                <h3>{{photoCat}}</h3>

                <div class="w3ls-border"> </div>
            </div>
        </div>
{% endblock gallery %}

render的定義:

render(request, template_name, context=None, content_type=None, status=None, using=None)將給定的模板與給定的上下文字典合並,並返回帶有該渲染文本的HttpResponse對象。

render方法將第一個參數作為request ,第二個參數作為template_name ,第三個參數是一個context ,該context是您選擇傳遞給模板的Dictionary類型,您可以使用鍵訪問dictionary的所有值。

因此,您的方法應如下所示:

def photo_wedding(request):
    photo_list = Photos.objects.filter(catagory = 'wedding').order_by('created')
    photoCat = 'Wedding'
    return render(request, 'gallery/gallery.html', {'photo_list' : photo_list, 'photoCat' : photoCat})

你為什么要通過兩個字典。 只需添加一個密鑰。 那就是上下文數據。

在基於類的視圖中,您還可以重載方法get_context_data

使用render()函數,第三個參數是上下文。 上下文是用於將變量發送到模板的字典。 無需通過兩個命令

def photo_wedding(request):
    photo_list = Photos.objects.filter(catagory = 'wedding').order_by('created')
    photoCat = 'Wedding'
 context = {'photo_list' : photo_list,'photoCat' : photoCat}
    return render(request, 'gallery/gallery.html', context)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM