【问题标题】:post_detail() view missing required positional arguments:post_detail() 视图缺少必需的位置参数:
【发布时间】:2017-09-05 05:25:16
【问题描述】:

我是 django 的初学者。我面临这个问题,我无法获得带有 year 、 month 和 post 的 post detail 视图。 错误是 post_detail() 缺少 4 个必需的位置参数:“年”、“月”、“日”和“帖子”。

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

【问题讨论】:

  • 将您的 html 发布到列表页面中的详细页面的 位置
  • 添加了它。请检查一下。
  • 也添加list.html
  • 你传递给post_detail视图的post是什么?
  • 添加了它-@Exprator

标签: python django


【解决方案1】:

到目前为止,我猜你想解析domain/year/month/day/slug URL。

以下更改将使其工作。

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'),
    ]

【讨论】:

  • 谢谢,但我想知道post_detail视图中Post参数的作用,
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2019-12-01
    • 2018-09-09
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多