post_detail()视图缺少必需的位置参数:

时间:2017-09-05 05:25:16

标签: python django

我是django的初学者。我正面临这个问题,我无法获得包含年份,月份和帖子的帖子详细信息视图。 错误是post_detail()缺少4个必需的位置参数:'year','month','day'和'post'。

这是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

{% 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>

2 个答案:

答案 0 :(得分:0)

从post_detail视图中删除post参数。并删除相同视图的第三个网址。现在您要求的网址应为:

localhost:8000/2017/12/16/my-post

答案 1 :(得分:0)

到目前为止,我猜您要解析domain/year/month/day/slug网址。

以下更改将使其有效。

views.py

def post_detail(request,slug,year, month,day):
    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

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})/(?P<slug>[-\w]+)/$',views.post_detail,name='post_detail'),
    ]