W
W
whiteblackness2016-03-14 11:33:36
Django
whiteblackness, 2016-03-14 11:33:36

Where can I find the standard rules for naming url, class base view and model methods?

Are there any standard or at least generally accepted naming conventions for django?
For example I call view

class BlaBlaCreate(CreateView):
...

class BlaBlaUpdate(UpdateView):
....

class BlaBlaDetail(DetailView):
...

I make url to them
url(r'^blabla/create'$, BlaBlaCreate.as_vew(), name='create_blabla')
url(r'^^blabla/(?P<pk>\d+)/edit$', BlaBlaUpdate.as_vew(), name='edit_blabla')
url(r'^blabla/(?P<pk>\d+)$', BlaBlaDetail.as_vew(), name='blabla_detail')

respectively in the model I have the following methods to get the url
class BlaBla(Model):

  def get_absolute_url(self):
    ...
   def get_edit_url(self):
   ...

   @staticmethod
   def get_create_url():
    ...

But something I'm starting to get confused where I have edit and where is update.
Yes, and I want to make more standard naming.
Is there any documentation on the standard names somewhere? I can't find anything.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Voronkov, 2016-03-14
@whiteblackness

Whatever you want within PEP8
I usually do this:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models


class Article(models.Model):
    title = models.CharField(max_lenght=32, verbose_name=_('name'))

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.views import generic


class ArticleMixin(object):
    fields = ['title']
    model = Article
    paginate_by = 30

    def get_success_url(self):
        return reverse('articles_article_index')


class Index(ArticleMixin, generic.ListView):
    template_name = 'articles/index.html'


class Create(ArticleMixin, generic.CreateView):
    template_name = 'articles/create.html'


class Update(ArticleMixin, generic.UpdateView):
    template_name = 'articles/update.html'


class Delete(ArticleMixin, generic.DeleteView):
    template_name = 'articles/delete.html'


index = Index.as_view()
create = Create.as_view()
update = Update.as_view()
delete = Delete.as_view()

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf.urls import url
from django.conf.urls import patterns

from articles.views import article

urlpatterns = patterns('',
                       url(r'^$', article.index, name='articles_article_index'),
                       url(r'^update/(?P<pk>[\w-]+)/$', article.update, name='articles_article_update'),
                       url(r'^delete/(?P<pk>[\w-]+)/$', article.delete, name='articles_article_delete'),
                       url(r'^create/$', article.create, name='articles_article_create'),
                       )

T
TyzhSysAdmin, 2016-03-14
@POS_troi

Look at the projects on github-e, learn how to say from the masters.
Look for a style guide

S
sim3x, 2016-03-14
@sim3x

Name it in the most understandable way for yourself
until the view becomes bold from the functionality - 'ArticleUpdateView'
when it becomes - 'ArticleCustomiseSomeShitView'
the Article prefix will help when typing code in the presence of intellisense, and the 'View' suffix will help distinguish the class from models and other classes
Focus not on naming , and on class comments it should be
used delete = ArticleDeleteView.as_view()with caution - I'm not sure if as_view is lazy, which can lead to a fun debug

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question