room_n
room_n

Reputation: 134

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>
    <p class="description">
        {{post.description}}
    </p>
    {{post.content|safe}}
</article>
{% endblock %}

Url pattern:

urlpatterns = [
    url(r'^blog/$', 'blog.views.post', name='blogpage'),
    url(r'^(?P<slug>[\w\-]+)/$', 'blog.views.post'),
]

View:

from django.conf import settings
from django.shortcuts import render, get_object_or_404
from blog.models import Post
from .forms import ContactForm
from django.core.mail import send_mail

def index(request):
    posts = Post.objects.filter(published=True)
    return render(request, 'blog/index.html', {'posts': posts})

def post(request, slug):
    post = get_object_or_404(Post, slug=slug)
    return render(request, 'blog/post.html', {'post': post})

def contact(request):
    form = ContactForm(request.POST or None)
    if form.is_valid():
        form_email = form.cleaned_data.get("email")
        form_message = form.cleaned_data.get("message")
        form_full_name = form.cleaned_data.get("full_name")
        subject = 'Site contact form'
        from_email = settings.EMAIL_HOST_USER
        to_email = [from_email, '#']
        contact_message = "%s: %s via %s"%(
            form_full_name, 
            form_message, 
            form_email)

        send_mail(subject, 
                contact_message, 
                from_email, 
                to_email, 
                fail_silently=False)

    context = {
        "form": form,
    }
    return render (request, 'forms.html', context)

Not sure what else I need to post to give you guys more details.

I'm using python 3.4.1 and django 1.9 on ubuntu 14.04.

Upvotes: 0

Views: 14175

Answers (2)

harishkb
harishkb

Reputation: 406

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):
    ...

Upvotes: 2

Pouria
Pouria

Reputation: 1091

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

Upvotes: 0

Related Questions