Home Ask Login Register

Developers Planet

Your answer is one click away!

room_n February 2016

TypeError: post() missing 1 required positional argument: 'slug'

I'm pretty new to Django, so naturally I hit the wall. I'm getting this error message, when trying to create a blog page. I tried to read up on the error I'm getting, but to no avail.

Full error:

TypeError at /blog/
post() missing 1 required positional argument: 'slug'
Request Method: GET
Request URL:    http://127.0.0.1:8000/blog/
Django Version: 1.9.2
Exception Type: TypeError
Exception Value:    
post() missing 1 required positional argument: 'slug'
Exception Location: /usr/lib/python3.4/site-packages/django/core/handlers/base.py in get_response, line 147
Python Executable:  /usr/bin/python3.4
Python Version: 3.4.3
Python Path:    
['/home/rumen/Desktop/venv2/blog_project',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4/site-packages',
 '/usr/local/lib/python3.4/dist-packages',
 '/usr/lib/python3/dist-packages']

Below is my model:

from django.db import models
from django import forms
from django.core.urlresolvers import reverse

class Post(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True, max_length=255)
    description = models.CharField(max_length=255)
    content = models.TextField()
    published = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created']

    def __unicode__(self):
        return u'%s' % self.title

    def get_absolute_url(self):
        return reverse('blog.views.post', args=[self.slug])

Post view:

{% block title %}{{post.title}}{% endblock %}

{% block content_blog %}
<article>
    <header>
        <h1> {{post.title}} </h1>
        <p>
            Posted on
            <time datetime="{{post.created|date:"c"}}">
            {{post.created|date}}
            </time>
        </p>
    </header&        

Answers


Pouria Hadjibagheri February 2016

You seem to be passing arguments from the browser to all your model attributes except slug, which is a positional argument of function post. I don't see you handling that when processing the request. Hence the missing positional argument. Do you do that elsewhere? Here you just say slug=slug. Where does the second slug come from? You should use Django tools or JavaScript/Ajax/HTML-Post to receive it from the browser. Once you do that, say through Ajax and JSON, it will be a part of your POST request as a dict().

def handle(request):

    if request.method == 'POST' and (
                'module_status' in request.POST.keys()
    ):

        module_status = request.POST['module_status']


harishkb February 2016

You're getting the error because '^blog/$' is configured to use the post view definition for which slug is a required field and none is supplied in the http query via the urlpatterns entry url(r'^blog/$', 'blog.views.post', name='blogpage'),.

Try setting a default value for slug in your view post definition and handle the two cases (for the two url patterns), accordingly.

def post(request, slug=None):
    ...

Post Status

Asked in February 2016
Viewed 1,912 times
Voted 6
Answered 2 times

Search




Leave an answer


Quote of the day: live life