牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

「Python の Web フレームワーク Django って凄い」ってアピールのに "django-generate-scaffold" を使って3分でブログを作るとよいかもな件

DjangoRuby on Rails の scaffold 的なことをする『django-generate-scaffold』を使ってみる。
(Python==2.7, Django==1.4, django-generate-scaffold==0.0.3a1)

Install: django-generate-scaffold

$ pip install django-generate-scaffold

startproject

$ django-admin.py startproject ownsomeblog
$ cd ownsomeblog/
$ python manage.py startapp blogs
$ ls
blogs/
manage.py
ownsomeblog/

settings.py

  • ownsomeblog/settings.py

SQLite3 設定。
INSTALLED_APPS に 'blogs', 'generate_scaffold' を追加。

:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': 'sqlite3.db',
:
    }
}
:
INSTALLED_APPS = (
:
    'blogs',
    'generate_scaffold',
)

generatescaffold --help

generatescaffold のヘルプを見てみる。

$ python manage.py generatescaffold --help
Usage: manage.py generatescaffold [options] 

Rails-like view/template generator.

manage.py generatescaffold <app_name> [options] <model_name> [field_name:field_type ...]

For example, to generate a scaffold for a model named "Post" in an app named "blogs",
you can issue the following command:

manage.py generatescaffold blogs Post title:char body:text blog:foreignkey=Blog

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  -d, --dry-run         Do not actually do anything, but print what would have
                        happened to the console.
  -m EXISTING_MODEL, --model=EXISTING_MODEL
                        An existing model to generate views/templates for.
  -t TIMESTAMP_FIELDNAME, --timestamp-field=TIMESTAMP_FIELDNAME
                        The name of the field used as a timestamp for date-
                        based views. This option may only be used when passing
                        a model via the `--model` option.
  -n, --no-timestamps   Do not automatically append created_at and updated_at
                        DateTimeFields to generated models.
  --version             show program's version number and exit
  -h, --help            show this help message and exit

generatescaffold --model Blog

モデル、ビュー、テンプレートなどを自動的に作る。

$ python manage.py generatescaffold blogs Blog title:string body:text is_public:bool
	        backup	blogs/models.py
	        create	blogs/views
	        backup	blogs/urls.py
	        create	blogs/templates
	        create	blogs/templates/blogs
	        create	blogs/templates/blogs/blog
	        append	blogs/models.py
	        create	blogs/views/__init__.py
	        create	blogs/views/blog_views.py
	        create	blogs/urls.py
	        append	blogs/urls.py
	        create	blogs/templates/blogs/blog/base.html
	        create	blogs/templates/blogs/blog/blog_archive.html
	        create	blogs/templates/blogs/blog/blog_archive_day.html
	        create	blogs/templates/blogs/blog/blog_archive_month.html
	        create	blogs/templates/blogs/blog/blog_archive_week.html
	        create	blogs/templates/blogs/blog/blog_archive_year.html
	        create	blogs/templates/blogs/blog/blog_confirm_delete.html
	        create	blogs/templates/blogs/blog/blog_detail.html
	        create	blogs/templates/blogs/blog/blog_form.html
	        create	blogs/templates/blogs/blog/blog_list.html
	        create	blogs/templates/blogs/blog/object_table_detail.html
	        create	blogs/templates/blogs/blog/object_table_list.html
	        create	blogs/templates/blogs/blog/pagination.html

views

  • blogs/views/blog_views.py

変更しないけどコード見てみる。

from django.views.generic import ListView, DetailView, CreateView, \
                                 DeleteView, UpdateView, \
                                 ArchiveIndexView, DateDetailView, \
                                 DayArchiveView, MonthArchiveView, \
                                 TodayArchiveView, WeekArchiveView, \
                                 YearArchiveView


from blogs.models import Blog


class BlogView(object):
    model = Blog

    def get_template_names(self):
        """Nest templates within blog directory."""
        tpl = super(BlogView, self).get_template_names()[0]
        app = self.model._meta.app_label
        mdl = 'blog'
        self.template_name = tpl.replace(app, '{0}/{1}'.format(app, mdl))
        return [self.template_name]


class BlogDateView(BlogView):
    date_field = 'created_at'
    month_format = '%m'


class BlogBaseListView(BlogView):
    paginate_by = 10


class BlogArchiveIndexView(
    BlogDateView, BlogBaseListView, ArchiveIndexView):
    pass


class BlogCreateView(BlogView, CreateView):
    pass


class BlogDateDetailView(BlogDateView, DateDetailView):
    pass


class BlogDayArchiveView(
    BlogDateView, BlogBaseListView, DayArchiveView):
    pass


class BlogDeleteView(BlogView, DeleteView):

    def get_success_url(self):
        from django.core.urlresolvers import reverse
        return reverse('blogs_blog_list')


class BlogDetailView(BlogView, DetailView):
    pass


class BlogListView(BlogBaseListView, ListView):
    pass


class BlogMonthArchiveView(
    BlogDateView, BlogBaseListView, MonthArchiveView):
    pass


class BlogTodayArchiveView(
    BlogDateView, BlogBaseListView, TodayArchiveView):
    pass


class BlogUpdateView(BlogView, UpdateView):
    pass


class BlogWeekArchiveView(
    BlogDateView, BlogBaseListView, WeekArchiveView):
    pass


class BlogYearArchiveView(
    BlogDateView, BlogBaseListView, YearArchiveView):
    make_object_list = True

urls.py

blogs の url を追加。

  • ownsomeblog/urls.py
urlpatterns = patterns('',
                       url(r'^', include('blogs.urls')),
)

syncdb

$ python manage.py syncdb

runserver

runserver してブラウザで動作確認。

$python manage.py runserver




generatescaffold --model Post

$ python manage.py generatescaffold blogs Post title:string body:text blog:foreign=Blog
	        backup	blogs/models.py
	        exists	blogs/views
	        exists	blogs/views/__init__.py
	        exists	blogs/urls.py
	        backup	blogs/urls.py
	        exists	blogs/templates
	        exists	blogs/templates/blogs
	        create	blogs/templates/blogs/post
	        append	blogs/models.py
	        create	blogs/views/post_views.py
	        append	blogs/urls.py
	        create	blogs/templates/blogs/post/base.html
	        create	blogs/templates/blogs/post/post_archive.html
	        create	blogs/templates/blogs/post/post_archive_day.html
	        create	blogs/templates/blogs/post/post_archive_month.html
	        create	blogs/templates/blogs/post/post_archive_week.html
	        create	blogs/templates/blogs/post/post_archive_year.html
	        create	blogs/templates/blogs/post/post_confirm_delete.html
	        create	blogs/templates/blogs/post/post_detail.html
	        create	blogs/templates/blogs/post/post_form.html
	        create	blogs/templates/blogs/post/post_list.html
	        create	blogs/templates/blogs/post/object_table_detail.html
	        create	blogs/templates/blogs/post/object_table_list.html
	        create	blogs/templates/blogs/post/pagination.html
$ python manage.py syncdb
$ python manage.py runserver 



memo

なかなかよさそうかも。時間空いたら調べてみるかも。