我对Django很新,所以我自然而然地碰到了墙。我在尝试创建博客页面时收到此错误消息。我试着阅读我得到的错误,但无济于事。
完整错误:
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']
以下是我的模特:
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])
发布视图:
{% 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 %}
网址格式:
urlpatterns = [
url(r'^blog/$', 'blog.views.post', name='blogpage'),
url(r'^(?P<slug>[\w\-]+)/$', 'blog.views.post'),
]
查看:
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)
不确定我还需要发布什么来给你们更详细的信息。
我在ubuntu 14.04上使用python 3.4.1和django 1.9。
答案 0 :(得分:0)
您似乎将浏览器中的参数传递给除slug之外的所有模型属性,slug是函数slug=slug
的位置参数。我没有看到你在处理请求时处理它。因此缺少位置论证。你在其他地方那样做吗?在这里,您只需说slug
。第二个POST
来自哪里?您应该使用Django工具或JavaScript / Ajax / HTML-Post从浏览器接收它。一旦你这样做,比如通过Ajax和JSON,它就会成为你dict()
def handle(request):
if request.method == 'POST' and (
'module_status' in request.POST.keys()
):
module_status = request.POST['module_status']
请求的一部分。
var tweets = from tweet in twitterContext.Status
where tweet.Type == StatusType.User
&& tweet.ScreenName == "Goofy"
select tweet;
答案 1 :(得分:0)
您收到错误是因为'^blog/$'
配置为使用post
视图定义,slug
是必填字段,http查询中没有通过urlpatterns条目提供url(r'^blog/$', 'blog.views.post', name='blogpage'),
。
尝试在视图slug
定义中为post
设置默认值,并相应地处理这两种情况(对于两个网址模式)。
def post(request, slug=None):
...