Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
NGINGX gives 502 Bad Gateway when trying to use NGINX + Gunicorn on Docker
I'm developing a Django app with a PostgreSQL database and I'm using NGINX + Gunicorn with Docker. PostgreSQL, NGINX and Gunicorn are on different containers communicating with networks. I can build my app with docker-compose build but when I execute it with docker-compose up and view my app in the browser all I get is a 502 Bad Gateway error and in the terminal all I see is this: nginx_1 | 127.0.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.0" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "172.23.0.1" nginx_1 | 172.23.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.1" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "-" My docker-compose looks like this: version: '3' services: # Database container db: image: postgres:10 volumes: - db_volume:/var/lib/postgresql/data env_file: - ./.env networks: - db_network # Web app container with gunicorn webapp: build: . env_file: ./.env volumes: - .:/opt/services/webapp/src - static:/opt/services/webapp/static - media:/opt/services/webapp/media networks: - db_network - nginx_network depends_on: - db # NGINX (Reverse proxy) container nginx: image: nginx:1.13 ports: - 8000:80 volumes: - ./config/nginx/conf.d:/etc/nginx/conf.d - static:/opt/services/webapp/static - media:/opt/services/webapp/media networks: - nginx_network depends_on: - webapp networks: db_network: driver: bridge nginx_network: driver: bridge volumes: db_volume: static: media: And this is my Dockerfile: … -
Grouping the list and converting the response to JSON
a question. there is a sample from the database sample uses Python2.7 + Django-1.67 + SqlAlchemy How can you convert such a selection into a grouped list so that the output is json, for example: [Russia -> [Moscow -> [Lenina, Pushkina], Tomsk -> [Nekrasova]], ...] There are about 600 records, issued through api, every time I load through request filters to the database, I think it is expensive, can be as soon as possible with such a list to work to group? -
How to convert excel or csv file to html table in django?
I have a small application where user can submit an excel or csv file and I want to display the data in that file to the user on the template. How can I do this? I know about libraries like openpyxl and xlrd, they're both python libraries and can loop through the file and display the values, but then getting those values over to the template seems like too much work (If that's the only way to do it, I'm going to do it). Is there another way? This is what I have so far: def home(request): if request.method == 'POST': uploaded_file = request.FILES['my_excel'] # What should I do here? return (render(request, 'demo/index.html')) -
Override default value in hubspot module
Lets say I created a hubspot module with an image field. In this field a default is provided. Now, suppose I have two different email templates which have a banner, and naturally an image, is there a way to override the default image provided initially to provide flexibility when including the snippet? i.e. - {% module "..." module_id="..." label="...." my_field='overrided value' %} This doesn't work, or I wouldn't be here..., so is there a variant syntax for this or is this not doable? -
Django Heroku no module named 'my app name'
I'm trying to deploy my first django app to heroku. I was able to migrate to db and create superuser, but now I'm stuck on this: [2018-05-19 22:51:01 +0000] [4] [INFO] Listening at: http://0.0.0.0:31247 (4) 2018-05-19T22:51:01.283512+00:00 app[web.1]: [2018-05-19 22:51:01 +0000] [4] [INFO] Using worker: sync 2018-05-19T22:51:01.287214+00:00 app[web.1]: [2018-05-19 22:51:01 +0000] [8] [INFO] Booting worker with pid: 8 2018-05-19T22:51:01.292792+00:00 app[web.1]: [2018-05-19 22:51:01 +0000] [8] [ERROR] Exception in worker process 2018-05-19T22:51:01.292795+00:00 app[web.1]: Traceback (most recent call last): 2018-05-19T22:51:01.292796+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker 2018-05-19T22:51:01.292797+00:00 app[web.1]: worker.init_process() 2018-05-19T22:51:01.292798+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process 2018-05-19T22:51:01.292800+00:00 app[web.1]: self.load_wsgi() 2018-05-19T22:51:01.292801+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi 2018-05-19T22:51:01.292802+00:00 app[web.1]: self.wsgi = self.app.wsgi() 2018-05-19T22:51:01.292803+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi 2018-05-19T22:51:01.292804+00:00 app[web.1]: self.callable = self.load() 2018-05-19T22:51:01.292806+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load 2018-05-19T22:51:01.292807+00:00 app[web.1]: return self.load_wsgiapp() 2018-05-19T22:51:01.292808+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp 2018-05-19T22:51:01.292809+00:00 app[web.1]: return util.import_app(self.app_uri) 2018-05-19T22:51:01.292810+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app 2018-05-19T22:51:01.292811+00:00 app[web.1]: __import__(module) 2018-05-19T22:51:01.292816+00:00 app[web.1]: ModuleNotFoundError: No module named 'whispering-shelf-25150' 2018-05-19T22:51:01.292905+00:00 app[web.1]: [2018-05-19 22:51:01 +0000] [8] [INFO] Worker exiting (pid: 8) 2018-05-19T22:51:01.329968+00:00 app[web.1]: [2018-05-19 22:51:01 +0000] [4] [INFO] Shutting down: Master 2018-05-19T22:51:01.330127+00:00 app[web.1]: [2018-05-19 22:51:01 +0000] [4] [INFO] Reason: Worker failed to boot. 2018-05-19T22:51:01.413026+00:00 heroku[web.1]: … -
Change DOM ID of model field
I have an email model field: class AdvertisePost(Post): email = models.EmailField(max_length=120, default="support@website.com") and an accompanying form: class AdvertisePostForm(forms.ModelForm): class Meta: model = AdvertisePost fields = [ 'email' ] How can I change the DOM ID of email from the automatcally generated id_email to a custom ID? -
AttributeError: 'NoneType' object has no attribute 'schema_metadata_enabled' django-cassandra-engine
I just install django-cassandra-engine and create new django project, adding cassandra db in settings.py DATABASES = { 'default': { 'ENGINE': 'django_cassandra_engine', 'NAME': 'test', 'TEST_NAME': 'testname', 'HOST': '127.0.0.1', 'OPTIONS': { 'replication': { 'strategy_class': 'SimpleStrategy', 'replication_factor': 1 } } } And adding string INSTALLED_APPS = ['django_cassandra_engine'] + INSTALLED_APPS But when I type in cmd python manage.py runserver I will get this error: Performing system checks... System check identified no issues (0 silenced). Unhandled exception in thread started by <function check_errors. <locals>.wrapper at 0x000001D3BBD9CC80> Traceback (most recent call last): File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\utils\autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\core\management\commands\runserver.py", line 123, in inner_run self.check_migrations() File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\core\management\base.py", line 427, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\db\migrations\executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\db\migrations\loader.py", line 49, in __init__ self.build_graph() File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\db\migrations\loader.py", line 206, in build_graph self.applied_migrations = recorder.applied_migrations() File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\db\migrations\recorder.py", line 61, in applied_migrations if self.has_table(): File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\db\migrations\recorder.py", line 44, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django\db\backends\base\base.py", line 255, in cursor return self._cursor() File "D:\Users\Pukasava\Anaconda3\lib\site- packages\django_cassandra_engine\base\__init__.py", line 152, in _cursor if not connection.cluster.schema_metadata_enabled and \ AttributeError: 'NoneType' object has no attribute 'schema_metadata_enabled' What I doing wrong? Why I get this error? -
Objects getting deled from Django
I am using django 1.9 with python 2.7 connected with postgres as backend. When I perform operations on a row stored in postgres, when an exception occurs, the row under operation gets deleted from the backend. I also tried having sqlite3 as the backend and had the same issue happen. Any ideas on why this is happening? Thanks! -
Overriding (globally) field label suffix in Django
I want to remove the colons from the ModelForm in Django (version 1.11). I used the following code in forms.py file: class User_accountModelForm(forms.ModelForm): # to remove colons from the labels: def __init__(self, *args, **kwargs): kwargs.setdefault('label_suffix', '') super(User_accountModelForm, self).__init__(*args, **kwargs) class Meta: model = User_account fields = ['first_name', 'last_name', 'user_name', 'password'] Here is the view class: class User_accountFormView(View): form_class = User_accountModelForm template_name = 'templatePath' def get(self, request, *args, **kwargs): form = self.form_class(None) return render(request, self.template_name, {'form':form}) The code is working with no error. the registration form is displayed correctly. The only thing is the colon (:) still there. It seems to me the init function for the label_suffix is not called. Any help with this is appreciated. -
django import error: Default storage finder
Please guys I have been stuck here for a while, problem is I don't even know the source of the problem. I have gone through my code over and over again but couldn't find anything. I use django 2.0 and python 3.6 on pycharm ception in thread started by .wrapper at 0x0000000C0EAF4158> Traceback (most recent call last): File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\module_loading.py", line 20, in import_string return getattr(module, class_name) AttributeError: module 'django.contrib.staticfiles.finders' has no attribute 'DefaulStorageFinder' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run self.check(display_num_errors=True) File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 364, in check include_deployment_checks=include_deployment_checks, File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 351, in _run_checks return checks.run_checks(**kwargs) File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\staticfiles\checks.py", line 7, in check_finders for finder in get_finders(): File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\staticfiles\finders.py", line 277, in get_finders yield get_finder(finder_path) File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\staticfiles\finders.py", line 286, in get_finder Finder = import_string(import_path) File "C:\Users\USER1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\module_loading.py", line 24, in import_string ) from err ImportError: Module "django.contrib.staticfiles.finders" does not define a "DefaulStorageFinder" attribute/class -
Django queries gets slower over time
I have a django application and a mysql database table having around 30,000 entries. I have to process each entry one by one, do some computation and store the result in database. When I start processing, the time taken to process 100 entries is around 40 seconds. But this time keeps on growing. So after processing 1000 entries, the time goes to 1 minute, then after processing 2000 entries, the time to compute 100 entries move to 1 minute 30 seconds. If I stop the server, and start computing again from say 2000th entry, then the time taken to process 100 entries becomes 40 seconds again, but keeps on increasing as more entries are processed. Does anyone know why is this happening? -
Unable to firgure out how ModelForm is rendered using generic UpdateView
I'm trying to figure out how the ModelForm is instantiated when I'm using generic UpdateView. I've gone through the django source code and looked into UpdateView and relevant Form classes but I can't see any line of code where we are explicitly passing instance to the object of ModelForm class. For example, say we have PostForm as a ModelForm then we would have written : form = PostForm(instance=Post.object.get(pk=pk)) to render the form from the models object. I can't see similar code in the django source code and can't figure out how the generic ModelForm is getting populated in case of UpdateView i.e. how the self.instance attribute of my form is getting instantiated when I submit data after POSTing the form. -
How to send POST request to Django REST API using python requests
I have a simple rest api which I want to send a post request to using requests. Inside my view I have: class ProductList(generics.ListAPIView): serializer_class = ProductSerializer def post(self, request, format=None): print('THIS IS A POST REQUEST') queryset = [product.name for product in Product.objects.all()] return Response(queryset) And I am trying to send a post request using: response = requests.post('http://127.0.0.1:8080/') However this returns a 403, and the print statement isn't printed. I have done some research and I think it may have something to do with the CSRF token not being there but i'm not to sure how to add that. Does anybody know how I can get the post request to work? -
F() expressions in django keeps giving me 0
I am about to update approx 2-3k of records for one of my django model. I know that using the update() would help but then because I am actually using the same value in the field and change it into something else for example having a function to scrumble the existing field. I figured there is something called F() would should be able to help me out with it but somehow, it keeps saving my values as 0 into the field. I did not get any error and have no idea where to look into. this is the tutorial I read https://docs.djangoproject.com/en/1.8/ref/models/expressions/ this is my example code before I really do such batch update for the 2-3k records # I used the below, gives me 0 when I look at my db querysets_of_model = Model.objects.filter(id__in[111, 123]).update(name=F('name') + ' testing') # this gives me 0 too querysets = Model.objects.filter(id__in[111,123]) querysets.update(name=F('name') + ' testing') # without using the F(), which of course gives me `testing only` in the name field qs = Model.objects.filter(id__in[111, 123]).update(name='testing only') what I originally wanted to do is something like this qys = Model.objects.filter().update(name=scrumble_fun(F('name))) the scrumble_fun is just a function that scrumble the existing value then return the … -
Difficulty with Django Migrations
What are some good best practices to prevent the following. Often, during django development I use postgres. If I roll back to an earlier commit, often new migrations will break things irreparably, because the postgres context is unknown to the older code. I have tried the syncdb etc, I have tried makemigrations on single apps, but neither seem to fix the problem. Also, if creating a new postgres db and deleting all the migrations and pycache folders, this too often causes problems that are not straightforward to fix. Is the answer to simply use sqlite during development, has anyone else encountered this? Details: django 2.0.5 postgres: most current. -
Do some stuff before saving form with CreateView
I want to populate GenericIPAddress Field before saving an instance in CreateView. It has to be done in View not in Form Class, because of access to request variable. Can I do that? Which method I had to override and how? Code I'm working with: models.py class Message(models.Model): author = models.CharField(max_length=60) email = models.EmailField() message = models.TextField(max_length=1200) ip_address = models.GenericIPAddressField(null=True, blank=True) timestamp = models.DateTimeField(auto_now_add=True) is_read = models.BooleanField(default=False) def __str__(self): return self.author + '/' + self.message[:20] forms.py class MessageForm(forms.ModelForm): class Meta: model = Message fields = ['author', 'email', 'message'] labels = { 'author': 'Autor', 'email': 'E-mail', 'message': 'Wiadomość' } views.py class SendMessage(CreateView): form_class = MessageForm template_name = 'blog/send_message.html' def post(self, request, *args, **kwargs): result = super(SendMessage, self).post(request, *args, **kwargs) ip, is_routable = get_client_ip(request) if ip: self.fields['ip_address'] = ip #I do it wrong return result -
AWS CodeBuild breaking on os.environ - python can't find environment variable
So I've built a Django application, and used the AWS CodeStar Django template (EC2, EB). The first thing I did was change the SECRET_KEY to pull from an environment variable like so os.environ['DJANGO_SECRET_KEY']. I ssh'ed into the EC2 instance and added this variable, and I also added this variable to the EB environment (in modify configuration). However, CodeBuild still breaks on the build. Where do I need to add this environment variable? -
Page not found erro with drf-nested-routers
I'm using Django: 2.0 Djanfo REST Frameword: 3.8.2 drf-nested-routers: 0.90.2 My contacts/views.py class ContactViewSet(viewsets.ModelViewSet): serializer_class = ContactSerializer permission_classes = (IsAuthenticated,) def get_queryset(self): return Contact.objects.filter(user=self.request.user) class ContactPhoneNumberViewSet(viewsets.ModelViewSet): serializer_class = ContactPhoneNumberSerializer permission_classes = (IsAuthenticated,) def get_queryset(self): print(self.kwargs) phone_numbers = ContactPhoneNumber.objects.all() return phone_numbers and app/urls.py from rest_framework_nested import routers from contacts.views import ContactViewSet, ContactPhoneNumberViewSet router = routers.SimpleRouter() router.register(r'contacts', ContactViewSet, 'contacts') contact_router = routers.NestedSimpleRouter(router, r'contacts', lookup='contact') contact_router.register(r'phone_number', ContactPhoneNumberViewSet, base_name='contact-phone-numbers') api_urlpatterns = [ path('', include(router.urls)), ] urlpatterns = [ path('api/', include(api_urlpatterns)), url(r'^admin/', admin.site.urls), ] using this setup, I'm able to access /api/contacts/ # <= list all contacts /api/contacts/<pk>/ # <= contact detail But on trying to access /api/contacts/<pk>/phone_number/ # <= list all phone numbers It is giving Page Not Found error. I also tried passing <phone_number_pk> but still Page not Found error is received. -
How to create a model for xml with child items
I need to return an xml that looks like the following <ussd> <type>T</type> <msg>MSG</msg> <premium> <cost>C</cost> <ref>R</ref> </premium> </ussd> How can I create a model for that responce. I am using django-rest-framework and django-rest-framework-xml Initially I have the model as class WASPResponse(models.Model): """Response to give back to Vodacom""" type = models.CharField(max_length=255) msg = models.CharField(max_length=255) I am stuck on the premium part. Can someone help. And is it necessary to implement a model for this situation. -
Celery executing parsing file function before the download of the file completes. I am using django +celery
I have a celery task that requires to download a file from remote server and then parse it. While I tried to it with normal function download() first and then parse(). But celery is executing the parse function even before it downloads which is causing a error is there a way to synchronize these two executions? file.download() #calling function to download file file.parse() #calling function to parse the file #followed by more code -
Django rest framework, change ForeignKey
i'm fighting with DRF too long so now i must ask question.. How change ForeignKey to another? I have user profile and relation to status model. models.py class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) ldap_uid = models.CharField(max_length=100, blank=True, null=True, default=None) redmine_id = models.IntegerField(blank=True, null=True, default=None) status = models.ForeignKey(Status, models.SET_NULL, blank=False, null=True, default=DEFAULT_STATUS_ID) location = models.ForeignKey(Location, models.SET_NULL, blank=False, null=True, default=DEFAULT_LOCATION_ID) online = models.BooleanField(default=False) class SelectValuesModel(models.Model): name = models.CharField(max_length=100) display_name = models.CharField(max_length=100) def __str__(self): return self.name class Meta: abstract = True class Status(SelectValuesModel): pass class Location(SelectValuesModel): pass What is good way to change Profile status to another? I'm trying with something like this without success views.py class UserStatusView(viewsets.ViewSet): def partial_update(self, request, pk=None): user = User.objects.get(pk=pk) user_profile = user.profile new_stauts = Status.objects.get(request.data.status) serialized_data = ProfileSerializer(user_profile) if(serialized_data.is_valid()): serialized_data.save(status=new_stauts) return Response(serialized_data.errors) And trying send new id via PATCH. I'm trying tto find solution but no success here too. And how do it good? Make another route for updating Profile status? Or make something like profile/1/update_status/2? Now my routing looks like: router = routers.DefaultRouter() router.register(r'users', views.UserViewSet) router.register(r'redmine', views.RedmineCurrentTaskView, base_name='redmine') router.register(r'parameters', views.ParametersView, base_name='parameters') router.register(r'update_status', views.UserStatusView, base_name='update_status') router.register(r'debug', views.DebugStatus, base_name='debug') urlpatterns = [ path('', views.index, name='index'), path('api/', include(router.urls)) ] -
Django adds extra field to the GROUP BY clause
Here is a query: speed_by_computer = ChartPoint.objects.filter( date__gte=start, date__lt=end, ).values('computer_id') \ .annotate(max_speed=Max('speed')) Here is an SQL query produced by this query: SELECT "computer_id", MAX("speed") AS "max_speed" FROM "api_chartpoint" WHERE ( "date" >= 2018-05-17 08:00:00+00:00 AND "date" < 2018-05-17 09:00:00+00:00 ) GROUP BY "computer_id", "date" ORDER BY "date" DESC Note that there is an unexpected date in the GROUP BY clause. Why is it here? Here is what I expected: SELECT "computer_id", MAX("speed") AS "max_speed" FROM "api_chartpoint" WHERE ( "date" >= 2018-05-17 08:00:00+00:00 AND "date" < 2018-05-17 09:00:00+00:00 ) GROUP BY "computer_id" -
Modifying value on serialization - Django Rest Framework
I have a model which contains sensitive data, let's say a social security number, I would like to transform that data on serialization to display only the last four digits. I have the full social security number stored: 123-45-6789. I want my serializer output to contain: ***-**-6789 My model: class Employee (models.Model): name = models.CharField(max_length=64,null=True,blank=True) ssn = models.CharField(max_length=16,null=True,blank=True) My serializer: class EmployeeSerializer(serializers.ModelSerializer): id = serializers.ReadOnlyField() class Meta: model = Employee fields = ('id','ssn') read_only_fields = ['id'] -
Creating list of usernames from User model in Django
Im trying to make a user list for my multiselectfield. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') users = User.objects.values_list('id','username') authorized = MultiSelectField(choices=users, null=True) but the problem is that later on in my html file i can't compare those values with a logged user id {% if latest_question_list %} <ul> {% for question in latest_question_list %} {% for person in question.authorized %} {% ifequal person request.user.id %} <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li> {% endifequal %} {% endfor %} </ul> {% endfor %} Thanks in advance -
How filter queryset with a calculation?
I have a model similar to class House: x = IntegerField() y = IntegerField() I want to return the houses that are at a smaller distance that distance from some position using the following calculation: abs(house.x - myPos.x) + abs(house.y - myPos.y) < distance But I don't know how to make that filter since filter() only compares with a model's field. I would like to do something like: House.objects.filter(abs(x - myPos.x) + abs(y - myPos.y) < distance)