/ myapp / beds /

时间:2019-03-13 19:50:08

标签: django python-3.x

get()缺少1个必需的位置参数:'mat_number'

我是编程新手,请按照以下详细信息,如何解决上述TypeError:

class BedsView(View):
    def get_user_details(self, username, mat_number):
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            return redirect('index') 
        userbeds = Userbed.objects.filter(user=user, mat_number=mat_number).order_by(-posted_date)[0]
        form = UserBedsForm({'mat_number':userbeds.mat_number})
        return (user, userbeds,form)

    @method_decorator(login_required)
    def get(self, request, username, mat_number):
        (user,userbeds,form) = self.get_user_details(username, mat_number)
        return render(request, 'myapp/beds.html', {'userbeds':userbeds, 'selecteduser':user, 'form':form})

    @method_decorator(login_required)
    def post(self, request, username):
        (user, userbeds, form) = self.get_user_details(username, mat_number)
        form = UserBedsForm(request.POST, instance=userbeds)
        if form.is_valid():
            form.save(commit=True)
            return redirect('beds', user. username)
        else:
            print(form.errors)
        return render(request, 'myapp/beds.html', {'userbeds':userbeds, 'selecteduser':user, 'form':form})

在myapp urls.py,

   from django.urls import path
   from myapp import views 
   from myapp.views import BedsView 

   app_name = 'myapp'

   urlpatterns = [
          path('beds/<username>/', BedsView.as_view(), name='beds'), 
   ]

在myproject urls.py,

  from django.contrib import admin 
  from django.urls import path, include 
  from myapp import views 


   urlpatterns = [
          path(' ', views.index, name='index'), 
          path('admin/', admin.site.urls),
          path('myapp/', include('myapp.urls')), 
   ]

请问如何对这个TypeError进行排序?...

1 个答案:

答案 0 :(得分:1)

您的代码显示为:

def get_user_details(self, username, mat_number):
# ...
(user, userbeds, form) = self.get_user_details(username)

get_user_details需要使用两个参数来调用:usernamemat_number。您只提供了一个:username。因此,可以在方法调用中提供mat_number或更改方法签名。