Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django [Postgreq] restore single field/column
I have a model with field creation_date = models.DateTimeField(auto_now_add=True). Doing some complex manipulations with migration file (converting model to multi-table inheritance and creating new objects) I've accidentally overwriten the field above. I have backups but need to overwrite only the single field creation_date as there are some fields in table are not align with current state. I guess there is a way to dump (id, creation_date) in JSON file and update the corresponding objects with for loop. Is there a way to pull data from gzip backup and save it to .json ? Or some more easier way to go? -
How to to debug DB errors in flaky frontend tests with django, htmx and playwright
I'm running a larger Django app that has a test suite that runs both on Debian Bookworm and Trixie. The tests use the test_server fixture and sqlite. We're currently experimenting with HTMX based views, which we test using playwright. A few weeks ago the test covering our new HTMX view started occasionally failing, but only ever on Trixie. The only relevant packages provided by the Debian Trixie host are sqlite 3.46 and python 3.13. Everything else comes in via pip and pyproject.toml in the same versions as on Bookworm. The test does not fail reliably and when it does fail, it doesn't always fail with the same error. The only commonality is that it always fails while trying to get the current session, in order to authenticate the request user (the tested view has an auth decorator). The error messages include django.contrib.auth.models.User.MultipleObjectsReturned: get() returned more than one User -- it returned 2! django/db/models/sql/compiler.py stepping out with List index out of range when trying to load the session cache django.db.utils.InterfaceError: bad parameter or other API misuse when trying to get the session cache. They mostly end up with AttributeError: 'SessionStore' object has no attribute '_session_cache' but there can be different issues … -
Django custom user model stores password in separate table — request.user causes "column users.password does not exist" error
i have created custom user in django class User(AbstractBaseUser, PermissionsMixin): which have password in user_credential table separately . what i want to request.user function i get error like ProgrammingError at /api/planer/user/ column users.password does not exist LINE 1: SELECT "users"."id", "users"."password", Ensured my custom authentication backend returns the correct user Confirmed AUTHENTICATION_BACKENDS is set properly Verified the User model has no password field -
Best way to design database tables for classified ads
I want to post classified ads. So in the context of designing the database is it better to put fields like body_type (which is not going to really change ever) or color into a separate table or keep as part of the overall model? -
Django + DigitalOcean Managed PostgreS: “remaining connection slots are reserved for roles with the SUPERUSER attribute” when using connection pooling
I’m trying to configure Django to use a connection pool against a DigitalOcean Managed PostgreSQL instance, but keep running into this error: OperationalError: connection failed: connection to server at "xxxxxxxxxxxx", port yyyy failed: FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute My DATABASES setting looks like this (Django 5.2+): DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': database_name, 'USER': database_user, 'PASSWORD': database_password, 'HOST': database_host, 'PORT': database_port, "OPTIONS": { 'sslmode': sslmode, 'pool': { 'min_size': 5, 'max_size': 150, 'timeout': 20, }, } } } Is this the correct way to enable pooling via Django’s built-in OPTIONS['pool'] settings? -
Why am I getting "ERR_CONNECTION_REFUSED" when querying by sidecar-hosted backend in Azure App Service?
I'm trying to deploy a fairly basic web app in Azure App Service. I have a backend image (running django+graphql) and and a frontend image (running vue+apollo), and when I run them locally either separately or via docker-compose it works with no issues. Once I deploy the app in Azure App Service, with the frontend as the main container, and the backend as the sidecar, I get "ERR_CONNECTION_REFUSED" when Apollo queries the graphql api in the backend. Apollo-config import { ApolloClient, InMemoryCache, } from "@apollo/client/core"; import { createApolloProvider } from "@vue/apollo-option"; import createUploadLink from "apollo-upload-client/createUploadLink.mjs"; const httpLink = createUploadLink({ uri: "http://localhost:8080/graphql", }); // Cache implementation const cache = new InMemoryCache(); // Create the apollo client export const apolloClient = new ApolloClient({ link: httpLink, cache, }); export const ApolloProvider = createApolloProvider({ defaultClient: apolloClient, }); Docker compose services: backend: container_name: backend build: context: ./backend ports: - "8080:8080" frontend: container_name: vue_frontend build: context: ./vue ports: - "80:80" Frontend dockerfile FROM node:22.14-alpine RUN npm install -g http-server WORKDIR /vue COPY package*.json ./ COPY .npmrc .npmrc RUN --mount=type=secret,id=npmrc,target=.npmrc npm install COPY . . EXPOSE 80 8080 CMD ["npm", "run", "dev", "--", "--port", "80", "--host"] Backend dockerfile FROM python:3.10-slim RUN mkdir /backend WORKDIR /backend ENV PYTHONDONTWRITEBYTECODE=1 … -
How can I properly test swagger_auto_schema for methods, request_body, and responses in drf-yasg with pytest?
I’m working on testing a Django REST Framework (DRF) CartViewSet using pytest, and I need to verify the swagger_auto_schema properties like the HTTP method, request body, and responses for different actions (e.g., add, remove, clear). I have the following code in my CartViewSet: class CartViewSet(GenericViewSet, RetrieveModelMixin, ListModelMixin): # Other viewset code... @swagger_auto_schema( method="post", request_body=AddToCartSerializer, responses={ 201: openapi.Response(description="Item added successfully."), 400: openapi.Response(description="Invalid input data"), }, ) @action(detail=False, methods=["post"], url_path="add") def add(self, request): # Logic for adding an item to the cart pass Now, I want to write a pytest unit test to check the following for the add method: HTTP Method: Ensure the swagger_auto_schema method is POST. Request Body: Ensure the correct serializer (AddToCartSerializer) is set for the request body. Responses: Verify that the response status codes (201 and 400) and their descriptions are properly set. Could someone guide me on how to properly test the swagger_auto_schema properties for method, request body, and responses in pytest? Any help or insights would be greatly appreciated! -
django : the page refreshed when i click on import file and no message appears
i am working on a django powered web app, and i want to customize admin view of one of my models. i have made a custom template for the add page and overrides save function in admin class to process input file before saving. here i have the admin class of RMABGD: @admin.register(RMABGD) class RMABGDAdmin(BaseModelAdmin): list_display = ('name', 'code_RMA', 'type_BGD', 'Partenaire', 'date_creation', 'RMA_BGD_state') list_filter = ('type_BGD', 'RMA_BGD_state', 'city') search_fields = ('name', 'code_RMA', 'Partenaire') add_form_template = "admin/spatial_data/RMABGD/change_form.html" change_form_template = "admin/spatial_data/RMABGD/change_form.html" def process_excel_import(self, request): excel_file = request.FILES.get('excel_file') if not excel_file: messages.error(request, "No file was selected. Please choose an Excel file.") return False try: df = pd.read_excel(excel_file) required_headers = ["code RMA", "code ACAPS", "Dénomination RMA", "Ville", "Adresse", "Longitude", "Latitude", "Type BGD", "Partenaire", "Date création", "Etat BGD RMA"] missing_headers = [header for header in required_headers if header not in df.columns] if missing_headers: messages.error(request, f"Missing required fields: {', '.join(missing_headers)}") return False else: # If all headers are correct, process data rows_imported = 0 errors = 0 for index, row in df.iterrows(): try: # Process row data obj = RMABGD( code_ACAPS=row["code ACAPS"], code_RMA=row["code RMA"], name=row["Dénomination RMA"], address=row["Adresse"], city=row["Ville"], location=f'POINT({row["Longitude"]} {row["Latitude"]})', type_BGD=row["Type BGD"], Partenaire=row["Partenaire"], date_creation=row["Date création"], RMA_BGD_state=row["Etat BGD RMA"] ) obj.save() rows_imported += 1 except Exception as … -
Error Running Spark Job from Django API using subprocess.Popen
I have created a Django project executable, and I need to run a Spark job from an API endpoint within this executable. I am using subprocess.Popen to execute the spark-submit command, but I am encountering an error when the command is executed. Here’s the command I am trying to run: /opt/spark-3.5.5-bin-hadoop3/bin/spark-submit --master local --deploy-mode client --conf "spark.ui.enabled=false" --conf "spark.ui.showConsoleProgress=false" --conf "spark.dynamicAllocation.enabled=false" --conf "spark.rdd.compress=false" --conf "spark.driver.memory=4g" --conf "spark.executor.memory=8g" --conf "spark.serializer=org.apache.spark.serializer.KryoSerializer" /Users/user1/Project/process-engine/route.py > /app/server/EAP/rasLite/engine/raslight/spark_submit_log/20250424/772_1.0_174702842893.log 2>&1 "{'processNo': '772', 'versionNo': '1.0', 'jsonData': '', 'executionDate': '', 'skipError': 'N', 'generated_executionid': '149897', 'isExecutionIdGenerated': 'True', 'executionId': '149897', 'isPreProcess': 'False'}" & However, I am getting the following error in the logs: Unknown command: 'C:/Users/user1/Project/process-engine/route.py' Type 'ras.exe help' for usage. Context: I am running this command from a Django API endpoint within a Django project executable. The path to route.py seems to be correct, but the error message indicates a Windows-style path (C:/Users/...) instead of the Unix-style path I am using (/Users/...). I am using the following code to execute the command: command = f'/Users/user1/Project/process-engine/spark-3.5.5-bin-hadoop3/bin/spark-submit --master local --deploy-mode client --conf "spark.ui.enabled=false" --conf "spark.ui.showConsoleProgress=false" --conf "spark.dynamicAllocation.enabled=false" --conf "spark.rdd.compress=false" --conf "spark.driver.memory=4g" --conf "spark.executor.memory=8g" --conf "spark.serializer=org.apache.spark.serializer.KryoSerializer" /Users/user1/Project/process-engine/route.py > {finalFilePath} 2>&1' final_command = command + " " + '\"' + raw_body_decoded1 + '\"' + … -
django-formset custom queryset
I am using the https://django-formset.fly.dev/ library, version 1.7.6. I'm trying to set a custom queryset on a ModelChoiceField, but I'm having trouble filtering the queryset based on the request object. Specifically, I want to filter and set the queryset to include only certain InRGPItem objects. Could you advise on how to properly set a custom queryset for ModelChoiceField in this context? forms.py # django libraries from django.forms.widgets import HiddenInput from django.forms.models import ModelChoiceField, ModelForm # django-formset libraries from formset.collection import FormCollection from formset.widgets import Selectize, DateInput, TextInput, Button from formset.renderers.bootstrap import FormRenderer as BootstrapFormRenderer # project models from rgp_entry_app.models import OutRGPEntry, OutRGPItem, InRGPItem class OutRGPItemForm(ModelForm): in_rgp_item = ModelChoiceField( label="In RGP Item", queryset=InRGPItem.objects.none(), # Using direct empty queryset instead empty_label="Select", # to_field_name="guid", # widget=Selectize( # search_lookup="name__icontains", # ), ) class Meta: model = OutRGPItem fields = ['in_rgp_item', 'sent_qty', 'note'] widgets = { 'note': Textarea(attrs={'rows': 1}), } class OutRGPItemCollection(FormCollection): outrgpitem = OutRGPItemForm() # ✅ repeatable formset items related_field = 'out_rgp_entry' legend = "Out RGP Items" min_siblings = 1 is_sortable = True ignore_marked_for_removal = True class OutRGPEntryCollection(FormCollection): outrgpentry = OutRGPEntryForm() outrgpitem_set = OutRGPItemCollection() legend = "Out RGP Entry" default_renderer = BootstrapFormRenderer( form_css_classes='row', field_css_classes={ # '*': 'mb-2 col-4', 'chalan_no': 'col-sm-4', 'chalan_date': 'col-sm-4', 'rgp_date': 'col-sm-4', … -
invalid_client or unsupported_grant_type in djnago testcases
This is my code which is a simple signup and login and I'm trying to login via oauth2 class UserAuthTests(TestCase): def setUp(self): self.client = APIClient() self.user = CustomUser.objects.create_user( username='test', password='strongpassword123', email='test@example.com' ) self.application = Application.objects.create( name="Test Application", client_type=Application.CLIENT_CONFIDENTIAL, authorization_grant_type=Application.GRANT_PASSWORD, skip_authorization=True, user=self.user, # Associate the application with the user ) self.application.save() def test_register_user(self): url = '/api/users/register/' data = { 'username': 'newuser', 'password': 'newpassword123', 'email': 'newuser@example.com' } response = self.client.post(url, data, format='json') print(response.data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.json()['username'], 'newuser') def test_login_user(self): url = '/o/token/' data = { 'grant_type': 'client_credentials', # i've tried with 'password' as well 'username': 'test', 'password': 'strongpassword123', 'client_id': self.application.client_id, 'client_secret': self.application.client_secret, } print(self.application.authorization_grant_type == 'password') print(f"Request Data: {data}") response = self.client.post(url, data=data, content_type="application/x-www-form-urlencoded") print(f"Response JSON: {response.json()}") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIn('access_token', response.json()) self.assertIn('refresh_token', response.json()) I'm getting Response JSON: {'error': 'unsupported_grant_type'} or Invalid Client, my settings file looks like this - OAUTH2_PROVIDER = { 'ACCESS_TOKEN_EXPIRE_SECONDS': 36000, # Set token expiration 'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.OAuthLibCore', } I have tried changing content_type, but it has not worked. Please let me know how I can fix this. In my postman, I've tried the same and it works, here is the curl - curl --location 'localhost:8000/o/token/' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'grant_type=password' \ --data-urlencode 'username=test' \ --data-urlencode 'password=strongpassword123' \ … -
Python django & server
I have a problem. I have placed my REST API project made in Python and Django on a VPS server on Ubuntu, but I can't configure it through nginx and gunicorn, but both of them are in the active running status. I fixed ngninx and gunicorn with chatgpt but still nothing happens when I enter ip -
Problème de Page non trouvée (404) dans Django [closed]
Bonjour, Je rencontre une erreur 404 lors de l'accès à la page d'accueil de mon application Django. Voici les détails de l'erreur : **Erreur : Page not found (404) reservation/accueil.html Détails de la requête : Request Method: GET Request URL: http://127.0.0.1:8000/ Raised by: reservations.views.accueil URLconf utilisé : Dans le fichier gestion_reservations.urls, Django a essayé de correspondre ces modèles d'URL, dans cet ordre : --admin/ --accounts/ --[name='accueil'] --Le chemin vide a été associé au dernier modèle. **Contexte : J'essaie d'afficher la page accueil.html dans le dossier reservation/, mais Django retourne une erreur 404. Mon fichier urls.py pour l'application semble être correctement configuré, mais la page ne se charge pas. **Voici une partie de mon code urls.py : from django.urls import path from . import views urlpatterns = [ path('', views.accueil, name='accueil'), # Autres chemins ] **Et voici la vue correspondante dans views.py : from django.shortcuts import render def accueil(request): return render(request, 'reservation/accueil.html') **Ce que j'ai essayé : --Vérifier la configuration des URL et des chemins des templates. --Relancer le serveur Django. --Vérifier que le fichier accueil.html est bien présent dans le dossier templates/reservation/. **Questions : --Pourquoi Django ne trouve-t-il pas le fichier accueil.html ? --Y a-t-il un problème dans ma configuration … -
How to check if a django queryset has a matching manytomany + foreignkey?
I try to check - in Djnago - if a user made queryset has two fields out of three which match 100%. class Foo(models.Model): # ... free_field = models.ForeignKey(FreeModel, ...) must_match_m2m_field = models.ManyToManyField(ManyModel, ...) must_match_fk_field = models.ForeignKey(BarModel, ...) # ... So, user generates a queryset of the "Foo" model (with several objects in it), which may contain many objects with a) different FreeModels, and b) precisely matching ManyModels & BarModels. In backend I need to check that all ManyModels and BarModels match in the query. for the ForeignKey objects I made following (I think its fair enough): def check_same_objects(object_list): return len(set(object_list)) == 1 check_bars = check_same_objects(qs_foo.values_list('must_match_fk_field', flat=True)) I made this for comparing the lists of objects in the manytomany fields of each object: def check_same_lists(object_lists): for lst in object_lists: if set(lst) != set(object_lists[0]): return False return True should_match_foos = [] for e in qs_foo: should_match_foos.append(e.must_match_m2m_field.all().values_list('id', flat=True)) check_manies = check_same_lists(should_match_foos) # finally checking both 'must-match-fields': check_both = all([check_bars, check_manies]) What is the more elegant pythonic/djnago variant of checking if theese two fields match 100% in the whole qs? -
Django Channels WebSocket connects but immediately closes (Live chat app)
I am building a live streaming website where users can watch a live class and interact via WebSocket chat. I have integrated Django Channels, Redis, and HLS.js for video playback. The chat connects via a WebSocket to Django Channels. However, the issue is: The WebSocket connects successfully, but immediately closes afterward. The browser console shows multiple attempts to reconnect, but the problem persists. There is no clear error message except "WebSocket connection closed" in the JavaScript console. My Setup Django 5.1.8 channels channels-redis Redis server is running (redis-cli ping returns PONG) Development server (local machine, runserver + daphne for Channels) Code Snippets HTML/JS Side (live_stream.html) <script> // WebSocket Chat functionality const streamId = "UDOOSDIHOH49849"; // Your stream_id let chatSocket = null; let reconnectAttempts = 0; const maxReconnectAttempts = 5; // You can fetch real username from Django template if available const username = "Anonymous"; // 🔥 You can dynamically change this later function connectWebSocket() { chatSocket = new WebSocket( 'ws://' + window.location.host + '/ws/chat/' + streamId + '/' ); chatSocket.onopen = function(e) { console.log('WebSocket connection established'); reconnectAttempts = 0; }; chatSocket.onmessage = function(e) { const data = JSON.parse(e.data); const chatBox = document.getElementById('chatMessages'); const newMessage = document.createElement('p'); newMessage.innerHTML = `<strong>${data.username}:</strong> ${data.message}`; … -
Product visible in Admin but not returned from database queries
I'm working on a Django project and I'm seeing a strange issue. There's a product that appears in the Django admin, but when I run queries (raw or Django ORM), it's not returned at all, as if it doesn't exist. I am using MySQL database. I checked the database and the object isn't saved there. -
How to use pagination with djangochannelsrestframework?
I'm using djangochannelsrestframework for my project and want to use Pagination. I found the PaginatedModelListMixin. This is my consumer: class UserConsumer(GenericAsyncModelAPIConsumer): queryset = User.objects.all() serializer_class = UserSerializer pagination_class = WebsocketPageNumberPagination @model_observer(User) async def user_activity(self, message, observer=None, **kwargs): await self.send_json(message) @user_activity.serializer def user_activity_serializer(self, instance, action, **kwargs): return { "action": action.value, "data": UserSerializer(instance).data, } async def connect(self): await self.accept() await self.user_activity.subscribe() The GenericAsyncModelAPIConsumer is just a wrapper for all the CRUD mixins class GenericAsyncModelAPIConsumer( PaginatedModelListMixin, CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, DeleteModelMixin, GenericAsyncAPIConsumer, ): pass The WebsocketPageNumberPagination should be a wrapper for the rest_framework's PageNumberPagination, but it didn't work for me. I send the request with a js WebSocket like this: class ModelWebSocket extends WebSocket { items = reactive([]) constructor(url, protocols, pk = 'id') { // Call the parent constructor super(url, protocols) // List all items when the connection is opened this.onopen = () => { console.debug('[WS] Connected') this.list() } // Handle incoming messages this.onmessage = (event) => { const message = JSON.parse(event.data) console.log('[WS] Message', message) // Some more stuff, but the message is the interessting } // Close and error handling // ... } list() { return new Promise((resolve, reject) => { const requestId = this.#getAndSetPendingRequest(resolve, reject) this.send( JSON.stringify({ action: 'list', request_id: requestId, page_size: … -
Using Asych in Django views to connect to LiveKit Backend throws missing arguments
I'm new to asynch side of django rest framework. I currently have a django rest api with Django v5 with all functions written in synchronous views. However I'm attempting to add a webrtc calling feature using the Livekit server.I'm attempting to connect my django rest api to LiveKit Server(self hosted on ubuntu 22.04) using this documentation(https://github.com/livekit/python-sdks) to create a room before connecting. The documentation clearly states RoomService uses asyncio and aiohttp to make API calls. It needs to be used with an event loop. Here is my code for the same : # Creating a room # RoomService uses asyncio and aiohttp to make API calls. It needs to be used with an event loop. async def createLiveKitRoom(self, request): request_data = request.data.dict() serializer = CreateLiveKitRoomSerializer(data=request_data) serializer.is_valid() data = serializer.validated_data room_uuid = data.get("room_uuid") # Will read LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET from environment variables lkapi = LiveKitAPI( "http://${nginx_sfu_media_server_intra_chat_ip}:${sfu_media_server_intra_i_chat_port}" ) room_info = await lkapi.room.create_room( CreateRoomRequest(name=room_uuid, empty_timeout=10 * 60, max_participants=20) ) print(room_info) await lkapi.aclose() return room_info asyncio.run(createLiveKitRoom()) I first have to create a room_uuid on my django end using the usual put synchronous call(which I already have) and pass this room_uuid to the above asynch call so that the room is created on livekit … -
I am trying to import categories but lines that have a parent is causing an issue
I have an empty product category table and I am trying to import categories from a csv file and I can import other categories for other apps but not for products so I had the csv file checked and I'm told it's fine. I get the following errors and they all start at the line that has a parent. Line number: 29 - ProductCategory matching query does not exist. Line number: 30 - ProductCategory matching query does not exist. Line number: 31 - ProductCategory matching query does not exist. Line number: 32 - ProductCategory matching query does not exist. Line number: 33 - ProductCategory matching query does not exist. If it's a new import why is it looking for an existing category? If I remove all the lines that has a parent then I can import and after importing and putting the lines back that has a parent I get the same error again, which now does not make sense. Here is my admin.py file: from django.contrib import admin from .models import * from import_export import resources from import_export.admin import ImportExportModelAdmin class CategoryResource(resources.ModelResource): class Meta: model = ProductCategory fields = ("id", "name", "description", "parent") class ProductCategoryAdmin(ImportExportModelAdmin): list_display = ("name", "id", … -
Dynamic initial values for a Django Model
How would i go about prepopulating a field in the Wagtail Admin UI with HTTP GET parameters? I have seen some solutions using the deprecated contrib.ModelAdmin, but have not really found something using the new ModelViewSet. My simplified use case would be a simple calender in the Admin UI (using some javascript calender like fullcalandar.js) where i would create a new Event by dragging a timeframe and having the browser visit an url like /admin/event/new?start=startdate&end=enddate showing the Event Form with the start and end fields being prepoulated by the timeframe. I have the following model class Event(models.Model): title = models.CharField(max_length=255) [...] class EventOccurrence(Orderable): event = ParentalKey(Event, on_delete=models.CASCADE, related_name='event_occurrence') start = DateTimeField() end = DateTimeField() So far i have tried to use an Custom Form Class inherting from WagtailAdminModelForm, which works nicely for the prepopulating, but i have no way to access the request object to fetch the GET paramters. Helpful AIs would like me to use the deprecated ModelAdmin or inject some javascript to prepopulate the fields on the frontend. My personal hail mary would be to create the event via an API and the just refer the user to the freshly created event, but i would like to avoid … -
Translate external lib text Django
I currently use Base64ImageField from drf_extra_fields in a serializer. This class uses INVALID_FILE_MESSAGE = _("Please upload a valid image.") and _ comes from from django.utils.translation import gettext_lazy as _ I implemented translations (from english to french) with pyhton manage.py makemessages and pyhton manage.py compilemessages to us them. But to translate the error message from Base64ImageField, i did not found any relevant solutions: Write the translation myself in django.po file, but as soon as i make ```python manage.py makemessages`` it's going to be commented out. Avoid my custom translation being commented out by having a file.py with _("Please upload a valid image.") Write a custom makemessages command to avoid translation being commented out Is there any other solution or only "dirtry tricks" work for this issue ? -
Overriding Collectstatic and Manifestic file storage
I am trying to integrate terser js minification in a django collectstatic command , by overriding the CompressedManifestStaticFileStorage of whitenoise in below code , but the def _save() method minifies the copied files but the hashed files are either stored as original or empty due to content.close() method i call during the minification. How can i bypass this so that both the original files and hashed files are minified with terser. class MinifiedCompressedManifestStaticFilesStorage(CompressedManifestStaticFilesStorage): def minify_js(self, content_str): """Minify JavaScript using Terser and validate output.""" terser_path = ( os.path.abspath("./node_modules/.bin/terser.cmd") if os.name == "nt" else "./node_modules/.bin/terser" ) try: command = f'"{terser_path}" -m -c' if os.name == "nt" else [terser_path, "-m", "-c"] # Explicitly specify Terser CLI path if installed locally result = subprocess.run( command, input=content_str.encode("utf-8"), capture_output=True, check=True, ) minified = result.stdout if not minified: raise ValueError("Terser returned empty output") return minified except (subprocess.CalledProcessError, FileNotFoundError, ValueError) as e: print(f"Minification failed: {str(e)}. Using original content.") return content_str.encode("utf-8") # Fallback to original def _save(self, name, content): if name.endswith(".js"): # Read and close original content content_str = content.read().decode("utf-8") content.close() # Minify and create new ContentFile minified_bytes = self.minify_js(content_str) content = ContentFile(minified_bytes, name=name) content.seek(0) # Reset pointer for parent class return super()._save(name, content) -
Upwork Graphql job search api correct query and pagination the one on docs is not working
Documenation link : https://www.upwork.com/developer/documentation/graphql/api/docs/index.html#mapping-jobs my query query = """ fragment MarketplaceJobpostingSearchEdgeFragment on MarketplaceJobPostingSearchEdge { node { title description createdDateTime skills { name } duration job { contractTerms { contractType hourlyContractTerms { engagementType } } } client { location { country } totalFeedback totalPostedJobs totalHires verificationStatus totalReviews } } } fragment PageInfoFragment on PageInfo { hasNextPage endCursor } query marketplaceJobPostings( $marketPlaceJobFilter: MarketplaceJobFilter $searchType: MarketplaceJobPostingSearchType $sortAttributes: [MarketplaceJobPostingSearchSortAttribute] $pagination: PaginationInput ) { marketplaceJobPostings( marketPlaceJobFilter: $marketPlaceJobFilter searchType: $searchType sortAttributes: $sortAttributes pagination: $pagination ) { totalCount edges { ...MarketplaceJobpostingSearchEdgeFragment } pageInfo { ...PageInfoFragment } } } """ -
How to create an account approval admin page in Django
I'm trying to create an admin page in Django to approve user accounts. Here are the specific requirements: Background I have a landlord registration system. Each landlord must provide personal information and details about their rental property when registering. Landlord accounts are inactive by default. Administrators need to review the property information before activating the account. Main Models # User model class User(AbstractUser): user_type = models.CharField(max_length=10, choices=UserType, default=UserType.TENANT) phone_number = models.CharField(max_length=10, unique=True, blank=True, null=True) # Other fields... # Property model class Property(models.Model): owner = models.ForeignKey("accounts.User", on_delete=models.CASCADE, related_name="properties") name = models.CharField(max_length=256) status = models.CharField(max_length=10, choices=PropertyStatus.choices, default=PropertyStatus.PENDING) # Other fields... Question I want to create a Django admin page that: Displays a list of unapproved landlord accounts When clicking on an account, shows detailed user information and their registered property details Has functionality to approve or reject the account (if the property is approved, the account will be activated) I've thought about using a custom ModelAdmin with readonly_fields to display detailed information, but I'm not clear on the best way to: Display information from multiple models (User and Property) in the same admin page Add actions to approve/reject accounts What's the best way to implement this? Code examples would be very helpful. … -
CORS Error between 2 local projects Django and React
I have a Django Project running on this url http://127.0.0.1:8000, with those settings DEBUG = True INSTALLED_APPS = [ ... 'corsheaders', ... ] MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', ... 'django.middleware.common.CommonMiddleware', ... ] ALLOWED_HOSTS = [] CORS_ALLOWED_ORIGINS = [ 'http://localhost:5173', 'http://127.0.0.1:5173', ] I have a React project running on this url : http://localhost:5173 with this App.jsx file function App() { // Fetch tournaments from the server const [tournaments, setTournaments] = useState([]); useEffect(() => { fetch('http://127.0.0.1:8000/api/tournaments', { method: 'GET', mode: 'cors', }) .then(response => response.json()) .then(data => setTournaments(data)) .catch(error => console.error('Error fetching tournaments:', error)); }, []); return ( <> <TournamentList tournaments={tournaments} /> </> ) } This request http://127.0.0.1:8000/api/tournaments is working using Postman But I have this basic cors error when using the React Front Access to fetch at 'http://127.0.0.1:8000/api/tournaments' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. What do I miss here ?