print関数の結果をファイルに出力する
with open('file.txt', 'w') as f:
print('abc', file=f)print関数の結果をファイルに出力する
with open('file.txt', 'w') as f:
print('abc', file=f)DjangoをApacheのWSGIで本番運用している場合、アプリケーションプログラムを変更した場合、いちいちApacheを再起動していました。面倒なので簡単に変更が反映される方法がないか調べたところ、
「wsgi.py を微修正して保存する」と変更が反映されることが分かりました。
これで少し楽になりました。
参考
Is it possible to reload the view without restarting Django? - Stack Overflow
https://stackoverflow.com/questions/9183114/is-it-possible-to-reload-the-view-without-restarting-django
If you are running a production server (nginx, apache, etc) and you want code-reload, then you need to add something to your wsgi module to detect code changes.
Code reloading with apache: http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
Code reloading with uwsgi: http://projects.unbit.it/uwsgi/wiki/TipsAndTricks
If you are running Django as a WSGI application in daemon mode you just need to touch the wsgi.py for your site and it will reload the application next time there is a request. (So no need for any special options).
list_display_links = [] としても、モデルのIDだけが勝手にリンクにされてしまいますが、以下のように None にするとすべて無効化できました。
class TestAdmin(admin.ModelAdmin):
list_display_links = None #リストのリンクをすべて無効化
通常のフィールドは、request.POST によりデータ受け渡しされるが、ファイルフィールドは、request.FILES で受け渡しされる。
フィールドが以下のような形だとすると、
<input type="file" name="file_1"> (選択したファイル名: filename_1.pdf)
<input type="file" name="file_2"> (選択したファイル名: filename_2.jpg)
<input type="file" name="file_3"> (選択したファイル名: filename_3.png)
request.FILES (dict型オブジェクト)は、
object = { 'file_1': file_1, 'file_2': file_3, 'file_1': file_3, }
という構造になっている。
ファイル名を取得するには、
ファイル名取得 object['file1'].name => filename_1.pdf
とする。
その他、以下が取得できる。
ファイルサイズ取得 object['file1'].size
ファイルMIMEタイプ取得 object['file1'].content_type
ファイル文字コード取得 object['file1'].charset
ファイルフィールド (input type=file) からファイル名を取得するには
jQueryの場合
$('.class_name').prop('files')[0].name
例
$ python manage.py shell
>>> from django.conf import settings
>>> settings.BASE_DIR
>>> '/var/www/html/myproject/'
例:
/abc/任意文字列 ⇒ /xyz/任意文字列
from django.views.generic.base import RedirectView
urlpatterns = [
url(r'^abc/(?P<path>.*)$', RedirectView.as_view(url='/xyz/%(path)s', query_string=True)),
]