django app_details()得到了一个意想不到的关键字参数' slug'错误

时间:2017-09-11 19:10:32

标签: python django

你好我想创建一个简单的Django博客,我想从一些简单的电影列表中获取mivies的详细信息,但我接受了这个错误:

app_details() got an unexpected keyword argument 'slug'

我已按照文档操作,我认为我的代码是正确的,但只要我犯了这个错误。

models.py

class app_movies(models.Model):
    title=models.CharField(max_length=255)
    slug_title = models.SlugField()

urls.py

url(r'^movies-details/(?P<slug>[^\.]+)/$', views.movies_details, name='movies_details'),

views.py

def movies_list(request):
    return render(request, 'movies_list.html',{'movies':app_movies.objects.filter(createddate__lte=timezone.now()).order_by('-createddate')})


def movies_details(request,slug_title):
    movies=app_movies.objects.all()
    app_movies=get_object_or_404(movies, slug=slug_title)
    return render(request, 'movies_details.html',{'movies':movies,'app_movies':app_movies})

html标签:

<a  href="/movies-details/{{movies.slug_title}}">View Project <span ></span></a>

2 个答案:

答案 0 :(得分:1)

假设您的错误实际上是movies_details,您已将该参数调用到该函数slug_title,但URL模式只有slug。那些必须是一样的。

答案 1 :(得分:1)

views.py 中将slug作为字段传递,然后它应该等于模型字段slug_title

def movies_details(request,slug):
    movies=app_movies.objects.all()
    app_movies=get_object_or_404(movies, slug_title=slug)
    return render(request, 'movies_details.html',{'movies':movies,'app_movies':app_movies})