1

Good day SO!

Next to Java I'm trying to learn some Python/Django since the company I work for is also going to use Django. However, I am trying to figure out how to work with Generic Class Based Views. I hope somebody can give me some information to guide me in the right direction to solve my problem.

I have a small blog application containing CRUD (Create, Read, Update, Delete) abilities with GCBV (Generic Class Based Views). In the Detail view I have a link to publish:

{% url 'blogs:publish' blog.pk %}

which i want to use like:

url(r'^(?P[0-9]+)/publish/$', xxx, name='publish')

I just can't get it to work. I have attempted (and simular attempts) to create a method in the Update(UpdateView) class called publish(self, **kwargs): and make the url pattern to call it:

url(r'^(?P[0-9]+)/publish/$', views.Update.publish(), name='publish')

which obviously doesn't work, otherwise you wouldn't be reading this right now ;) I've been reading quite some docs/google/etc, but mostly it's function based or the tutorial stops after CRUD. Can you push me in the right direction (tip/clear tutorial/example) or an explanation where I'm taking the wrong choices? Thanks in advance!

Nrzonline
  • 1,600
  • 2
  • 18
  • 37

1 Answers1

1

UpdateView is used for updating, but you may take a look at CreateView. It is used to create objects.

Also you need to understand that you can't call a method as it's even hard to imagine how it has to work. GCBV are just sequences of already written methods which make your life easier. You can overwrite GCBV basic methods and create your own, which then can be used inside the view, but you can't call them in the urls.

pythad
  • 4,241
  • 2
  • 19
  • 41
  • Thank you for your reply. When a blog is created it is always unpublished, the detail view shows a link to publish. How can i make that link publish the blog properly? – Nrzonline Nov 18 '15 at 22:00
  • 1
    @Nrzonline, All you need to do is to create a link and map an `url` to it in which you will capture the `pk` of an `object`. After this you need to write a view which will get object instance using `pk` captured in the url and update the object's filed `object.published = True`, after this you just save the object and return a `HttpResponseRedirect`. It's okay to use a function base view in this situation. You can also use ajax if you don't want to reload the page, but this depends on what you want to do. – pythad Nov 18 '15 at 22:10
  • thanks. For me it felt like I had to wrap it into GCBVs, I actually don't know why. Anyway, it is working now. Thanks again! – Nrzonline Nov 19 '15 at 15:25