我在django中制作了此应用,以便在Django中显示Category。我为此功能制作了视图和urls应用程序,但是没有用。我不知道为什么它不起作用。我尝试了很多不同的代码来解决它,但是它们没有用。
Urls.py
from django.conf.urls import url
from . import views
from django.contrib import admin
from django.views.generic import ListView, DetailView
from django.contrib.staticfiles.urls import
staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='index'), #Index
url(r'^all-articles$', views.PostList, name='PostList'), #All
Posts
url(r'^category$', views.Category, name='Category'), #All
CAtegory
path('<slug:slug>/', views.PostDetail.as_view(),
name='post_detail'), #Single Posts
]
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
Views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
from django.views.generic import View
from django.views import generic
def index(request): #HomePage
return render(request, 'blog/index.php', {})
# Post Section
def PostList(request): #Articles List
articles = Article.objects.all().order_by('data')
return render(request, 'blog/post/PostList.php',
{'articles':articles})
class PostDetail(generic.DetailView): #Single Article
model = Article
template_name = 'blog/post/post_detail.php'
def Category(request): #Category List
category = Category.objects.all()
return render(request, 'blog/post/category.php',
{'category':category})
Models.py
from django.db import models
from django import forms
from django.contrib.auth.models import User
from django.urls import reverse
#Categorie
class Category(models.Model):
class Meta:
verbose_name = 'category'
verbose_name_plural = 'categorie'
name = models.CharField('Titolo', max_length = 250)
slug = models.SlugField(max_length = 250, unique = True)
def __str__(self):
return self.name
#Articles
class Article(models.Model):
class Meta:
verbose_name = 'Articolo'
verbose_name_plural = 'Articoli'
'''
Classe per creare articoli generali con media
'''
title = models.CharField('Titolo', max_length=100)
author = models.ForeignKey(User, on_delete=models.CASCADE,)
category = models.ForeignKey (Category,
on_delete=models.CASCADE)
desc = models.CharField('Descrizione', max_length=10000,
blank=True)
text = models.TextField('Testo', max_length=10000, blank=True)
image = models.ImageField('Foto', blank=True, upload_to="img")
data = models.DateTimeField('Data di pubblicazione', blank=True)
slug = models.SlugField(max_length = 250, null = True, blank =
True, unique=True)
class Meta: #ordinare i post secondo la data
ordering = ['-data',]
def __str__(self):
return "Crea un nuovo articolo"