我是初學者到Django。我面臨這個問題,我無法在年,月和後發佈帖子詳情視圖。 錯誤是post_detail()
缺少4個必需的位置參數:「年」,「月」,「日」和「發佈」。post_detail()視圖缺少必需的位置參數:
這是models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
class Post(models.Model):
STAUTS_CHOICE = (('draft','Draft'),('published','Published'),)
title =models.CharField(max_length =250)
slug = models.SlugField(max_length =250)
author = models.ForeignKey(User,related_name='blog_post')
body = models.TextField()
publish =models.DateTimeField(default = timezone.now)
created = models.DateTimeField(auto_now_add=True)
udated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length =250 ,
choices = STAUTS_CHOICE , default = 'draft')
class Meta:
ordering = ('-publish',)
def get_absolute_url(self):
return reverse('blog:post_detail', args = [self.slug, self.publish.year , self.publish.strftime('%m'), self.publish.strftime('%d')]])
def __str__(self):
return self.title
______________________________________________________________
這是views.py
from django.shortcuts import render , get_object_or_404
from .models import Post
def post_list(request):
post = Post.objects.all()
return render(request,'blog/post/list.html',{'post':post})
def post_detail(request,slug,year, month,day,post):
post = get_object_or_404(Post, slug = slug , status = 'published' ,publish_year = year, publish_month = month , publish_day = day)
return render(request,'blog/post/detail.html',{'post':post})
這是urls.py
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^post_list',views.post_list,name ='post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'r'(?P<slug>[-\w]+)/$',views.post_detail,name='post_detail'),
這是
post_detail.html頁{% extends 'blog/base.html' %}
{% block title %}Post details{% endblock %}
{% block content %}
<h2><a>{{post.title}}</a></h2>
<p class="date">published {{ post.published}} by {{ post.author}}</p>
{{ post.body|linebreaks }}
{% endblock %}
這是list.html頁面:
{% extends 'blog/base.html' %} {% block head_title %}Posts list{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for x in posts %}
<h2>
<a href="{{ x.get_absolute_url }}">{{x.title}}</a>
</h2>
<p class="date">published {{ x.published}} by {{ x.author}}</p>
{{ x.body|truncatewords:30|linebreaks }} {% endfor %} {% endblock %}
這是base.html文件從post_detail視圖
{% load staticfiles %}
<!DOCTYPE html>
<head>
<title>{% block head_title %}Welcome to my blog{% endblock %}</title>
<!-- <link rel="stylesheet" href=""> -->
</head>
<body>
<div id="sidebar">
<h1></h1>
</div>
<div id="content">
{% block content %} {% endblock %}</div>
</body>
</html>
發表您的HTML,你必須在詳細資料頁一個在 – Exprator
房源頁面添加它。請檢查一下。 –
也添加list.html – Exprator