django-shell

How can I print the value of TEMPLATE_DIRS from the django interactive shell?

与世无争的帅哥 提交于 2019-12-06 03:48:00
I would like to print this value for debugging purposes. How can I do it? print TEMPLATE_DIRS doesn't work print settings.TEMPLATE_DIRS doesn't work. Did you import the settings first? $steve ./manage.py shell Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) In [1]: from django.conf import settings In [2]: settings.TEMPLATE_DIRS Out [2] ('/Volumes/HDD/usr/local/django/mytestproject/templates',) I think the easiest, and therefore the ans you may be looking for is to add the following to your settings.py print ("This is the template.dirs", TEMPLATES[0]['DIRS'])...python 3.x for 2.x leave out the

Variables scope in inline django shell, vs python shell

孤人 提交于 2019-12-06 01:02:28
I have problem, with strange behavior of django shell. I have this code: fields = ('name', 'description', 'long_description', 'foot_description') a = 1 dict( (field, a) for field in fields) When I run it from python shell it's give me right dict. But when i running it from django shell i get: --------------------------------------------------------------------------- NameError Traceback (most recent call last) /usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>() ----> 1 dict( (field, a) for field in fields) /usr/local/lib/python2.7/dist-packages/django

Django: simulate HTTP requests in shell

安稳与你 提交于 2019-12-02 22:34:36
I just learnt that with Rails is possible to simulate HTTP requests in the console with few lines of code. Check out: http://37signals.com/svn/posts/3176-three-quick-rails-console-tips (section "Dive into your app"). Is there a similar way to do that with Django? Would be handy. How I simulate requests from the python command line is: Use the excellent requests library Use the django reverse function A simple way of simulating requests is: >>> from django.urls import reverse >>> import requests >>> r = requests.get(reverse('app.views.your_view')) >>> r.text (prints output) >>> r.status_code

Saving image/file through django shell

跟風遠走 提交于 2019-11-30 11:24:40
问题 I am trying to save an image file through django shell. My model.py is: class user(models.Model): name=models.CharField(max_length=20) pic=models.ImageField() Everyhing is fine with admin and forms but I want to save image using shell: something like >>>user1=User(name='abc', pic="what to write here") 回答1: from django.core.files import File user1=User(name='abc') user1.pic.save('abc.png', File(open('/tmp/pic.png', 'r'))) You will end up with the image abc.png copied into the upload_to

How to execute a Python script from the Django shell?

早过忘川 提交于 2019-11-26 15:36:49
I need to execute a Python script from the Django shell. I tried: ./manage.py shell << my_script.py But it didn't work. It was just waiting for me to write something. codeape The << part is wrong, use < instead: $ ./manage.py shell < myscript.py You could also do: $ ./manage.py shell ... >>> execfile('myscript.py') For python3 you would need to use >>> exec(open('myscript.py').read()) danodonovan You're not recommended to do that from the shell - and this is intended as you shouldn't really be executing random scripts from the django environment (but there are ways around this, see the other

How to execute a Python script from the Django shell?

别说谁变了你拦得住时间么 提交于 2019-11-26 04:29:56
问题 I need to execute a Python script from the Django shell. I tried: ./manage.py shell << my_script.py But it didn\'t work. It was just waiting for me to write something. 回答1: The << part is wrong, use < instead: $ ./manage.py shell < myscript.py You could also do: $ ./manage.py shell ... >>> execfile('myscript.py') For python3 you would need to use >>> exec(open('myscript.py').read()) 回答2: You're not recommended to do that from the shell - and this is intended as you shouldn't really be