Search code examples
djangodjango-class-based-viewsurl-patternresolveurl

(GCBV) How to call custom method to publish blog (UpdateView)


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!


Solution

  • 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.