ãã®æçš¿ã§ã¯-Djangoã§ã®äœæ¥ã«é¢ããå°ããªãã³ããåå¿è ããããããŒã«åœ¹ç«ã€ãããããŸããã Djangoããã¹ã¿ãŒããæ ã®æåã«ãããç¥ãããã®ã§ïŒ..
ãããã®ãã³ãã¯ããçšåºŠã®æ¹å€ã䌎ãã¹ãã ãšèããŠãã ããã ããªããäžæ£ç¢º/æè¯ã®è§£æ±ºçãèŠã€ããå ŽåããŸãã¯ããã¥ã¡ã³ãã«ãªãdjangoã®ç¬èªã®ããããããææ¡ããå Žåãç§ã¯åãã§ããŸãã
ããã§ã¯ãé ãããå§ããŠã詳现ã«ç§»ããŸãããã
ä»®æ³ç°å¢
djangoã¢ããªã±ãŒã·ã§ã³ã«virtualenvã䜿çšããŠããªãå Žåã¯ã ãã²è©ŠããŠã¿ãŠãã ããã
ãã§ã«virtualenvã䜿çšããŠããå Žåã¯ã-no-site-packagesãå¿ èŠãã©ããçããŠãã ããã ãã®ãã©ã°ã¯ããã©ã«ãã§æå¹ã«ãªã£ãŠãããä»®æ³ç°å¢ãäœæãããšãã«äœ¿çšãããŸãã ãå éšãããã°ã©ã ã®ãã©ã°ããªã³ã®å Žåãç°å¢ã¯ããã°ã©ã ããå€éšãã«è¡šç€ºããŸããã ããã±ãŒãžãããŒãžã£ãŒãã°ããŒãã«ã«ããã±ãŒãžåããå Žåãããšãã°python2-djangoã®å Žåãç°å¢ã®ãå éšãã«pip install djangoãã€ã³ã¹ããŒã«ããå¿ èŠããããŸãã
ãªãã°ããŒãã«ã«ã€ã³ã¹ããŒã«ãããããã±ãŒãžãå¿
èŠãªã®ã§ããããïŒ
xapianã§æ€çŽ¢ãšã³ãžã³ãèšå®ãããšãã«ããã«åºããããŸããã Xapianã¯ãxapian-coreïŒC ++ã§èšè¿°ïŒããã³xapian-bindingsïŒpythonãå«ãããŸããŸãªèšèªã®ãã€ã³ãã£ã³ã°ïŒãæäŸããŠããŸãã ããããåæã«æŽæ°ããããšã¯è«ççã§ã-ãšã³ãžã³ãå€æŽãããå Žåããã€ã³ãã£ã³ã°ãæŽæ°ããå¿
èŠããããŸãã ãã®ãããxapian-coreãããã±ãŒãžãããŒãžã£ãŒãšããŠã°ããŒãã«ã«é
眮ããŠãããã€ãã³ã°ã§ã¯ä¿®æ£ãããŸããïŒpipã§ã¯ãããŸããïŒã åºå2ïŒ
äžè¬ã«ãã¢ãžã¥ãŒã«ãçŽç²ãªpythonã§èšè¿°ãããŠããå Žåãåé¡ã¯ãããŸãã-virtualenvã®pipãä»ããŠã€ã³ã¹ããŒã«ããŸãã ã¢ãžã¥ãŒã«ããããšãã°c ++ãšpythonã®æ··åç©ã§ããå Žåãéæ³ãå§ãŸããŸãã
- virtualenvå ã«ãŽãç®±ãäœæããŸãã./configure --prefix = / path / to / virtualenv && make && make install
- ã°ããŒãã«ããã±ãŒãžãå€éšããèŠããããã«ããŠãéžæãããã£ã¹ããªãã¥ãŒã·ã§ã³ã®ããã±ãŒãžãããŒãžã£ãŒã§æŽæ°ããŸãã
äžè¬ã«ãã¢ãžã¥ãŒã«ãçŽç²ãªpythonã§èšè¿°ãããŠããå Žåãåé¡ã¯ãããŸãã-virtualenvã®pipãä»ããŠã€ã³ã¹ããŒã«ããŸãã ã¢ãžã¥ãŒã«ããããšãã°c ++ãšpythonã®æ··åç©ã§ããå Žåãéæ³ãå§ãŸããŸãã
virtualenvããã®ã°ããŒãã«ããã°ã©ã ã®å¯èŠæ§/éå¯èŠæ§ã¯ããã¡ã€ã«[virtualenv] / lib / python *ã* / No-global-site-packages.txtã®äžåš/ååšã«ãã£ãŠèšå®ãããŸãã ãšãŠãç°¡åã§ãã
ã¡ãªã¿ã«ãvirtualenvã®ãåé¢ãã«é¢ããèšäºãã¿ããªã«ãå§ãããŸããvirtualenvãšpipãå«ããªçç± ïŒãµã€ããé ããªãã web.archive.orgããããéããŸããã§ããïŒã ããã¯ãå®éã«virtualenvããå€éšãç°å¢ããã©ã®ããã«éé¢ãããŠãããã調ã¹ãŸã-ã€ãŸããéšåçãªéé¢ã«ãããŸããã
ipython
Pip install ipythonã¯ãæšæºã®Pythonã·ã§ã«ããã«ã©ãŒãªã³ã°ããªãŒãã³ã³ããªãŒããã€ã³ããã¹ãã¯ã·ã§ã³ã䟿å©ãªè€æ°è¡å ¥åãã³ããŒã¢ã³ãããŒã¹ããªã©ã®é«åºŠãªã·ã§ã«ã«çœ®ãæããŸãã Djangoãã€ã³ã¹ããŒã«ãããŠããå Žåãipythonãèªåçã«éžæããŸãã
ãšããã§ããããã®å©ç¹ã¯ãã¹ãŠ./manage.pyã·ã§ã«ã ãã§ãªãããããã°ã§ã䜿çšã§ããimport i pdbã䜿çšããŠãããã°ã§ããŸãã i pdb.set_traceïŒïŒã
ãããžã§ã¯ãæ§é
ãããžã§ã¯ããŸãã¯ã¢ããªã±ãŒã·ã§ã³ã®äœææã«ããã©ã«ãã§Djangoãå¿ èŠãªãã£ã¬ã¯ããªãäœæããŸãã ããããããªãèªèº«ãèããå¿ èŠããããŸãã
ãããžã§ã¯ãã«ååãä»ãããšãã€ã³ããŒãããŸã
ãããžã§ã¯ããããžã§ã¯ãïŒdjango-admin.py startproject projectïŒã«ååãä»ããŸã-ãŸããŸãã§ããããã¹ãŠã®ãããžã§ã¯ãã«åãååãä»ããŸãã 以åã¯ããã¡ã€ã³ã«åŸã£ãŠãããžã§ã¯ããåŒã³åºããŸããããä»ã®ãããžã§ã¯ãã§ã¢ããªã±ãŒã·ã§ã³ãåå©çšããå Žåãã€ã³ããŒããã¹ãå€æŽããå¿ èŠããããŸãã-ã¹ãŒããŒãµã€ãã€ã³ããŒããŠãŒãã£ãªãã£ããããã¥ãŒãµã€ãã€ã³ããŒããŠãŒãã£ãªãã£ããã ããã¯æ··ä¹±ãæããæ°ãæ£ããŸãã ãã®ã¢ããã€ã¹ãå±éããå Žåã¯ããã¹ãŠã®ãããžã§ã¯ãã®ãã£ã¬ã¯ããªæ§é ãèªåã§ä¿®æ£ïŒçµ±åïŒããå³å¯ã«ããã«åŸã£ãŠãã ããã
ã©ã€ãã®äŸïŒ
--site.ru |--static |--media |--project ( ) |--manage.py |--project ( ) | |--settings.py | |--urls.py | |-- ... |--app1 |--app2 |--...
HTMLãã³ãã¬ãŒããä¿åããå Žæ
ãã³ãã¬ãŒãïŒ.htmlïŒãã¢ããªã±ãŒã·ã§ã³ã®ãã³ãã¬ãŒããã©ã«ããŒã«ããããããªãã§ãã ããã ã¢ããªã±ãŒã·ã§ã³ã®ååãšäžèŽããååã®è¿œå ãã£ã¬ã¯ããªãåžžã«äœæããŸãã
ããã¯æªãã§ãããªããªã ããšãã°ã{ïŒ include 'main.html'ïŒ }ã䜿çšããŠããã³ãã¬ãŒãã®è¡çªãäœæããŸãã
/gallery/templates/main.html /reviews/templates/main.html
ããã¯è¯ãã§ãã{ïŒ include 'reviews / main.html'ïŒ }ã䜿çšã§ããŸãïŒ
/gallery/templates/gallery/main.html /reviews/templates/reviews/main.html
{ïŒ includeïŒ }
ã¡ãªã¿ã«ã{ïŒ include 'some_template.html'ïŒ }ã䜿çšããå Žåãäœããééã£ãŠããå¯èœæ§ããããŸãã ãªãã§ïŒ
äŸïŒ
def view(request): return render( request, 'master.html', {'var': 'Some text'} }
<!-- master.html --> Value of variable var: {{ var }}. {% include 'slave.html' %} <!-- slave.html --> Again, value of variable var: {{ var }}.
1ïŒKISSã¯æ£®ã«ä¹ããŸãã äžæ¹ã§ãããŒãžã³ãŒãã¯ããã€ãã«åå²ãããŸã-master.htmlãšãã©ã°ã€ã³slave.htmlãããã¯å€§ããªhtmlããŒãžãããŒãã«åå²ããã®ã«äŸ¿å©ã§ãã ãã ãããã®å Žåãvarå€æ°ã¯æé»çã«slave.htmlãã³ãã¬ãŒãã«æž¡ãããŸããvarã¯master.htmlã«æž¡ãããslave.htmlã¯ãã¹ã¿ãŒã³ã³ããã¹ãããããã¯ãããã ãã§ãã ãããã£ãŠã{ïŒ includeïŒ }å ã®ãã³ãã¬ãŒãã¯ã¡ã€ã³ãã³ãã¬ãŒãã®ã³ã³ããã¹ãã«äŸåããããšãããããŸãã 芪ãã³ãã¬ãŒãã®ã³ã³ããã¹ããç£èŠããå¿ èŠããããŸããããããªããšãåãã³ãã¬ãŒãã§äœãåé¡ãçºçããå¯èœæ§ããããŸãã
2ïŒç§ã®èŠ³å¯ã«ãããšã{ïŒ includeïŒ }ã¯ã¬ã³ããªã³ã°ã®ç¹ã§é«äŸ¡ã§ãã ãããé¿ããæ¹ãè¯ãã
ã©ããã äžéšã®ãã³ãã¬ãŒããæ¬åœã«ä»ã®ãã³ãã¬ãŒãã«å«ãããå Žåã¯ãå å«ã¿ã°ã䜿çšããŸãïŒãããã«ã€ããŠã¯ä»¥äžããèªã¿ãã ããïŒã ããããããã¯ç°¡åã§ã-ãã¹ãŠã1ã€ã®ãã¡ã€ã«ã«æžã蟌ãã ãã§ãïŒ
<!-- master.html --> Value of variable var: {{ var }}. Again, value of variable var: {{ var }}.
settings.py
ãã¹ããµãŒããŒãšå±éãµãŒããŒã«2ã€ã®ç°ãªãsettings.pyããããŸãããïŒ
è¿œå ã®local_settings.pyããã³deployment_settings.pyãäœæãã察å¿ãããµãŒããŒã«ã®ã¿é©çšããããã¹ãŠãããããããŸãã
ããã§ãããšãã°ãlocal_settings.pyã§èšå®ããè«ççãªãã®
DEBUG = True DOMAIN = '127.0.0.1:8000' ALLOWED_HOSTS = ['127.0.0.1', DOMAIN] SERVER_EMAIL = 'mail@test.ru' EMAIL_HOST = 'localhost' EMAIL_PORT = 1025 EMAIL_HOST_USER = '' EMAIL_HOST_PASSWORD = '' EMAIL_USE_TLS = False EMAIL_SUBJECT_PREFIX = '[' + DOMAIN + '] ' DEFAULT_FROM_EMAIL = 'mail@test.ru' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'test', 'USER': 'test', 'PASSWORD': 'test', 'HOST': 'localhost', 'PORT': '', 'ATOMIC_REQUESTS': True, } } CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', } }
settings.pyã§ã¯ãåé ã«æ¬¡ã®ããã«èšè¿°ããŠããŸãã
# Load local settings if available try: from local_settings import * except ImportError: from deployment_settings import *
ãããã£ãŠããããã€æã«local_settings.pyãåé€ããŸãã å¹²æžããªãããã«ã.gitignoreã«è¿œå ã§ããŸãã
ãããžã§ã¯ãã«ãŒã
settings.pyã§ãããžã§ã¯ãã«ãŒããèšå®ããŸããããã«ãããåŸã§äœæ¥ã楜ã«ãªããŸãã
from os import path BASE = path.dirname(path.dirname(path.dirname(path.abspath(__file__)))) MEDIA_ROOT = BASE + '/media/' STATIC_ROOT = BASE + '/static/'
ã³ã³ããã¹ãããã»ããµïŒcontext_processors.pyïŒã{ïŒ includeïŒ }ããã³å å«ã¿ã°
ã³ã³ããã¹ãããã»ããµã¯ããµã€ãã®åããŒãžã®ã³ã³ããã¹ãã«å€æ°ãè¿œå ããå¿ èŠãããå Žåã«ã®ã¿äœ¿çšããŠãã ãããçµæã䜿çšããªãå Žåã§ããã³ã³ããã¹ãããã»ããµã¯ãã¹ãŠã®ããŒãžã§åŒã³åºãããŸãã å人çã«ãç§ã¯ãããã䜿çšããŠé»è©±çªå·ããã³ãã¬ãŒãã®ã³ã³ããã¹ãã«è»¢éããŸã-ãã®çªå·ã¯å®éã«ã¯ãã¹ãŠã®ããŒãžã«è€æ°å衚瀺ãããŸãã å¥ã®äŸã¯ããµã€ãã¡ãã¥ãŒã§ãã ããããŒãšãªã³ã¯ãã³ã³ããã¹ãããã»ããµã«ç»é²ããŸãããã¡ãã¥ãŒã«æ°ããã»ã¯ã·ã§ã³ãè¿œå ããå¿ èŠãããå Žåã¯ãã³ã³ããã¹ãããã»ããµã«è¿œå ããã ãã§ããµã€ãã®ã©ãã«ã§ãèªåçã«è¿œå ãããŸãã
1ã€ã®ééãããããŸã-ãŠã£ãžã§ããã«ã³ã³ããã¹ãããã»ããµã䜿çšããã ããšãã°ããµã€ãã«åžžã«è¡šç€ºããããã¥ãŒã¹åããããŸãã ãã¹ãŠã®ããŒãžã«ã news / context_processors.pyãäœæããnewsãå«ããã¥ãŒã¹å€æ°ãã³ã³ããã¹ãã«è¿œå ãããã³ãã¬ãŒãã«{ïŒ include 'news / news_widget.html'ïŒ }ããŸãã¯{ïŒ load news_widgetïŒ } {ïŒ news_widget newsïŒ } ...
ããã¯æ©èœããŸãããã³ã³ããã¹ããä¹±éã«ãªããããã«ããã®åããããã©ããã誰ãç¥ã£ãŠãããããããŸããã å å«ã¿ã°ã䜿çšããæ¹æ³ããããŸãã ãã³ãã¬ãŒã{ïŒ newsïŒ }ã«æžã蟌ãã ãã§ããã®ãã³ãã¬ãŒãã¿ã°ã¯ãã§ã«ãã¥ãŒã¹ãæ€çŽ¢ãããã¥ãŒã¹åãæ¿å ¥ããŸãã ãããŠãããã¯ããªããæ¬åœã«ãããå®è¡ãããšãã«ã®ã¿æ©èœããŸã-ã€ãŸã ãã³ãã¬ãŒãã«{ïŒ newsïŒ }ãèšè¿°ããŸãã
é»æ±
django-debug-toolbar-template-timings
誰ãã圌ãç¥ã£ãŠããããããã圌ã䜿çšããŠããŸãã ãããã django-debug-toolbar-template-timings-ãã³ãã¬ãŒãã®ã¬ã³ããªã³ã°æéã枬å®ãããããã°ããŒã«ããŒãã©ã°ã€ã³ããããŸãã ãŸããdjangoãã³ãã¬ãŒãã¯éåžžã«ãé«äŸ¡ãã§ããïŒé·ãéã¬ã³ããªã³ã°ããïŒããšãèãããšããã®ãã©ã°ã€ã³ã¯å»åž«ããµã€ããé«éåããããã«åœãããã®ã§ãã
adv_cache_tag
django-adv-cache-tagã䜿çšãããšããã³ãã¬ãŒãã®éåžžã«æè»ãªãã£ãã·ã¥å¶åŸ¡ïŒããŒãžã§ã³ç®¡çãå§çž®ãéšåãã£ãã·ã¥ïŒãå¯èœã«ãªããŸãã ã¡ããã©è©äŸ¡ïŒ
{% load adv_cache %} {% cache 0 object_cache_name object.pk obj.date_last_updated %} <!-- , obj.date_last_updated --> {{ obj }} {% nocache %} {{ now }} <!-- --> {% endnocache %} {{ obj.date_last_updated }} {% endcache %}
django-mail-templated
djangoã«ã¯ã¡ãŒã«ãã³ãã¬ãŒãããããŸããã django-mail-templated
django-ipware
django-ipwareã¯ãŠãŒã¶ãŒã®IPã決å®ããæ¹åããŸãã
ãŠãŒã¶ãŒã®IPãååŸããå Žæãç¥ã£ãŠããŸããïŒ
'HTTP_X_FORWARDED_FOR', # client, proxy1, proxy2 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'HTTP_VIA', 'REMOTE_ADDR',
çŸããã¹ãŒã
htmlããŒãµãŒãäœæããªãã§ãã ããã HTMLãèªåã§è§£æããªãã§ãã ããã ãã¹ãŠããã§ã«ããã«ãããŸãã
圹ã«ç«ã€ãããããªããã³ãã¬ãŒãã¿ã°
add_class
ãã©ãŒã ãäœæããåå ¥åã«ã¹ã¿ã€ã«ãã¯ã©ã¹ããŸãã¯ãã¬ãŒã¹ãã«ããŒãèšå®ããå Žåãdjangoã¯ååã«éåããããšã匷å¶ãããã¹ãŠã®ã¹ã¿ã€ã«ãforms.pyã«çŽæ¥èšè¿°ããŸãã
class SomeForm(ModelForm): class Meta: model = SomeModel fields = ('field1', 'field2') widgets = { 'field1': Textarea(attrs={'rows': '2', 'class': 'field1_class'}), }
.htmlãã¡ã€ã«ã«ãªãhtmlããã¹ãã衚瀺ããããšãè³éãã«ãªããŸãã ããã¯ãMVTã¢ãŒããã¯ãã£ã«éåããŸãã ãããã£ãŠãç§ã¯èªåçšã®ãã£ã«ã¿ãŒãäœæããŸããã
{% load add_class %} {{ form.field1|add_class:'field1_class' }}
ãã®ãã£ã«ã¿ãŒã¯ã¯ã©ã¹ãã¿ã°ã«è¿œå ããŸãããä»»æã®ããããã£ãæžãæããŠè¿œå ã§ããŸãã
ã³ãŒãadd_class.py
from django import template from django.utils.safestring import mark_safe from bs4 import BeautifulSoup register = template.Library() @register.filter def add_class(html, css_class): soup = BeautifulSoup(unicode(html), 'html.parser') for tag in soup.children: if tag.name != 'script': if 'class' in tag: tag['class'].append(css_class) else: tag['class'] = [css_class] return mark_safe(soup.renderContents())
is_current_page
ç¹å®ã®ããŒãžãéããŠããå Žåããã³ãã¬ãŒãã«äœãã衚瀺ããå¿ èŠãããå ŽåããããŸãã ããšãã°ããŠãŒã¶ãŒãã¹ãã¢ã»ã¯ã·ã§ã³ã«ããå Žåã¯ãã¡ãã¥ãŒã®[ã¹ãã¢]ãã¿ã³ã匷調衚瀺ããŸãã 次ã®ãªãã·ã§ã³ããå§ãããŸãã
from django import template from django.core.urlresolvers import resolve from project.utils import parse_args register = template.Library() @register.filter def is_current_page(request, param): return resolve(request.path).view_name == param
ããã¯ã¿ã°ã§ã¯ãªããã£ã«ã¿ãŒã§ããããã®çç±ã¯åãã§ãã{ïŒ ifïŒ }ã䜿çšãããšãå®å šã«ã¯ã€ã«ããªæ§é ãæ§ç¯ã§ããŸãã ããšãã°ãçŸåšã®ããŒãžã補åã«ãŒãã§ããããŠãŒã¶ãŒãæ¿èªãããŠããå ŽåïŒ
{% if request|is_current_page:'shop/product' and user.is_authenticated %}
åŒæ°ïŒargsãŸãã¯kwargsïŒã䜿çšããŠæ£ç¢ºãªããŒãžïŒã€ãŸããã補åã®ããŒãžãã ãã§ãªãããid = 36ã®è£œåããŒãžãïŒã決å®ãããããæ£ç¢ºãªä»£æ¿å®è£ ããããŸãã
{% if request|is_current_page:'shop/product,id=36' %}
@register.filter def is_current_page(request, param): params = param.split(',') name = params[0] args, kwargs = parse_args(params[1:]) # Do not mix args and kwargs in reverse() - it is forbidden! if args: return request.path == reverse(name, args=args) elif kwargs: return request.path == reverse(name, kwargs=kwargs) else: return request.path == reverse(name)
ã¢ãã«
空ã®
ã¢ãã«ã空ã®å ŽåããããŸãã ãã®ããã«ïŒ
class Phrase(models.Model): pass class PhraseRu(models.Model): phrase = models.ForeignKey(Phrase, verbose_name='', related_name='ru') class PhraseEn(models.Model): phrase = models.ForeignKey(Phrase, verbose_name='', related_name='en')
ãã®å ŽåãPhraseã¯PhraseEnãšPhraseRuã®éã®ãªã³ã¯ã§ãããããèªäœã«ã¯äœãå«ãŸããŠããŸããã 2ã€ã®ã¢ãã«ãåçã§ããã1ã€ã®å šäœã«æ¥ç¶ããå¿ èŠãããå Žåã«åœ¹ç«ã¡ãŸãã
ãžã§ããªãã¯ãªã¬ãŒã·ã§ã³ããã¯ã¹ã€ã³
GenericRelationãªããžã§ã¯ãã¯ããªããžã§ã¯ãã1ã€ãããªãããšã確å®ã«ããã£ãŠããå Žåã§ããåžžã«QuerySetã«ãã£ãŠè¿ãããŸãã
class Token(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() class Registration(models.Model): tokens = generic.GenericRelation(Token)
ããŒã¯ã³ã«ã¢ã¯ã»ã¹ããå¿ èŠãããå Žåãregistration.tokens.firstïŒïŒãèšè¿°ããŸãã ããããããŒã¯ã³ã1ã€ãããªãããšãç¥ã£ãŠããã®ã§ãregistration.tokenãèšè¿°ããŠã倧åãªããŒã¯ã³ãããã«ååŸãããã ãã§ãã ããã¯mixinã§å¯èœã§ãïŒ
class Token(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() class TokenMixin(object): @property def token(self): content_type = ContentType.objects.get_for_model(self.__class__) try: return Token.objects.get(content_type__pk=content_type.pk, object_id=self.id) except Token.DoesNotExist: return None class Registration(models.Model, TokenMixin): tokens = generic.GenericRelation(Token)
ããã§registration.tokenã¯æ©èœããŸãïŒ
get_absolute_url
{ïŒ url 'shop / product' id = product.idïŒ }ãèšè¿°ããªãããã«ããŠãã ããã
ã¢ãã«ããšã«ãget_absolute_urlïŒïŒã¡ãœãããèšå®ãã{{object.get_absolute_url}}ã䜿çšããŸãã åæã«ã管çããã«ã«ããµã€ããèŠãããªã³ã¯ã衚瀺ãããŸãã
pre_save
pre_saveã§ã¯ãä¿ååŸã«ã¢ãã«ãå€æŽããããã©ããã確èªã§ããŸãã äŸ¡æ Œ-ããŒã¿ããŒã¹ããå€ãã¬ã³ãŒããååŸããããã®ããŒã¿ããŒã¹ãžã®ãªã¯ãšã¹ãã
@receiver(pre_save, sender=SomeModel) def process_signal(sender, instance, **kwargs): old_model = get_object_or_None(SomeModel, pk=instance.pk) if not old_model: # Created old_value = None ... else: old_value = old_model.field new_value = instance.field if new_value != old_value: # field changed!
ãã©ãŒã
ãã®ãã¿ãŒã³ã¯ãã§ã«ããäžã«ãããŸããããèšããŸã§ããããŸããã
form = SomeForm(request.POST or None) if form.is_valid(): # ... actions ... return HttpResponseRedirect(...) return render( request, {'form': form} )
ããã ãã§ã ãæž èŽããããšãããããŸããã
UPDã Habréã®ãã€ãã®ããã«ãã³ã¡ã³ãã§è¡å人ã¯æèŠãè¿°ã¹ãèšäºã«ããããã®çŽ æŽãããã¢ã€ãã¢ãè¿œå ãã³ã¡ã³ããæäŸããŸããã ç§ã¯ããããèšäºã«å«ããŸããã§ãããã代ããã«ãèšäºã«é¢ããã³ã¡ã³ããèªãããšã匷ããå§ãããŸãã