I am designing generic class base view inside which i want to use the value of pk (primary key) from URL pattern how can i do it?
I have tried view base solution using two parameter (request, pk). But how can it done using gcbv
post method.
Django URL pattern
urls.py
url(r'^(?P<pk>[0-9]+)/add_product/$', views.AddProduct.as_view(), name='add_product')
views.py
class AddProduct (LoginRequiredMixin, CreateView):
login_url = '/login_user'
redirect_field_name = 'redirect_to'
model = Product
template_name = 'shopsurfer/add_product.html'
fields = ['name', 'category', 'lot', 'specs', 'price', 'product_logo']
def form_valid(self, form):
object = form.save(commit=False)
pk = ***here want pk from url***
object.shop = get_object_or_404(Shop, pk=pk)
object.save()
return super(AddProduct, self).form_valid(form)
I want to store the pk value inside the variable PK which is declared inside the AddProduct
它在kwargs里面
pk = self.kwargs['pk']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.