Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Why setUpTestData has an argument and setUp doesn't?
Pretty much the title. In django, when creating tests, we have two options, setUp and setUpTestData. I understand that setUp will be called each time a single test runs, while setUpTestData will be called only once and be used in multiple tests. I don't understand what the cls does in setUpTestData(cls), though. And why setUp() doesn' t have that argument? -
django custom admin login page
I have a custom auth mechanism for users and I want to use the same for django admin. All works fine for authenticated users but if an unauthenticated user opens the url /admin he is redirected to /admin/login with the std. login page for admin. I want to redirect to auth/sign-in or block the page. urlpatterns = [ path('admin/', admin.site.urls), path('admin/login', SignInView.as_view(), name='admin/login'), ... Redefining the url like in the code block does not work. Any idea? -
Django Form is not submitting (No POST action on HTML form)
I have created a Django Form but it does not submit after hitting the submit Button. It seems like the HTML is broke because if i hit the submit button Django doesn´t output a POST Action. Template is as follows: <form method="POST"> {% csrf_token %} <div class="relative transform overflow-hidden rounded-lg bg-gray-800 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg"> <div class="bg-gray-50 dark:bg-gray-800 px-4 pt-5 pb-4 sm:p-6 sm:pb-4"> <div class="sm:flex sm:items-start"> <div class="mt-3 text-center text-gray-200 sm:mt-0 sm:ml-4 sm:text-left"> {{ addForm.email }} {{ addForm.givingLessons }} </div> </div> </div> </div> <input type="submit" class="inline-flex w-full justify-center rounded-md border border-transparent bg-indigo-600 px-4 py-2 text-base font-medium text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 sm:ml-3 sm:w-auto sm:text-sm" value="Update" /> </form> views.py def manage_students(request): if request.method == "POST": form = addStudentGivingLessons(request.POST) if form.is_valid(): lessons = form.cleaned_data.get('givingLessons') print(lessons) return redirect('home') else: print("form is not Valid") else: Forms.py class addStudentGivingLessons(forms.Form): email = forms.EmailField() givingLessons = forms.ModelMultipleChoiceField(widget = forms.CheckboxSelectMultiple, queryset = Faecher.objects.all()) -
Django GROUP BY without aggregate
I would like to write the following query in Postgresql using Django ORM: SELECT t.id, t.field1 FROM mytable t JOIN ... JOIN ... WHERE .... GROUP BY id Note that there is NO aggregate function (like SUM, COUNT etc.) in the SELECT part. By the way, it's a perfectly legal SQL to GROUP BY primary key only in Postgresql. How do I write it in Django ORM? I saw workarounds like adding .annotate(dummy=Count('*')) to the queryset (but it slows down the execution) or introducing a dummy custom aggregate function (but it's a dirty hack). How to do it in a clean way? -
is_valid() don't work with my form in Django
i'm a beginner with Django 4.1. I'm using model to edit a form and render it in a webpage. This run correctly. When i click on submit, my function recognize the POST method but never validate the form. I'm sure that i have to fix a bug in my models or my form but i don't know where. in models.py class Auction(models.Model): HOUSE = "HOU" MOTORS = "MOT" PROPERTY = "PPT" HOBBIES = "HOB" INFORMATION_TECHNOLOGY = "IT" MUSIC = "MUS" BOOK = "BOK" CATEGORY_CHOICES = [ (HOUSE, "All for your House"), (MOTORS, "Car, Moto, Boat"), (PROPERTY, "Houses, flats, manors"), (HOBBIES, "Hobbies"), (INFORMATION_TECHNOLOGY, "Laptop, Desktop, Mobile Phone"), (MUSIC, "CD, Musical Intrusments"), (BOOK, "Books, Comics,...") ] ONE = 1 THREE = 3 SEVEN = 7 FOURTEEN = 14 DURATION_CHOICES = [ (ONE, "1 day"), (THREE, "3 days"), (SEVEN, "7 days"), (FOURTEEN, "14 days") ] title = models.CharField(max_length=64) description = models.TextField(max_length=500, blank=True) creation_date = models.DateTimeField(auto_now_add=True) image = models.URLField(null=True, blank=True, default="") user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="seller") duration = models.CharField(max_length=7, choices=DURATION_CHOICES, default=SEVEN) category = models.CharField(max_length=3, choices=CATEGORY_CHOICES, default=INFORMATION_TECHNOLOGY) price = models.DecimalField(max_digits=12, decimal_places=2, default=0.0) def __str__(self): return f"{self.id}: {self.title}" in forms.py class CreateListingsForm(forms.ModelForm): class Meta: model = Auction fields = ["title", "description", "image", "category", "duration", "price"] and … -
How to automatically and regularly update database for a web application?
I am new to web development and I have a fundamental question on developing a website that takes queries from the users and searching inside a database. I am developing a website that takes queries from users and searches in an existing database. I already developed the web app part with Django. The app takes some keywords, searches the database, and displays the returned data in a table form. The database is made out of several csv files that are irregularly updated, say every one or two weeks. They are located in some local storage, say /home/james/csv_files/. To develop the website, I first processed the csv files in my local machine and made them into db.sqlite3 file, the default(?) database that comes with Django. I uploaded these to GitLab, including the db.sqlite3, and deployed to see if it works, and it worked file. But current implementation always searches in the uploaded db.sqlite3. I am looking for an automatic way that regularly process the csv files in /home/james/csv_files/ to update the database and let the web app to use the updated database. Where should I start? Currently, there is a python script that reads /home/james/csv_files/*.csv and merge them into /home/steven/project/db.sqlite3. Application … -
Password reset confirmation link redirects to page not found 404
DJOSER = { 'LOGIN_FILED': 'email', 'USER_CREATE_PASSWORD_RETYPE': True, 'USERNAME_CHANGED_EMAIL_CONFIRMATION':True, 'PASSWORD_CHANGED_EMAIL_CONFIRMATION':True, 'SEND_CONFIRMATION_EMAIL':True, 'PASSWORD_RESET_CONFIRM_RETYPE':True, 'SET_USERNAME_RETYPE': True, 'SET_PASSWORD_RETYPE': True, 'PASSWORD_RESET_CONFIRM_URL': 'password/reset/confirm/{uid}/{token}', 'USERNAME_RESET_CONFIRM_URL': 'email/reset/confirm/{uid}/{token}', 'ACTIVATION_URL': 'activate/{uid}/{token}', 'SEND_ACTIVATION_EMAIL':True, 'SERIALIZERS':{ 'user_create': 'app1.serializers.UserCreateSerializer', 'user': 'app1.serializers.UserCreateSerializer', 'user_delete': 'djoser.serializers.UserDeleteSerializer', } } Here it works fine: You're receiving this email because you requested a password reset for your user account at localhost:8000. Please go to the following page and choose a new password: http://localhost:8000/password/reset/confirm/MTI/bep1bq-486f1f58d396e13f7bdf862a12ba0a18 Your username, in case you've forgotten: m**********0@gmail.com The localhost:8000 team I want to: when I click to this link, it should navigate to the page that I customized to set a new password, but It redirects to page not found Page not found (404) Request Method: GET Request URL: http://localhost:8000/password/reset/confirm/MTI/bep1bq-486f1f58d396e13f7bdf862a12ba0a18 Please anyone knows how to solve this issue))? -
How to get the date by day name in python?
I have a day name such as Monday. and the current day is Wednesday. Now i want to get the date of next coming Monday. How can i do it with a best practice in a very short way. Right now i have done this with bunch of if conditions. and that thing made the code messy. and it only work when the day name is same as current day name. here is my code : day_time = timezone.now() current_day = day_time.strftime("%A") coming_day = "monday" if coming_day == current_day: day_to_set = day_time + timedelta(days=7) -
How to add a hyperlink to a list of objects in Django Rest Framework?
I have this code: class EntrySerializer(serializers.HyperlinkedModelSerializer): comments = serializers.HyperlinkedRelatedField(many=True, view_name='comment-detail', read_only=True) owner = serializers.HiddenField(default=serializers.CurrentUserDefault()) class Meta: model = Entry fields = ['url', 'owner', 'title','comments'] which produces this: HTTP 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Type: application/json Vary: Accept [ ... { "url": "http://127.0.0.1:7000/api/posts/34476dbd-89fc-420b-8d9e-28f2feb7c193/", "title": "A Title For This Entry", "comments": [ "http://127.0.0.1:7000/api/comments/dd1a0f06-c096-494f-a70e-5507f99082e6/" ] }, ... ] What I want to achieve is: the "comments" field to a hyperlink that leads to a Comment List. I tried using: comments = serializers.HyperlinkedRelatedField(many=True, view_name='comment-detail', hoping to get something like this: "comments": "http://127.0.0.1:7000/api/comments/" but that produces the following error: ImproperlyConfigured at /api/posts/ Could not resolve URL for hyperlinked relationship using view name "comment-list". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field. Request Method: GET Request URL: http://127.0.0.1:7000/api/posts/ Django Version: 4.1.3 Exception Type: ImproperlyConfigured Exception Value: Could not resolve URL for hyperlinked relationship using view name "comment-list". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field. Exception Location: /Users/banana/.virtualenvs/mvp/lib/python3.10/site-packages/rest_framework/relations.py, line 416, in to_representation Raised during: api.viewsets.EntryViewSet Python Executable: /Users/banana/.virtualenvs/mvp/bin/python Python Version: 3.10.2 Python Path: ['/Users/banana/Library/Mobile ' 'Documents/com~apple~CloudDocs/repos_x/projects/Duck_take/Backend/django/dev/entries_comments_requirements', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10', … -
Using multiple backend frameworks with one database in a virtual machine
I'm wondering if I could use multiple backend frameworks, using only 1 database in a VM instance in production without any problems. For example, I wanna keep Django Admin and its authentication rest API services, while using FastAPI for other things, possibly even ExpressJs in the future. I want to achieve this because different frameworks have different strengths and available third-party libraries in their ecosystems. I know NGINX as a reverse proxy and a load balancer will control the traffic load and directions to different endpoints. Still, would there be any conflicts in the database or anything I should be watching out for? Thank you! -
How to add custom view to custom AdminSite in Django
im a beginner with Django and basically want to add a section in Django Admin where if I click on it, I have a view that can process data. For example: I have models Campaign and Components which I can access through Django Admin and now I also want an Option "Analytics" that work the same way as if it was a model. Maybe this image will make clear what I want: When I click on "Analytics" I want to call a view that can fill a html template with data that I calculate from my models e.g. build a table that counts my instances of Campaigns and Components. What I've done so far: create a custom AdminSite to overwrite the default AdminSite. Problem: I do not see my option appearing. #admin.py class MyAdminSite(admin.AdminSite): def get_urls(self): urls = super(MyAdminSite, self).get_urls() custom_urls = [ path(r'myapi/test/', self.admin_view(testview)), ] return urls + custom_urls site_header = "My custom site" admin_site = MyAdminSite(name="myadmin") #apps.py class MyAdminConfig(AdminConfig): default_site = "myapi.admin.MyAdminSite" #settings.py INSTALLED_APPS = [ 'rest_framework', #'django.contrib.admin', "myapi.apps.MyAdminConfig", #views.py @api_view(["GET", "POST"]) def testview(request): print("testmyview") return render(1, "mytest.html") #urls.py urlpatterns = [ path('admin/', admin_site.urls), path('api/', include('myapi.urls')), ] I tried to follow this tutorial: https://adriennedomingus.medium.com/adding-custom-views-or-templates-to-django-admin-740640cc6d42 but I think after … -
Create a GENERATED ALWAYS AS IDENTITY identifier field in a PostgreSQL 14+ database table using Django 4.1.x
I would like Django 4.1.x (I'm using 4.1.3 at the time of writing) to create a PostgreSQL (v14+) table for which the identifier, id, has to be created as if I were creating the table with this SQL snippet for the id field: id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY. This would normally ends up on a schema like this in PG 15: $ pg_dump --table table --schema-only -d postgres -- multiple SET statements here (...) CREATE TABLE public.table ( id bigint NOT NULL, name character varying(1024) NOT NULL ); ALTER TABLE public.table OWNER TO postgres; ALTER TABLE public.table ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY ( SEQUENCE NAME public.table_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1 ); ALTER TABLE ONLY public.table ADD CONSTRAINT table_pkey PRIMARY KEY (id); Here's my current model in Django: class Foo(models.Model): name = models.CharField( max_length=256, verbose_name=_("Name"), null=False, ) def __str__(self): return f"{self.name}" It actually creates this table (makemigrations/migrate) with the following ALTER statement instead: ALTER TABLE public.table ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY ( SEQUENCE NAME public.table_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1 ); How can I achieve that, … -
Django - How to randomly pick objects based on input from form?
I'm trying to build a basic workout planner, where you have to input in a form some info, and based on the info a personalized planner will be generated. I created a model where I inputted all the exercises, and which movement they are focusing on. If the user choose to exercise 2x week, 2 days will be generated with 6 specific movements each day. Each movement has a number of exercises, but only one exercise will be randomly picked and showed on the planner. So you basically have 6 different exercises each day. If the user pick 3x week there will be 3 days with 6 other exercises, and so on. I tried to find some way to randomly pick and generate those exercises, but I can't. I can't just use random, because I have the condition where each frequency picked on the form, has to follow a specific movement schedule. So I decided to hard code it, and write each condition, line by line. But that will lead to many lines of code. And that is not the only issue. By doing so, every time I reload the page, the exercises name change. It keeps randomly picking new … -
post_merge bash script to install requirements and migrate django
I use PythonAnywhere to deploy my Django app and made automatic deployments with GitHooks. Now I want to create a script in post_merge file to install requirements and migrate the db changes so it will be fully automated. I've tried something like this for installing requirements but it isn't working: #!/bin/sh source /home/user/.virtualenvs/venv/bin/activate #### activate venv cd /home/user/app #### go to django app folder pip install -r requirements.txt #### install requirements touch /var/www/user_pythonanywhere_com_wsgi.py #### restart webapp I don't know why it's not working or what's the right way to do it, so I really appreciate your answers. -
Django: media files doesn't load up uploaded by user in production environment
I'm hosting my site in railway. Everything is set up and works fine but the images uploaded by user don't load up. settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ] MEDIA_URL ='/media/' MEDIA_ROOT = os.path. join(BASE_DIR,'media') models.py class Post(models.Model): img = models.ImageField(upload_to="pics") blog.html {% extends 'base.html' %} {% load static %} {% static "images/projects" as baseUrl %} {% for post in post_list %} <div class="image_wrapper"><a href="{% url 'post_detail' post.slug %}" target="_parent"><img src="{{ post.img.url }}" alt="image 1"/></a></div> {% endfor %} urls.py urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root= settings.MEDIA_ROOT) if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) Error I'm getting: Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg -
Problem running single django files from VS Code
I have a big Django project that I work in, and I have a problem when I try to run a single .py file from the terminal (I have the correct env set in my VSCode), and it usually breaks when trying to import a module (another django app). When I try to run the same file from PyCharm I have no issues and runs perfectly (using the same env) Haven't tried much, I have no idea where to start to try and tackle this issue, all I find in google is people using the wrong env, which is not the case here. -
How can i push object value into array - javascript
This may seem like an easy question, but im stuck. Currently, I am passing an object over to my function, which in return is supposed to push an object's value to an array. As of now its not pushing INTO the array and it coming back as a count 0 and undefined if I try to get at the actual value explicitly. In my check file, i have a function call with a forloop that sends the object and the count for (let i=0; i< data.length; i++){ addtoList(i, data); } }); the forloop is looping through data returned from a Django object as such data = serialize("json", modelname.filter(username=...)) return HttpResponse(data) In my addto file, the actual function should handle adding the object to the list. var pushere = [] function addToList(i,data){ pushere.push(data[i].fields.title) console.log(data[i].fields.title) }; console.log(pushere) now i also logged a regular list to see the differences const testListt = ['a','b','v'] console.log(testListt) with that being said, how am i supposed to properly 'push' object values to an array, which is what i am needing before displaying it to the page. I've tried mapping objects with help from Mozilla Map and their help on Objects. When a user first logs in, it … -
add permissions to user on the serializer
i'm trying to add/update permissions for users by id on the serializer, but nothing changed (no add no update) and i dont get any errors so i can't know where is the problemes, i have tried those method and nothing works. thank u all. serializer (update): class UpdateSerializer(serializers.ModelSerializer): """Handle serialization and deserialization of User objects.""" # user_permissions = PermissionSerializer(many=True, write_only=True) # user_permissions = serializers.SlugRelatedField( # many=True, read_only=True, slug_field="id" # ) def get_user_permissions(self, user): return list(user.user_permissions.all().values_list("id", flat=True)) class Meta: model = User fields = [ "email", "password", "tel", "first_name", "last_name", "registration_id", "avatar", "is_deleted", "user_permissions", ] extra_kwargs = { "is_deleted": {"write_only": True}, "password": {"write_only": True}, } # override the update method to save the data def update(self, instance, validated_data): """Perform an update on a User.""" password = validated_data.pop("password", None) # permissions_list = self.get_user_permissions() # permissions = Permission.objects.filter(id__in=permissions_list) permissions = self.request.data["user_permissions"] # permissions = Permission.objects.filter(id__in=Permission.objects.all()).values instance.user_permissions.set(permissions) for (key, value) in validated_data.items(): setattr(instance, key, value) if password is not None: instance.set_password(password) instance.save() return instance user view: class UpdateUserView(generics.RetrieveUpdateAPIView): permission_classes = [IsAuthenticated] serializer_class = UpdateSerializer queryset = User.objects.all() def update(self, request, *args, **kwargs): instance = self.get_object() serializer = self.serializer_class(instance, data=request.data, partial=True) if serializer.is_valid(raise_exception=True): self.perform_update(serializer) return Response(serializer.data) if getattr(instance, "_prefetched_objects_cache", None): # If 'prefetch_related' has been … -
Why does the NoReverseMatch error pop up when trying to paginate my django website?
I have a list of data from my models that I would like to paginate as it looks flooded on one singular page and it generally takes a longer time for the page to load. However, when I tried to use a paginating method, it doesn't seem to work in my code. What I've already done for my code is: .../clubs/views.py class ClubListView(generic.ListView): model = Club paginate_by = 6 .../clubs/urls.py from django.urls import path from . import views app_name = "clubs" urlpatterns = [ path('', views.ClubListView.as_view(), name="club-list"), path('<int:pk>/', views.ClubDetailView.as_view(), name="club-detail"), ] .../clubs/club_list.html {% block group_content %} <hr> {% comment %} Complete making group display. {% endcomment %} <p></p> <div class="col-md-8"> <div class="container"> {% for club in club_list %} <a class="list-group-item" href="{{ club.get_absolute_url }}"> <h3 class="title list-group-item-heading">{{ club.name }}</h3> <span>{{ club.slogan|safe }}</span> </a> <p></p> <br> {% endfor %} </div> </div> {% endblock %} {% block pagination %} {% if page_obj.has_previous %} <a href="{% url 'club-list' page_obj.previous_page_number %}"> Previous Page </a> {% endif%} {% if page_obj.has_next %} <a href="{% url 'club-list' page_obj.next_page_number %}"> Next Page </a> {% endif%} {% endblock %} However, it still gives me the error of NoReverseMatch of 'club-list' in my html page even though it is given from … -
How to configure media root file for multitenant app in django
I am creating multitenant app in django. I have chosen "Isolated database with a shared app server" from this website - https://books.agiliq.com/projects/django-multi-tenant/en/latest/isolated-database.html. I am using django-storages with AWS S3 bucket. The problem I could not figure out how to configure MediaRoot file for different database in django settings file. Is there any option in the settings file to use mediaroot with if condition. For example, if I am using db_1 use this mediaroot_1 elseif using db_2 , then use mediaroot_2. This is basically to charge the client as per the aws bucket usage. settings.py DATABASE_ROUTERS = ["core.router.TenantRouter"] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', }, 'db_1': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db_1.sqlite3', }, 'db_2': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db_2.sqlite3', }, } #### the below I need if statement for different database #### I want to use bucket_1 for db_1 and so on AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME") AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com' I tried to bring get_current_db_name() from the middleware.py into settings.py but I could not do it. I have tried django-tenant-schemas and other similar packages but I would like to go without 3rd party packages. ######## middleware.py import threading from .utils import tenant_db_from_request THREAD_LOCAL = threading.local() … -
How can I add Datatables functions(flexible search, flexible order by, pagination) to Django GraphQL Union?
I have multiple problems but they are related. I have inherited models, I use Union for them because I want to return all sub models. DjangoFilterConnectionField only accept DjangoObjectType I can't use it that's why. I was going to use it by all datatables columns. I thought override functions but I dont know where should I start. Do you have any idea about it? -
How to return a group by query via Django REST API?
I have a Django REST API which works perfectly when I want to query some data from the database. Here I have an example: views.py class ProductListAPIView(generics.ListAPIView): def get_queryset(self): # Collect data from products table and filter it queryset = Product.objects.filter(name="Ferrari", date__lte="2022-08-01") # <- Substitute with line below return queryset serializer_class = ProductSerializer authentication_classes = [authentication.SessionAuthentication, authentication.TokenAuthentication] permission_classes = [IsOwnerPermission] serializers.py from rest_framework import serializers from insert_data.models import Product class ProductSerializer(serializers.ModelSerializer): class Meta: model = Product fields = ["name","category","date","price"] The output is what I expect i.e. a json response with the content of the database. The problems arise when I try to group the queryset in order to return the average price of a product. By doing some research in the internet I noticed that I just need to substitute the line of code in the views.py script with this: Product.objects.filter(name=params['name'], date__lte=query.date).values('name','category').annotate(price_average=Avg('price')).order_by() I am pretty confident that this does the work, however the API returns an error which I do not know how to fix: AttributeError: 'int' object has no attribute 'pk' I totally have no idea what this refers to. Would you be able to suggest a smart and elegant way to return the average price of a product … -
how to fix the text so that its code is not written?
I created a blog site. But when I create some style for the text in the blog, it is then displayed on the main page. for example, I create bold text when creating a blog example picture and the main page displays the code ex pic2 how to fix this? I created this form using Django code: def add_blog(request): form = AddBlogForm() if request.method == "POST": form = AddBlogForm(request.POST, request.FILES) if form.is_valid(): user = get_object_or_404(User, pk=request.user.pk) category = get_object_or_404(Category, pk=request.POST['category']) blog = form.save(commit=False) blog.user = user blog.category = category blog.save() messages.success(request, "Blog added successfully") return redirect('blog_details', slug=blog.slug) else: print(form.errors) context = { "form": form } return render(request, 'add_blog.html', context) -
Django large CSV export with celery (async)
I am trying to export a CSV file which takes a while to generate. I have written the export CSV view which is successfully working, however I needed to adapt it to use celery for it to work in production with large data volumes. I have written the following code (mostly following https://thoslin.github.io/async-download-with-celery/): View: from tasks import export_report @staff_member_required() def export_status_report(request): task = export_report.delay() return render(request, "admin/poll_for_download.html", {"task_id": task.task_id }) # Check if task is finished def poll_for_download(request): task_id = request.GET.get("task_id") filename = request.GET.get("filename") if request.is_ajax(): result = AsyncResult(task_id) if result.ready(): return HttpResponse(json.dumps({"filename": result.get()})) return HttpResponse(json.dumps({"filename": None})) try: f = open("/path/to/export/"+filename) except: return HttpResponseForbidden() else: response = HttpResponse(file, mimetype='text/csv') response['Content-Disposition'] = 'attachment; filename=%s' % filename return response Task: @app.task def export_report(): date = datetime.now() date = date.strftime("%m/%d/%Y") filename = "reoport"+date+".csv" response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename= "{}"'.format(filename) payouts = Payout.objects.all() writer = csv.writer(response) #instantiate writer # write headers writer.writerow(['Field1', 'Field2', 'Field3']) for payout in payouts: list = [payout.field1, payout.field2, payout.field3] writer.writerow(list) return filename HTML <script> $(function(){ $.ajaxSetup({ cache: false, timeout: 360000 }); var url = "admin/poll-for-download/"; var i = 0; (function worker() { $.getJSON(url+"?task_id=", function(data){ if(data.filename) { var file_url = url+"?filename="+data.filename; $("#content").html("If your download doesn't start automatically, please click … -
Responses is returned as string and not as json - Python
I am trying to send back (get requests) a json but instead is sending a json as string "{"series": [{"name": "Count of Signups", "type": "column", "data": [4, 29, 10]}, {"name": "Average of signups over last 3 Months", "type": "line", "data": [NaN, NaN, 14.0]}], "labels": ["2020-06", "2020-07", "2020-08"]}" The disire result its the same but as json without the quotes (not as string) { "series": [{ 'name': "Count of Signups", 'type': "column", 'data': [4, 29, 10], }, { 'name': "Average of signups over last 3 Months", 'type': "line", 'data': [NaN, NaN, 14.0], }], "labels": ["2020-06", "2020-07", "2020-08"] } here is my code, my view in django: def Apiname(request): start_date = request.GET.get('start') end_date = request.GET.get('end') res = { "series": [{ 'name': "Count of Signups", 'type': "column", 'data': [], }, { 'name': "Average of signups over last 3 Months", 'type': "line", 'data': [], }], "labels": [] } df = pd.read_csv("mycsvfile.csv") mask = (df['MonthYear'] >= start_date) & (df['MonthYear'] <= end_date) df = df.loc[mask] for i, row in df.iterrows(): res['series'][0]['data'].append(row['Count_of_signups']) res['series'][1]['data'].append(row['signups_3mth_moving_average']) res['labels'].append(row['MonthYear']) return HttpResponse(json.dumps(res), content_type="application/json") I tried with to_dict to parse it to json but got the same results, the most strage is that i have another APIs with the same structure and they return …