I'm trying to update a model in Django using the class-based generic view UpdateView.
我试图使用基于类的泛型视图UpdateView来更新Django中的一个模型。
I read the page Updating User model in Django with class based UpdateView to try and get me started, but I'm getting an error 'WSGIRequest' object has no attribute 'id'
我用基于类的UpdateView在Django中读取页面更新用户模型,试图让我开始,但是我得到了一个错误“WSGIRequest”对象没有属性“id”
I'm a fresh face to Django, so please be forgiving if I'm doing something stupid.
我是Django的新面孔,所以如果我做了一些愚蠢的事情,请原谅我。
//urls.py
/ / urls . py
url(r'^portfolios/update/(?P<id>\d+)/$',PortfoliosUpdateView.as_view()),
//views.py
/ / views.py
class PortfoliosUpdateView(UpdateView):
form_class = PortfoliosCreateForm
model = Portfolios
template_name = 'portfolios/create.html'
def get(self, request, **kwargs):
self.object = Portfolios.objects.get(id=self.request.id)
form_class = self.get_form_class()
form = self.get_form(form_class)
context = self.get_context_data(object=self.object, form=form)
return self.render_to_response(context)
def get_object(self, queryset=None):
obj = Portfolios.objects.get(id=self.request.id)
return obj
It's mostly just a modified version of the code originally posted, but I thought it'd work. I know that I'm trying to retrieve the id passed as a GET parameter, but that doesn't seem to come through in the request variable. Am I going about this the wrong way?
它只是最初发布的代码的一个修改版本,但我认为它可以工作。我知道我正在尝试检索作为GET参数传递的id,但这似乎在request变量中没有实现。我是不是走错路了?
Thanks
谢谢
Edit: I think I fixed it, but this may be wrong: I changed the lines
编辑:我想我修好了,但这可能是错的:我改了台词。
self.object = Portfolios.objects.get(id=self.request.id)
obj = Portfolios.objects.get(id=self.request.id)
to
来
self.object = Portfolios.objects.get(id=self.kwargs['id'])
obj = Portfolios.objects.get(id=self.kwargs['id'])
I could be wrong.
我可能是错的。
2 个解决方案
#1
22
It should be:
应该是:
def get_object(self, queryset=None):
obj = Portfolios.objects.get(id=self.kwargs['id'])
return obj
Look at class based generic view dispatch explains that keyword arguments are assigned to self.kwargs.:
基于类的通用视图分派解释了关键字参数被分配给self。kwargs。
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
self.request = request
self.args = args
self.kwargs = kwargs
return handler(request, *args, **kwargs)
#2
6
id = self.request.GET.get('id',None)
is what you needed when trying to access the GET query string.
id = self.request.GET.get('id',None)是您在尝试访问GET查询字符串时需要的。
However, your view can be simplified:
不过,你的观点可以简化:
from django.conf.urls import *
from django.views.generic import UpdateView
from yourapp.models import Portfolios
from yourapp.forms import PortfoliosCreateForm
urlpatterns = patterns('',
url('^portfolios/update/(?P<pk>[\w-]+)$', UpdateView.as_view(
model=Portfolios,
form_class=PortfoliosCreateForm,
template_name='portfolios/create.html',
success_url='/portfolios'
), name='portfolio_update'),
)
#1
22
It should be:
应该是:
def get_object(self, queryset=None):
obj = Portfolios.objects.get(id=self.kwargs['id'])
return obj
Look at class based generic view dispatch explains that keyword arguments are assigned to self.kwargs.:
基于类的通用视图分派解释了关键字参数被分配给self。kwargs。
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
self.request = request
self.args = args
self.kwargs = kwargs
return handler(request, *args, **kwargs)
#2
6
id = self.request.GET.get('id',None)
is what you needed when trying to access the GET query string.
id = self.request.GET.get('id',None)是您在尝试访问GET查询字符串时需要的。
However, your view can be simplified:
不过,你的观点可以简化:
from django.conf.urls import *
from django.views.generic import UpdateView
from yourapp.models import Portfolios
from yourapp.forms import PortfoliosCreateForm
urlpatterns = patterns('',
url('^portfolios/update/(?P<pk>[\w-]+)$', UpdateView.as_view(
model=Portfolios,
form_class=PortfoliosCreateForm,
template_name='portfolios/create.html',
success_url='/portfolios'
), name='portfolio_update'),
)