categories.html#這就是我想被CategoryView.as_view()
調用的那種從不起作用的東西。如何index.html工作,但我已經在index.html中如下硬編碼鏈接。儘管index.html擴展了base.html,它是輸出url的地方。 編輯:避免混淆,這是在我的猜測應該工作,但是從我的根目錄現在Page not found (404)
儘管我努力學習Django模板,但它仍然令人失望
<li><a href="{% url 'all_categories' %}">All</a></li>
顯示指數以下錯誤index.html的模板可當我鍵入http://127.0.0.1:8000它的工作原理,但我想要的東西像這樣http://127.0.0.1:8000/cartegories/
(index.html
或categories.html
)我認爲index.html或categories.html在輸入時不可見,這很好。
我不能相信這是帶我12小時,所有可能的直觀調整仍然無法讓它工作。請有人建議做錯了什麼。在essense我只是outputing hellow世界categories.html但以後我會遍歷並列出所有類別在我的博客...如果hello world
不能工作,那麼什麼都不會..
#posts urls.py
from django.conf.urls import include, url
from django.contrib import admin
from .views import IndexView, CategoryView
urlpatterns = [
url(r'^$', IndexView.as_view(), name='index'),
url(r'^all_categories$', CategoryView.as_view(), name='all_categories'),
# stuff i have tried
]
#admin urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^/', include('posts.urls', namespace="posts")),
url(r'^admin/', include(admin.site.urls)),
]
#views.py
from django.shortcuts import render
from .models import Post, Comment, Category, Reply
from django.views import generic
class IndexView(generic.ListView):
template_name = 'posts/index.html'
context_object_name = 'posts'
model = Post
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['categories'] = Category.objects.all()
return context
class CategoryView(generic.ListView):
template_name = 'posts/categories.html'
context_object_name = 'categories'
model = Category
#modesl.py
class Category(models.Model):
category_name = models.CharField(max_length=200)
class Meta:
verbose_name_plural = "categories"
def __str__(self):
return self.category_name
def get_absolute_url(self):
return reverse('all_categories')
class Post(models.Model):
title = models.CharField(max_length=200)
…
category = models.ForeignKey(Category)
def save(self, *args, **kargs):
if not self.id:
self.slug = slugify(self.title)
super(Post, self).save(*args, **kargs)
def __str__(self):
return self.title