Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Channels: group_send() getting delayed while sending to active window when handling multiple websocket consumers
Background: I'm working on a chat application on my local (development) server. Python 3.9.5, Django 3.2, Channels 3.0.3. Each user can see an "all messages" section on the page, containing messages from all users, and a "my messages" section, containing only his own messages. Goal: All messages should be sent to all users, and each user's messages should be separately sent only to him/her. If the user is logged in via multiple browser tabs/ devices (this scenario is supposed to be handled by Channels as per the docs here), and the user sends a message from one window, it needs to immediately send back (echo) the message to all his windows, including the window where the message was sent from. For this, I'm adding each connection to a group when it is first opened. Consumers.py async def websocket_connect(self, event): await self.send({ "type": "websocket.accept" }) await self.channel_layer.group_add("master", self.channel_name) user = self.scope['user'] user_group = "user_" + str(user.id) if user.is_authenticated: await self.channel_layer.group_add(user_group, self.channel_name) As you can see, the master group is for all users, and the user_group is for all connections of this particular user. Further: async def websocket_receive(self, event): user = self.scope['user'] user_group = "user_" + str(user.id) ....some code... await self.channel_layer.group_send( "master",{ … -
Querying related models that has foreign key to model in question (in one optimized query)
I got the following models. class Person(models.Model): name = models.CharField(max_length=200) class PhoneNumber(models.Model): person = models.ForeignKey(Person) number = models.CharField(max_length=200) class Email(models.Model): person = models.ForeignKey(Person) email = models.CharField(max_length=200) class WebSite(models.Model): person = models.ForeignKey(Person) url = models. URLField(max_length=200) I want to query all the models from already obtained Person model since this should be my start point. I can do something like; person = Person.objects.get(id=1) phones = PhoneNumber.objects.filter(person=person) emails = Email.objects.filter(person=person) website = WebSite.objects.filter(person=person) # ... However, the snippet above would hit the DB four times and I don't want that at all. Is there any way to make a query like the one below to hit the DB once (after person is already queried, which would be two in total at most)? query = Person.objects.get(id=1).getPhoneNumber(filters).getEmails(filters).getWebSites(filters) As you know prefetch_related also results in hitting DB more than once. I don't wanna use select_related since I would need to use other models to get to Person model, which actually will have no difference when it comes to how many times it would hit to the DB since I got multiple models. I am struggling to make a very performant query with the task above. Any help is much appreciated! -
Json data to CSV format using Python
I have JSON data I want to convert to a CSV file. How can I do this with Python? Below, is the json structure. { "data": [ { "matter_id": 1, "billing_clientId": 1, "billing_contactID": 1, "branch_code": "8032FHDSL", "category": "sadsaddda", } ] } what i tried below, but it's throw me ValueError: "Invalid file path or buffer object type: <class 'collections.OrderedDict'> " for record in serializer.data: import pandas as pd import json df = pd.read_json(record) print('df----->', df) df.to_csv("/home/satyajitbarik/test.csv", index = None) -
`coreapi` must be installed for schema support
I am trying to generate swagger docs using the drf_yasg and it was working fine but now its not. It's giving me error and I can not find solution for it. Any idea why am I getting this error ? Request Method: GET Request URL: http://0.0.0.0:8007/doc/ Django Version: 3.2.4 Python Version: 3.9.5 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.staticfiles', 'rest_framework', 'drf_yasg', 'promotions', 'vendor', 'settings'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'includes.common.middlewares.identity.IdentityMiddleware', 'includes.common.middlewares.timezone.TimezoneMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch response = self.handle_exception(exc) File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception self.raise_uncaught_exception(exc) File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception raise exc File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch response = handler(request, *args, **kwargs) File "/usr/local/lib/python3.9/site-packages/drf_yasg/views.py", line 92, in get generator = self.generator_class(info, version, url, patterns=[]) File "/usr/local/lib/python3.9/site-packages/drf_yasg/generators.py", line 183, in __init__ self._gen = SchemaGenerator(info.title, url, info.get('description', ''), patterns, urlconf) File "/usr/local/lib/python3.9/site-packages/rest_framework/schemas/coreapi.py", line 120, in __init__ assert coreapi, '`coreapi` must be installed for schema support.' Exception Type: AssertionError at /doc/ Exception Value: `coreapi` must … -
Getting errors when launching Elastic Beanstalk instance in AWS for Django app
I'm using Python 3.9.5 for my Django app and I am trying to deploy it on AWS in Elastic Beanstalk. The platform I chose was AWS Linux 2 with Python 3.8. When I try to do an eb create, I get this stacktrace error: ---------------------------------------- /var/log/eb-engine.log ---------------------------------------- cmd_obj.run() File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/setuptools/command/build_ext.py", line 79, in run _build_ext.run(self) File "/usr/lib64/python3.8/distutils/command/build_ext.py", line 340, in run self.build_extensions() File "/usr/lib64/python3.8/distutils/command/build_ext.py", line 449, in build_extensions self._build_extensions_serial() File "/usr/lib64/python3.8/distutils/command/build_ext.py", line 474, in _build_extensions_serial self.build_extension(ext) File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/setuptools/command/build_ext.py", line 196, in build_extension _build_ext.build_extension(self, ext) File "/usr/lib64/python3.8/distutils/command/build_ext.py", line 528, in build_extension objects = self.compiler.compile(sources, File "/usr/lib64/python3.8/distutils/ccompiler.py", line 574, in compile self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) File "/usr/lib64/python3.8/distutils/unixccompiler.py", line 129, in _compile raise CompileError(msg) distutils.errors.CompileError: command 'gcc' failed with exit status 1 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-v7gtd0uf/cld2-cffi_20eacbc571034a60bc5a47d71b1737a7/setup.py", line 154, in <module> setup( File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/setuptools/__init__.py", line 153, in setup return distutils.core.setup(**attrs) File "/usr/lib64/python3.8/distutils/core.py", line 148, in setup dist.run_commands() File "/usr/lib64/python3.8/distutils/dist.py", line 966, in run_commands self.run_command(cmd) File "/usr/lib64/python3.8/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/setuptools/command/egg_info.py", line 299, in run self.find_sources() File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/setuptools/command/egg_info.py", line 306, in find_sources mm.run() File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/setuptools/command/egg_info.py", line 541, in run self.add_defaults() File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/setuptools/command/egg_info.py", … -
awsebcli 'SyntaxError- no encoding declared' in terminal upon executing any eb command
I finished django project and during deployment run into error- after installing awsebcli package I try to run eb commands in terminal but get this error: File "C:\Users\Bartski\AppData\Local\Programs\Python\Python37\Scripts\eb.exe", line 1 SyntaxError: Non-UTF-8 code starting with '\x90' in file C:\Users\Bartski\AppData\Local\Programs\Python\Python37\Scripts\eb.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details It shows the same error no matter what eb command I try to run. awsebcli installs only exe version so I am not able to change the encoding manually. I tried many solutions given to similar problems but none of them worked so far. Any ideas how can I make it work? -
django.db.utils.IntegrityError: (1062, "Duplicate entry '1' for key 'user profile.PRIMARY'")
I am very new to Django, creating rest API for my android application In my signup form I am taking parameter like email, password, firstname, lastname, contact etc... Every time new user signup, user profile data will also created based on that specific user id and those user id will should be unique in both the tables. When I am trying to signup, facing the below issue : django.db.utils.IntegrityError: (1062, "Duplicate entry '1' for key 'user profile.PRIMARY'") models.py from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager from django.db import models from django.contrib.auth.models import AbstractUser from django.db.models.signals import post_delete, post_save from django.dispatch import receiver from django.utils.timezone import now from django.conf import settings import uuid class UserManager(BaseUserManager): def create_user(self, email, password=None): if not email: raise ValueError('User must have an email address') user = self.model( email = self.email, ) user.set_password(password) user.save() return user class User(AbstractBaseUser): objects = UserManager() email = models.EmailField(unique=True, db_index=True) is_active = models.BooleanField('active', default=True) is_admin = models.BooleanField('admin', default=False) USERNAME_FIELD = 'email' ordering = ('id',) def is_staff(self): return self.is_admin def has_perm(self, perm, obj=None): return True def has_module_perms(self, app_label): return True def get_short_name(self): return self.email def get_full_name(self): return self.email def __unicode__(self): return self.email class Meta: db_table = 'user' class UserProfile(models.Model): user = models.OneToOneField(User, on_delete = models.CASCADE, … -
Trying to learn javascript integration in Django
I am trying to learn javascript so that I can improve my Django application. I'm trying to vary images with a javascript function based on whether the URL link exists or not, but it's not working: Here's my javascript function: // Creates an object which can read files from the server var reader = new XMLHttpRequest(); var checkFor = "https://res.cloudinary.com/.../{{ object.id }}.png"; // Opens the file and specifies the method (get) // Asynchronous is true reader.open('get', checkFor, true); //check each time the ready state changes //to see if the object is ready reader.onreadystatechange = checkReadyState; function checkReadyState() { if (reader.readyState === 4) { //check to see whether request for the file failed or succeeded if ((reader.status == 200) || (reader.status == 0)) { //page exists -- redirect to the checked //checked for page document.location.href = checkFor; } else { var checkFor = "https://res.cloudinary.com/.../{{ object.system }}.png" return checkFor } }//end of if (reader.readyState === 4) }// end of checkReadyState() // Sends the request for the file data to the server // Use null for "get" mode reader.send(null); Then I try to call 'checkFor in my HTML template: <img src="checkFor"class="mx-auto" width="250px" onerror='this.onerror = null; this.src="https://res.cloudinary.com/.../image.svg"'/> I just get the default image, when … -
Blending image with background in HTML
In the following HTML document, the Microsoft logo does not blend with the background image. Does anyone know how you can blend the logo with the background image or remove the logo's background colour? I have tried using the command mix-media-mode: multiply but the image still did not blend properly. Thank you for your help. <!DOCTYPE html> <html lang="en"> <head> <style> body { background-image: url('https://i.pinimg.com/564x/09/01/f0/0901f088d78988a5b6bc66af7cfbfec8.jpg'); background-repeat: no-repeat; background-attachment: fixed; background-size: cover; } </style> </head> <body> <img src= "https://i.pinimg.com/564x/09/05/85/090585401505f54e69a1a5e218759171.jpg" height="35px" alt=""> </body> </html> -
(Stuck!) Django operational error: table "accounts_profile" already exists
Long story short. I tried to add a custom user model to my existing project and realized too late that it wouldn't work well as I already started my project. Then I deleted the migrations, all the customusermodel related codes and re ran makemigrations and migrate. Nothing wrong showed up at this point. Then I started following a tutorial to create a profile model to link to the default User model and everything was fine until I wanted to create a new superuser and django showed this error: django.db.utils.OperationalError: no such table: auth_user. Then, I saw similar errors online and and followed one of the solutions which was to fake migrate the app (accounts for me) and then re run migrations. So I did that but then this error shows up when I try to run Python manage.py migrate: django.db.utils.OperationalError: table "accounts_profile" already exists. Today, I have ventured into far too much unknown territory of django and now I am in a mess which I have no idea how to solve and so I need help. I would not like to delete my database as it contains existing data and the website is also live. What do you guys recommend … -
how to print content inside UserSerializer type - django rest framework
Hi I am trying to create user register using django rest frame work. So far I have been following this link https://stackoverflow.com/a/29391122/13240914, and I am still new in django, the thing that I would like to ask is.. is there a way to print content inside UserSerializer type.. here is part of the code class UserSerializer(serializers.ModelSerializer): password = serializers.CharField(write_only=True) def create(self, validated_data): print(self) print(self.data) user = UserModel.objects.create_user( username=validated_data['username'], password=validated_data['password'], ) return user Here is the result when I print self: UserSerializer(context={'request': <rest_framework.request.Request: POST '/create/user/'>, 'format': None, 'view': <face_detector.views.CreateUserView object>}, data={'username': 'hello', 'password': 'hello123456'}): username = CharField(help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, validators=[<django.contrib.auth.validators.UnicodeUsernameValidator object>, <UniqueValidator(queryset=User.objects.all())>]) password = CharField(write_only=True) and when I print print(self.data) I only get {'username': 'hello'} so, what I would like to print is data={'username': 'hello', 'password': 'hello123456'} -
How to show a form when you click a button in django
I am creating a blog website using django. I am implementing the feature to comment on blogs with a form, and it works. The only thing is that, I want there to be a button Add a comment, and only when the user clicks on the button it will show the comment form. The best example I can give is StackOverflow and how it's commenting works. Thank You! -
The view soldagem.views.update_ieis didn't return an HttpResponse object. It returned None instead
I am getting this error when I try to instantiate an object. I use redirect and I don't understand why this one is not returning. view.py This method works in other views. from django.shortcuts import render, redirect def create_ieis(request): form_ieis = IEISForm(request.POST or None) if form_ieis.is_valid(): form_ieis.save() return redirect('soldagem:ieis') def form_ieis(request, pk): data = {} data['db'] = IEIS.objects.get(pk=pk) data['form_ieis'] = IEISForm(instance=data['db']) return render(request, 'soldagem/form.html', data) models.py Here I have FK for other apps. I don't think this is why. class IEIS (models.Model): nome = models.CharField (max_length=25,null=False, blank=False) especificacao = models.CharField (max_length=20,null=True, blank=True, unique=False) espec_material = models.ForeignKey (EspecMaterial, on_delete=models.CASCADE) diametro1 = models.ForeignKey (Diametros,related_name='diametro_nps1', on_delete=models.CASCADE) FNUMBER_CHOICES = ( (1, '1 a 5'), (2, '1 e 4 '), (11, '11') ) fnumber = models.CharField(max_length=10, choices=FNUMBER_CHOICES) us_tp = models.PositiveIntegerField (null=True, blank=True) rx_tp = models.PositiveIntegerField (null=True, blank=True) dz_tp = models.PositiveIntegerField (null=True, blank=True) class Meta: ordering = ['nome'] verbose_name = 'IEIS' verbose_name_plural = 'IEISs' def __str__(self): return str (self.nome)``` **form.html** <form name="form_ieis" id="form_ieis" action="{% if db %}/update_ieis/{{db.id}}/{%else%}/create_ieis/{% endif %}" method="post"> {% csrf_token %} {{form_ieis.as_p}} <input type="submit" class="btn btn-success" value="Salvar"> </form> Thank you for any attention. -
Django not accepting request from client using bearer token from react native
I have been stuck on this for a couple days. If I pass the bearer token as hard coded it works but if I use props or a variable it gets rejected.. I am not too sure what is wrong. I also added a check in the component before to make sure the jwt is there. import { StyleSheet, Text, View, Image, SafeAreaView, TouchableOpacity, ScrollView, } from "react-native"; import React, { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; var axios = require("axios"); const FriendsList = (props) => { useEffect(() => { var config = { method: "get", url: "http://127.0.0.1:8000/getuserinfo/", headers: { Authorization: `Bearer ${props.jwt}`, }, }; const fetchD = async () => { const res = await axios(config).then(function (response) { console.log(JSON.stringify(response.data)); }); res(); }; fetchD(); }, []); return ( <View> <Text>FriendList</Text> </View> ); }; export default FriendsList; Here is the component before the above, I can login but when I pass the bearer token to the server I get back a 400 response not authorized :0 import { StyleSheet, Text, View, Image, SafeAreaView, TouchableOpacity, ScrollView, } from "react-native"; import React, { useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { accessCookie, … -
How should I create my project so it is easily shareable and won't require the user to install additional packages?
I am writing a small coding exercise I need to share with someone, who will run it on their system. The project uses Django+Celery and runs a search using a CLI tool installed on a Docker image (from Dockerhub). I am wondering what the best solution is for creating a neatly-packaged project with the least amount of user setup required. I can think of only two options here: I could host the entire project on a Docker container built from the Dockerhub image. Since the base image (which I am required to use for the tool it has) does not have Python installed, I would need to use the Dockerfile to install Python, Celery, sqlite3+libsqlite3-dev, as well as redis-server for Celery using the package manager (the image is Debian) For brief context, I run the search using subprocess.Popen("cmd") in a Celery task. I could instead change this to subprocess.Popen("docker run cmd") and use the Docker image only for the CLI tool it has, instead of attempting to host the entire project with Docker. The issue here is that Docker requires sudo, and it seems like very bad practice to run subprocess.Popen("sudo docker run cmd") if it would even work. Are … -
How to record activities of different types?
I am writing an application that would allow a user to record things that occur in their life. For example, you could record Drinks, Breakfast, Lunch, Dinner, Exercise, Supplements, Mood, Sleep Quality, etc. Below are the data models that I have thus far. How should I record the activity of the user when the category of the activity may be different? For example: YYY-MM-DD TIME Breakfast food_id YYY-MM-DD TIME Lunch food_id YYY-MM-DD TIME Exercise 30 minutes from django.contrib.admin.options import ModelAdmin from django.contrib.admin.sites import NotRegistered from django.db import models from django.conf import settings class Food(models.Model): class Meta: verbose_name = "food" verbose_name_plural = "foods" def __str__(self): return self.label food_id = models.BigAutoField(primary_key=True) label = models.CharField(max_length=64) notes = models.CharField(max_length=200, blank=True, null=True) user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) class Unit(models.Model): class Meta: verbose_name = "unit" verbose_name_plural = "units" def __str__(self): return self.label unit_id = models.BigAutoField(primary_key=True) label = models.CharField(max_length=64, unique=True) class Ingredient(models.Model): class Meta: verbose_name = "ingredient" verbose_name_plural = "ingredients" def __str__(self): return self.label ingredient_id = models.BigAutoField(primary_key=True) label = models.CharField(max_length=64, unique=True) class Allergen(models.Model): class Meta: verbose_name = "allergen" verbose_name_plural = "allergens" def __str__(self): return self.label label = models.CharField(max_length=64) class FoodIngredient(models.Model): class Meta: verbose_name = "food_ingredient" verbose_name_plural = "food_ingredient" def __str__(self): return self.recipe_id food_ingredient_id = models.BigAutoField(primary_key=True) food_id = models.ForeignKey(Food, … -
How to get value from "object_list" returned by listview
I want to check the particular value from "object_list" returned by ListView in a template. So I wrote code this way. {% object_list.<primary key>.<column> %} Example {% if object_list.ABCD1234.item_name == "myitem" %} ... do something ... {% endif %} In this example, ID(primarykey) is "ABCD1234" Column name is "item_name" and value in "item_name" is "myitem". Maybe I wrote something wrong, it does not work at all. Please teach me how to fix the code to get value from object_list to make the above conditional branching works well. I just mentioned the above settings in this question but still if more code is required then tell me I'll update my question with that information. Thank you -
Django password reset form randomly pre-populating email field
Our system is using the default Django auth password managment, including the password reset view: django.contrib.auth.views.PasswordResetView We override the form used by the view django.contrib.auth.forms.PasswordResetForm by: Adding a simple captcha field to the form (https://pypi.org/project/django-nocaptcha-recaptcha/) Overriding clean_email Overriding send_mail from django.contrib.auth.forms import PasswordResetForm from nocaptcha_recaptcha.fields import NoReCaptchaField from django.contrib.auth import get_user_model from common.utils import send_password_reset_email ... class MyPasswordResetForm(PasswordResetForm): captcha = NoReCaptchaField() def clean_email(self): """Ensure the email address exists in the system.""" email = self.cleaned_data['email'] email_in_db = get_user_model().objects.filter(email__iexact=email).exists() if not email_in_db: raise forms.ValidationError(NO_EMAIL_MESSAGE) user = get_user_model().objects.get(email__iexact=email) if user and not user.password: raise forms.ValidationError( mark_safe( 'Your account doesnt have a password yet, click this ' f'<a href="{reverse("accounts:password-setup")}">Password ' # noqa: E901 'Setup</a> link to setup your password.' ) ) return email.lower() def send_mail(self, *args, **kwargs): """Sends the templated password reset email to the appropriate user.""" email = self.cleaned_data['email'] user = get_user_model().objects.get(email__iexact=email) send_password_reset_email(user) We've been getting some very odd behaviour with the form being initialised to whatever the last email that was reset in the system. This is inconsistent. I can visit the page, and refresh it 10 times, and 3 of the 10 times will initialise the form with an email, while the other times it is blank. -
Django: How do I authenticate an AbstractBaseUser?
I have created a model for an AbstractBaseUser, but am struggling to authenticate it. Here is my model: from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.contrib.auth.password_validation import validate_password from uuid import uuid4 class CustomUserManager(BaseUserManager): def _create_user(self, email, password, is_staff=False, is_superuser=False, **other_fields): if not email: raise ValueError('Email address must be specified') if not password: raise ValueError('Password must be specified') user = self.model( email=self.normalize_email(email), is_staff=is_staff, is_superuser=is_superuser, **other_fields ) validate_password(password) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password, **other_fields): return self._create_user(email, password, False, False, **other_fields) def create_superuser(self, email, password, **other_fields): return self._create_user(email, password, True, True, **other_fields) class CustomUser(AbstractBaseUser): id = models.UUIDField(primary_key=True, default=uuid4(), editable=False, unique=True) email = models.EmailField(max_length=254, unique=True) is_superuser = models.BooleanField(default=True) is_staff = models.BooleanField(default=True) is_active = models.BooleanField(default=True) is_premium = models.BooleanField(default=False) first_name = models.CharField(max_length=40) last_name = models.CharField(max_length=40) display_name = models.CharField(max_length=40) date_of_birth = models.DateField() currency = models.CharField(max_length=3) date_joined = models.DateTimeField(auto_now_add=True) date_modified = models.DateTimeField(auto_now=True) REQUIRED_FIELDS = ['first_name', 'display_name', 'date_of_birth', 'currency'] USERNAME_FIELD = 'email' EMAIL_FIELD = 'email' objects = CustomUserManager() def get_full_name(self): return ("%s %s" % (self.first_name, self.last_name)) if self.last_name != '' else self.first_name def get_short_name(self): return self.first_name def __str__(self): return "%s %s - %s" % (self.first_name, self.last_name, self.email) Here is my view: from django.contrib.auth import authenticate from django.http import HttpResponse def user_login(request): user … -
Download bz2, Read compress files in memory (avoid memory overflow)
As title says, I'm downloading a bz2 file which has a folder inside and a lot of text files... My first version was decompressing in memory, but Although it is only 90mbs when you uncomrpess it, it has 60 files of 750mb each.... Computer goes bum! obviusly cant handle like 40gb of ram XD) So, The problem is that they are too big to keep all in memory at the same time... so I'm using this code that works but its sucks (Too slow): response = requests.get('https:/fooweb.com/barfile.bz2') # save file into disk: compress_filepath = '{0}/files/sources/{1}'.format(zsets.BASE_DIR, check_time) with open(compress_filepath, 'wb') as local_file: local_file.write(response.content) #We extract the files into folder extract_folder = compress_filepath + '_ext' with tarfile.open(compress_filepath, "r:bz2") as tar: tar.extractall(extract_folder) # We process one file at a time: for filename in os.listdir(extract_folder): filepath = '{0}/{1}'.format(extract_folder,filename) file = open(filepath, 'r').readlines() for line in file: print(line) Is there a way I could make this without dumping it to disk... and only decompressing and reading one file from the .bz2 at a time? Thank you very much for your time in advance, I hope somebody knows how to help me with this... -
Django Vs .Net for AWS Glacier
I'm starting to plan a new web app for the company that I'm currently working for. The idea is to create a web app where the user will do all the basic operations (upload, delete, update, etc) on AWS Glacier. Users basically will admin the account of AWS Glacier, create vaults, tags, upload files, delete files, etc.. all the options AWS Glacier has to offer. There are going to be several users each user with different AWS Glacier accounts and of course each user with unique AWS configurations. Another important aspect is the security of the information of each user (the AWS Glacier credentials), I believe I'll need to somehow encrypt this information of each user. I'm having second thoughts if I should develop this project with Django or .Net. What do you think? or maybe is there a third option y should consider besides Django and .Net? -
'Order' object has no attribute 'get_cart_items'
I have no errors in Vs Code but keep getting this in Django, any ideas? Any help much appreciated (: Django AttributeError at / 'Order' object has no attribute 'get_cart_items' Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 3.2.4 Exception Type: AttributeError Exception Value: 'Order' object has no attribute 'get_cart_items' Exception Location: /Users/alessiolivolsi/Work/pp/ecommerce/ecommerce/ecommerce/store/views.py, line 12, in store Python Executable: /usr/local/opt/python@3.9/bin/python3.9 Python Version: 3.9.5 Python Path: ['/Users/alessiolivolsi/Work/pp/ecommerce/ecommerce/ecommerce', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/site-packages'] Server time: Mon, 28 Jun 2021 23:21:08 +0000 views.py from django.shortcuts import render from django.http import JsonResponse import json from .models import * def store(request): if request.user.is_authenticated: customer = request.user.customer order, created = Order.objects.get_or_create(customer=customer, complete=False) items = order.orderitem_set.all() cartItems = order.get_cart_items else: items = [] order = {'get_cart_total':0, 'get_cart_items':0} cartItems = order['get_cart_items'] products = Product.objects.all() context = {'products':products, 'cartItems':cartItems} return render(request, 'store/store.html', context) def cart(request): if request.user.is_authenticated: customer = request.user.customer order, created = Order.objects.get_or_create(customer=customer, complete=False) items = order.orderitem_set.all() else: items = [] order = {'get_cart_total':0, 'get_cart_items':0} context = {'items':items, 'order':order} return render(request, 'store/cart.html', context) def checkout(request): if request.user.is_authenticated: customer = request.user.customer order, created = Order.objects.get_or_create(customer=customer, complete=False) items = order.orderitem_set.all() else: items = [] order = {'get_cart_total':0, 'get_cart_items':0} context = {'items':items, 'order':order} return render(request, 'store/checkout.html', context) def updateItem(request): data = json.loads(request.body) … -
Supervisor within Ansible playbook not running Gunicorn
I want my Ansible playbook to use supervisor to start a Gunicorn server that runs my django project. The playbook that I have right now runs completely through, however Gunicorn does not seem to be running on completion. Here is the current playbook: --- - hosts: sorb become: true vars: product: "sorb" vars_files: - "../../group_vars/sorb" - "../../../../ansible/group_vars/base" roles: - role: base min_mnt_size: 30 daemon_logs: - nginx logs: - file: "{{ log_json }}" category: python-json - file: "{{ log_unhandled }}" category: unhandled-output cron: condition: "{{ not_dev }}" jobs: - sessioncleaner - role: offline # "offline" follows "base" to prevent alerts during deploy - role: install_env #install dep to avoid creation errors - role: pip_virtualenv #this installs pip reqs and the virtual env uses requirements.txt - role: github repositories: #clone in common and sorb from github - "common" - "sorb" - role: gunicorn - role: oefenweb.supervisor - role: supervisor templates: - "../../../gunicorn/templates/supervisor/gunicorn" - role: online - role: done Here is the gunicorn role tasks main.yml: --- - name: Render gunicorn.conf.py template: src: gunicorn/gunicorn.conf.py.j2 dest: "{{ gunicorn_config_path }}" owner: "{{ nginx_user }}" group: "{{ nginx_group }}" mode: "{{ '0o666' if is_vbox else '0o655' }}" notify: Restart Supervisor Thank you!! -
Django Hide unavailable dates
I have a simple application to have an appointment with a doctor. I would like to have an appointment when the doctor is available and hide the occupation date in my form. form: class ContactForm(ModelForm): class Meta: model = Appointment fields = ('doctor', 'date', 'timeslot', 'patient_name') model : from django.db import models class Appointment(models.Model): class Meta: unique_together = ('doctor', 'date', 'timeslot') TIMESLOT_LIST = ( (0, '09:00 – 09:30'), (1, '10:00 – 10:30'), (2, '11:00 – 11:30'), (3, '12:00 – 12:30'), (4, '13:00 – 13:30'), (5, '14:00 – 14:30'), (6, '15:00 – 15:30'), (7, '16:00 – 16:30'), (8, '17:00 – 17:30'), ) doctor = models.ForeignKey('Doctor',on_delete = models.CASCADE) date = models.DateField(help_text="YYYY-MM-DD") timeslot = models.IntegerField(choices=TIMESLOT_LIST) patient_name = models.CharField(max_length=60) def __str__(self): return '{} {} {}. Patient: {}'.format(self.date, self.time, self.doctor, self.patient_name) @property def time(self): return self.TIMESLOT_LIST[self.timeslot][1] class Doctor(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20) middle_name = models.CharField(max_length=20) specialty = models.CharField(max_length=20) def __str__(self): return '{} {}'.format(self.specialty, self.short_name) @property def short_name(self): return '{} {}.{}.'.format(self.last_name.title(), self.first_name[0].upper(), self.middle_name[0].upper()) -
How to pass a list variable from java script to html?
I need to create a list "task_list" (in script part) based on a variable "letters_per_word" that is randomly drawn (either 1 or 2): [0,1] or task_list=[0,1,2], respectively. task_list = get_list(letters_per_word) Then I need to use "task_list" inside for loop: {% for l in task_list%} .... {% endfor %} The problem is that I cannot access task_list in that loop I tried using document.getElementById("task_list").value = task_list But it worked as if 'task_list' was empty How can I pass 'task_list' to use it in 'for' loop?