简体   繁体   中英

post_detail() view missing required positional arguments:

I'm beginner to django . I am facing this problem that I can't get the post detail view with year , month and post . Error is post_detail() missing 4 required positional arguments: 'year', 'month', 'day', and 'post'.

this is 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
    ______________________________________________________________

this is 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})

this is 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'),

this is post_detail.html page

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

this is list.html page :

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

this is 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>

remove post parameter from post_detail view. and remove third url for same view. Now your requested url should be:

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

So far I guessed you want to resolve domain/year/month/day/slug URL.

The following changes will make it work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM