Django - feed()中的TypeError缺少1个必需的位置参数:' id'

时间:2017-08-05 14:30:47

标签: python django django-views

我试图为我的项目添加评论,但我发现了这个错误:feeds() missing 1 required positional argument: 'id'。这是我的代码。

views.py

@login_required
def feeds(request, id):
    feeds_list = Feed.objects.all()
    feed = get_object_or_404(Feed, id=id)
    users = User.objects.filter(is_active=True)

    if request.method == 'POST':
        # Komentarz został opublikowany.
        feed_form = FeedForm(request.POST)
        if feed_form.is_valid():
            # Utworzenie obiektu Comment, ale jeszcze nie zapisujemy go w bazie danych.
            new_feed = feed_form.save(commit=False)
            new_feed.user = request.user
            new_feed.date = timezone.now
            # Zapisanie komentarza w bazie danych.
            new_feed.save()
    else:
        feed_form = FeedForm()

    context = {'section': feeds,
               'users': users,
               'feeds_list': feeds_list,
               'feed_form': feed_form}
    return render(request, 'feed/list.html', context)

我认为这是问题:feed = get_object_or_404(Feed, id=id),但我不知道如何解决它。

感谢您的帮助。

编辑: urls.py

from django.contrib.auth.urls import url
from . import views

app_name = 'feedApp'
urlpatterns = [
    url(r'^$', views.feeds, name='feeds'),
    url(r'^feed/new$', views.post_new, name='post_new'),
    url(r'^feed/(?P<id>[0-9]+)/$', views.feed, name='post_detail'),
    # url(r'^feed/(?P<id>[0-9]+)/comment$',
    #     views.comment_feed,
    #     name='comment_feed'),
    url(r'^feed/(?P<id>[0-9]+)/delete/$',
        views.feed_delete,
        name='feed_delete'),
]

2 个答案:

答案 0 :(得分:1)

根据您的urls,您需要向其添加必需的参数id。 编辑您的网址:

url(r'^(?P<id>[0-9]+)$', views.feeds, name='feeds'),

答案 1 :(得分:0)

您还没有使用过&#39; id&#39;作为views.feeds网址中的关键字参数。 添加url(r'^feed/(?P<id>[0-9]+)/$', views.feeds,),