On my production environment only, my app uses America/Chicago as time zone while I have TIME_ZONE = "UTC" in my settings.
I'm running my code on Debian and the system locale is C.UTF-8 with Django 4.1.
I dug in Django code and find the source of my problem here: https://github.com/django/django/blob/c2118d72d61746f2462fca695dbf3adf44ebf8f7/django/utils/timezone.py#L72
I tried in ./manage.py shell to reproduce the problem:
# ./manage.py shell
Python 3.10.9 (main, Dec 21 2022, 08:51:48) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>> from django.utils import timezone
>>> import zoneinfo
>>> settings.TIME_ZONE
'UTC'
>>> timezone.get_current_timezone()
zoneinfo.ZoneInfo(key='America/Chicago')
And if I call the methods in get_current_timezone, I have a different result
>>> settings.USE_DEPRECATED_PYTZ
False
>>> zoneinfo.ZoneInfo(settings.TIME_ZONE)
zoneinfo.ZoneInfo(key='UTC')
Where could the America/Chicago come from ?
Willem was right, I used timezone.something() in my settings before the TIME_ZONE was set.
For posterity: I'm using django-split-settings and was using globs ("settings/*.py") and the order of the files was different locally and in production.
Related
I installed django but when i run command django-admin startproject send_email it gives an error showing zsh: command not found: django-admin what can i do please help me. i search same question in google but the answers are not working
pathparakh#Paths-MacBook-Air send_email % django-admin startproject send_email
zsh: command not found: django-admin
pathparakh#Paths-MacBook-Air send_email % python3
Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(3, 2, 5, 'final', 0)
>>>
I am going through the Django tutorial.
I thought the TIME_ZONE in settings.py was of form 'UTC-5', but it isn't.
I replaced it with 'America/Chicago'
However, when I do:
python manage.py shell
from django.utils import timezone
timezone.now()
I get 'UTC'
How do I get the timezone to take effect?
It can be confusing. If you run the code below, you will see that it is set, but it won't output as you expect:
from django.utils import timezone
timezone.get_current_timezone() # Should be 'America/Chicago'
timezone.now() # should show UTC
If you want it to output in the shell with your set timezone, use timezone.localtime()
from django.utils import timezone
timezone.localtime()
I am having trouble serving my staticfiles. Using django 2.0
This is the settings file,
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static')
STATIC_URL = '/static/'
I ran collectstatic and then every static file was copied in the STATIC_ROOT folder.
The thing is the admin static files and rest framework are being served, but not the ones which I have added.
(env) luvpreet#nfs:~/myntracms/myntracms$ python manage.py shell
Python 3.6.5 (default, Mar 29 2018, 03:28:50)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>> settings.BASE_DIR
'/home/luvpreet/myntracms/myntracms/myntracms'
See this,
luvpreet#nfs:~/myntracms/myntracms/static$ pwd
/home/luvpreet/myntracms/myntracms/static
luvpreet#nfs:~/myntracms/myntracms/static$ ls
admin debug_toolbar djcelery images js rest_framework
luvpreet#nfs:~/myntracms/myntracms/static$
Admin static files work,
But my files are not being served,
Even if I paste my files in the admin folder, still they are not being served.
luvpreet#nfs:~/myntracms/myntracms/static/admin/js$ ls | grep home
home.png
But still,
You have set STATIC_ROOT (where static files are collected to), but you haven't set STATICFILES_DIRS (where static files are collected from).
It looks as if you want
STATICFILES_DIRS = [os.path.join(os.path.dirname(BASE_DIR),'static')]
You should then change STATIC_ROOT to be a different directory (often outside of your Django project). You can also remove the directories that have been collected to ~/myntracms/myntracms/static/ (e.g. admin and django-debug-toolbar).
I am using django_cups in my project, and I am getting import error in the models line 4. Here is the models.py file
python ./manage.py runserver
...
File "/home/lex/myapp/django_cups/models.py", line 4, in <module>
import cups
ImportError: No module named cups
I have django_cups installed because when I import cups in the python shell it doesn't complain.
lex#lex-pc:~/django/mykapp$ python
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cups
>>>
>>>
But when I do it in the InteractiveConsole for Django, it complains...
(env)lex#lex-pc:~/django/myapp$ python ./manage.py shell
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import cups
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named cups
>>>
What could be the issue here?
The virtual environment, by default, does not include the system packages. To use both system and local packages, use virtualenv --system-site-packages when creating the environment.
I've written a simple django app to test ImageField, but I'm running into problem where upload_to just doesn't seem to work. Below is the code:
1 from django.db import models
2
3 # Create your models here.
4 class TestImage(models.Model):
5 img = models.ImageField(max_length=256, upload_to='images')
In my settings.py I have:
2 from os.path import dirname, join, abspath
3 __dir__ = dirname(abspath(__file__))
50 MEDIA_ROOT = join(__dir__, 'static')
55 MEDIA_URL = '/media/'
Then I start python shell using manage.py:
jchin#ubuntu:~/workspace/testimage$ ./manage.py shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import settings
>>> from image_app.models import TestImage
>>> p = TestImage(img='test.jpg')
>>> p.save()
>>> p.img.name
'test.jpg'
>>> p.img.path
u'/home/jchin/workspace/testimage/static/test.jpg'
>>> p.img.url
'/media/test.jpg'
As you can see from the result, django totally ignored my 'upload_to' paramater. Which I can't figure out why. From the documentation I should be expecting p.img.path to return "/home/jchin/workspace/testimage/static/images/test.jpg" and in the DB storing "images/test.jpg", correct? But the DB simply store the file name only:
mysql> select * from image_app_testimage;
+----+-----------+
| id | img |
+----+-----------+
| 1 | test.jpg |
+----+-----------+
1 rows in set (0.00 sec)
I checked all the documentations and I could not find what I am doing wrong. Anyone have any ideas? I'm using django 1.2.5 and it should support upload_to.
Please help!
John
But upload_to is for uploading, as the name implies. You're not uploading, you're assigning an image directly. It's only when you create a FileField object - by uploading from a form, for example - that upload_to is used.
Daniel Roseman is correct, upload_to is only used when creating a FileField object.
If you're doing something in a less-traditional manner (in my case, having a separate process place a file into a directory and simply informing Django of it's existence), to use the .url property on a FileField/ImageField, the following may work for you:
import os
from django.core.files.storage import FileSystemStorage
from django.db import models
class Video(models.Model):
video = models.FileField(
upload_to=settings.VIDEO_MEDIA_URL,
storage=FileSystemStorage(
location=settings.VIDEO_FILES_PATH,
base_url=os.path.join(settings.MEDIA_URL, settings.VIDEO_MEDIA_URL)
))
and in settings.py:
MEDIA_URL = '/media/'
VIDEO_MEDIA_URL = 'videos/' # Note: Trailing slash required.
Now, the url property should return the proper path:
>>> from test_app.models import Video
>>> p = Video(video='test.mp4')
>>> p.save()
>>> p.video.name
'test.mp4'
>>> p.video.path
u'/home/alukach/Projects/test/media_dir/videos/test.mp4'
>>> p.video.url
'/media/videos/test.mp4'
FYI, it's important to have enctype="multipart/form-data" part of the form declaration in the HTML template, otherwise the FileField/ImageField might not work appropriately.
This Works for me...
Models.py
img = models.ImageField(upload_to='static/images', default='', blank=True)
Admin.py
class TestImage(admin.ModelAdmin):
fieldsets = [['Some text', {'fields': ['img']}],]
now you can upload your image.