python,django代码在html中不起作用

时间:2018-01-04 00:30:51

标签: python html django

我的文章没有出现在页面上,为什么。在其他html文件中,它们出现。

  

product.html

<aside>
        <div id="news">
            <h2 class="heading">News</h2>
            <div style="clear: both"><br></div>
            {% for articles in news_articles %}
            <div id="articles">
                <div class="article">
                <a href="{{ articles.article.get_absolute_url }}">
                <img src="{{ articles.image }}">
                    <div style="clear: both"></div>
                <span></span><div style="clear: both"></div>
                </a>
                <em>{{ articles.article.created }}</em>
            <div style="clear: both"><br></div>
                </div>
            {% endfor %}
            </div>
            <a href="" title="View More Articles">
                <div id="btn">
                    <span>View More</span>
                </div>
            </a>
        </div>
    </aside>

这是我的views.py文件代码部分

  

views.py

from django.shortcuts import render
from products.models import *
from news.models import *
from django.shortcuts import get_object_or_404
from django.views.generic.detail import DetailView

class GameLink(DetailView):
    model = Product
    context_object_name = 'product'

    def get_object(self):
        return get_object_or_404(Product, slug__iexact=self.kwargs['slug'])


class ArticleLink(DetailView):
    model = Article
    context_object_name = 'article'

    def get_object(self):
        return get_object_or_404(Article, slug__iexact=self.kwargs['slug'])

def product(request, slug):
    product = Product.objects.get()
    product_images = ProductImage.objects.filter(is_active=True, is_main=False, product=product_id)
    links = ProductDownload.objects.filter(is_active=True, product=slug)
    news_articles = NewsImage.objects.filter(is_active=True, is_main=True)
    return render(request, 'products/product.html', locals())

def new(request, product_id):
    article = Article.objects.get(id=product_id)
    news_articles = NewsImage.objects.filter(is_active=True, is_main=True)
    return render(request, 'news/article.html', locals())

这是我的models.py文件代码部分

  

models.py

class Product(models.Model):
    name = models.CharField(max_length=128, blank=True, null=True, default=None)
    description = models.TextField(default=None)
    processor = models.CharField(max_length=300, blank=True, null=True, default=None)
    video = models.CharField(max_length=300, blank=True, null=True, default=None)
    ram = models.CharField(max_length=300, blank=True, null=True, default=None)
    disk_space = models.CharField(max_length=300, blank=True, null=True, default=None)
    oS = models.CharField(max_length=300, blank=True, null=True, default=None)
    video_trailer = models.CharField(max_length=10000, blank=True, null=True, default=None)
    img = models.CharField(max_length=10000, blank=True, null=True, default=None)
    category = models.ManyToManyField(ProductCategory, blank=True, default=None)
    is_active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField(primary_key=True, max_length=250, unique=True, default=None)

    def __str__(self):
        return '%s' % self.name


    def get_absolute_url(self):
        return reverse('product', args=[str(self.slug)])
class ProductImage(models.Model):
    product = models.ForeignKey(Product, blank=True, null=True, default=None, on_delete=False)
    image = models.CharField(max_length=10000, blank=True, null=True, default=None)
    is_main = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return '%s' % self.product

    class Meta:
        verbose_name = 'Image'
        verbose_name_plural = 'Images'
  

urls.py

from django.contrib import admin
from django.conf.urls import *
from products import views
from products.views import *

urlpatterns = [
   # url(r'^games/(?P<product_id>\w+)/$', views.product, name='product')
   url(r'^games/(?P<slug>[-\w]+)/$', GameLink.as_view(template_name = 'products/product.html'), name='product'),
   url(r'^articles/(?P<slug>[-\w]+)/$', ArticleLink.as_view(template_name = 'news/article.html'), name='article')
]

This are my file paths in project

我认为问题出在我的views.pyurls.py,但我不确定。

1 个答案:

答案 0 :(得分:2)

您传递给模板的唯一数据是locals(),它将是您的本地变量。我建议将特定变量传递给您的模板,如下所示:

return render(request, 'news/article.html', { 'news_articles': news_articles, 'article': article})

现在它更容易阅读,您可以看到您的变量如何从new()传递到您的article.html模板以及它在此模板上的工作原理。

news_articles无法在您的product.html模板上运行,因为您没有在product()中设置本地变量。当您将locals()传递给此模板时,您将发送:

return render(request, 'products/product.html', { 'product_images': product_images, 'product': product, 'links': links})

将news_articles变量添加到product()并将其添加到渲染以在模板上访问它们。