Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Celery Beat tasks limit to number of cores,, but opening terminals I can run the same task more times
I have about 5 python web scrapers. I can open 5 terminals and run the scraper 5 times simultaneously. I have implemented Django Celery Beat, where after a single iteration the scraper waits for about 2 min and restart. Following is my problem, in the command prompt I can run 5 sessions, but via celery beat, we can only run 2 scripts at a time, because my ubuntu has only 2 cores. This to me, a waste of resources. We love Celery, but we can open 5 terminals and a cron call can run the 5 different scrapers concurrently. We are basically using celery as a periodic cron job. Are we doing anything wrong? Do we have to ditch Celery? Can we deploy multiple workers? The server has enough resources to run more than 2 python scripts. Can you help? Thank you very much -
Django Admin: Limit size of read-only field in tabular inline
It seems that Django happens to just use a Tag for read-only text fields inside forms (at least tabular inlines in admin forms). What I want is, to display an URL inside a tabular inline (just using one line if possible). Unfortunately that URL tends to be rather long. So the best would be, when the URL would be inside some text field, where I can scroll or just select the whole content with CNTRL-A to get the link. But the field should be read-only. When I do that in the admin, it changes the whole output to and thus the URL will be displayed in full length and taking over the whole width of the screen -- and compressing other fields -- what is rather ugly. Any solution? -
Django Python - Creating a landing page with a prepopulated form
I'm working on a project for CS50's web course where users can visit a Wikepedia mock up and edit entries. Right now I'm having a hard time getting my entry's editing page to load properly. Right now I can't seem to get the 'entry' title from the Edit Button on the entry page in order for the page to load with the entry title/content. THe current implementation throws me this error "Reverse for 'edit_landing' with arguments '('title',)' not found. 1 pattern(s) tried: ['edit_landing$']" I've tried swapping out "title" for "entry" and even declaring "entry = entry" OR "entry = title" but nothing seems to work. I'm a little confused as to how django passing variable in a url still. I've tried about 4 or 5 implementations and I'm super lost on how to do it. Right now separating the edit landing and edit functionality seems to work. Any help would be appreciated. Thanks so much! Here is my code so far: views.py: def edit_landing_page(request, entry): edit = EditEntryForm({'content':(util.get_entry(entry))}) return render(request, "encyclopedia/edit.html", { {"edit":edit} }) def edit(request, entry): if request.method == "POST": util.save_entry(entry) return redirect('wiki'+entry) else: content = util.get_entry(entry) return render(request, "encyclopedia/edit.html", { "title": entry, }) urls.py: urlpatterns = [ path("", … -
Start Django development server on AWS EC2 Amazon Linux
This tutorial here shows how to start the development server on AWS EC2: ALLOWED_HOSTS=['EC2_DNS_NAME'] ... python manage.py runserver 0.0.0.0:8000 In a browser, navigate to public DNS of the instance, make sure to append port “8000” at the end and if everything was properly configured, it will show the index page of the application. For example: www.ec2dnsname.com:8000 My public IPv4 DNS is of this form: ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com So, in my settings.py ALLOWED_HOSTS=['ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com'] And run this in the EC2 terminal: python manage.py runserver 0.0.0.0:8000 However, when I try to reach this address: www.ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com:8000 nothing is showing up (The page took too long to respond). Is this the complete and correct way to just try run a development server on AWS EC2? -
Build DevOps to deploy ReactJS - Django app
I am currently developing a web app where the front-end is done in React and the back-end in Django (using Django REST API to communicate the front with the back) and now is the time to deploy the first version :) So the situation is the following: The code is in BitBucket. I want to deploy it in Google Cloud Platform. I would like to create a Jenkins pipeline for CI/CD. How should I do this? Does anyone have any recommendations or guidelines? Many thanks! -
Django read excel (xlsx) file to display in a table
I have a simple excel file I am trying to figure out how I can get my app to read the excel file, so I can use the data to display in a template. I have looked into xlrd but does anyone have any suggestions on the best way to do this? -
Attaching VS Code to a dockerized django app and running the debugger, where are the docker logs?
I have a dockerized django app which I work on with VS Code. I start the container, then attach to it using VS Code's remote attach. Then I start the debugger on port 7000 with the following launch file. I run the code that crashes things, but its a silent failure in the Terminal / Output / Debug Console. I inspect the docker logs but I only see output from the program running on port 8000. How do I see the stack trace (or docker logs) for the code running on port 7000? -
How do I use Apache Guacamole with Django for a Web Application? (Without using Default Client)
I'm currently working on a project which involves using Apache Guacamole to make remote connections to other devices in a network. I am using Django and its REST Framework purely to serve as the backend for my web application (Front-end hosted elsewhere). I do not want to use the default Guacamole client and would like to just implement its remote connection capability into my own web application. My question is, is there a standard way to achieve this using Django? If not, how would I go about integrating remote desktop access using Apache Guacamole with Django to allow users of my web application to remotely access machines? P.S. I am familiar with how to use the prepackaged Guacamole Client supplied. I have also looked into solutions to my issues such as: https://github.com/jimmy201602/django-guacamole/tree/master/guacamole (Fork because original repository has been deleted). However, I am facing an issue with this code where the remote desktop connection is successfully established (user on computer kicked out), there is no display of the remote desktop shown (just a white screen). -
How to automatically get Model Field type, its properties and size from other apps?
I am developing API in DRF. Django : 3.2.3 djangorestframework : 3.12.4 In my master_details App I've created a model with different fields. Some are mentioned bellow class MasterModel(models.Model): name = models.CharField(max_length=100, null=False, blank=True) address = models.TextField(null=False, blank=True) data = models.DecimalField(max_digits=5, decimal_places=2) ... Similarly I have a report App which provide Get API endpoint to reporting tools. This report model shall contain certain fields that are same as/imported from MasterModel as shown bellow class ReportModel(models.Model): name = models.CharField(max_length=100, null=False, blank=True) address = models.TextField(null=False, blank=True) data = models.DecimalField(max_digits=5, decimal_places=2) ... or class ReportModel(models.Model): name = models.ForeignKey(MasterModel, on_delete=models.CASCADE) address = models.ForeignKey(MasterModel, on_delete=models.CASCADE) data = models.ForeignKey(MasterModel, on_delete=models.CASCADE) ... I am unable to use Foreign Key, in case data in MasterModel changes it will propagate to ReportModel. This should be avoided as historical reports must not be affected and the integrity of historical records must be maintained. I have manually duplicated the fields in the ReportModel and I manually maintain their congruence. I wish to get around this. I wish to know is their any automatic way whereby I can create Model Field type and apply its properties and size from other apps? -
How do I create a prepopulated model field in django to view in admin panel?
I have a model in models.py: class Package(models.Model): diagnosis=models.ForeignKey(Diagnosis, on_delete=CASCADE) treatment=ForeignKey(Treatment, on_delete=CASCADE) patient_type=ForeignKey(PatientType, on_delete=CASCADE) max_fractions=models.IntegerField(default=max_fractions) total_package=models.FloatField(max_length=10, default=total_package) rt_number=ForeignKey(Patient, on_delete=CASCADE) I want the fields, max_fractions and total_package, to be prepopulated with the values returned by their respective default parameters. The functions: def max_fractions(): if Treatment.treatment=='3DCRT' and PatientType.patient_type=='MJPJAY': return 30 if Treatment.treatment=='3DCRT' and PatientType.patient_type=='PMJAY': return 30 def total_package(): if Treatment.treatment=='3DCRT' and PatientType.patient_type=='MJPJAY': return float(75000) if Treatment.treatment=='3DCRT' and PatientType.patient_type=='PMJAY': return float(70000) But I get: What am I missing? -
when I instaling django with pipenv which approch is better?
when installing pakages and other things in project like django declaring version is good or bad? pipenv install django or pipenv install django==3.1.0 -
Raise AttributeError(f"Use item[{name!r}] = {value!r} to set field value") in Django
we are following a tutorial for connecting Scrapy with Django. This is our model.py file: class Siteinformation(models.Model): unique_id = models.CharField(max_length=100, null=True) data = models.CharField(max_length=1000, null=True) timestamp = models.DateTimeField(default=timezone.now) @property def to_dict(self): data = { 'data': json.loads(self.data), 'timestamp': self.timestamp } return data def __str__(self): return self.unique_id This is the pipelines.py file: class ShopspiderappPipeline(object): def __init__(self, unique_id, *args, **kwargs): self.unique_id = unique_id self.items = [] @classmethod def from_crawler(cls, crawler): return cls( unique_id=crawler.settings.get('unique_id'), ) def close_spider(self, spider): item = Siteinformation() item.unique_id = self.unique_id item.data = json.dumps(self.items) item.save() def process_item(self, item, spider): self.items.append(item['url']) return item This is the spider.py file: class ShopSpider(CrawlSpider): name = 'shop' def __init__(self, *args, **kwargs): self.url = kwargs.get('url') self.domain = kwargs.get('domain') self.start_urls = [self.url] self.allowed_domains = [self.domain] ShopSpider.rules = [ Rule(LinkExtractor(unique=True), callback='parse_item'), ] super(ShopSpider, self).__init__(*args, **kwargs) def parse_item(self, response): item = Siteinformation() item = {} item['url'] = response.url yield item Unfortunately it produces this error: 2021-06-29 17:17:45 [scrapy.core.engine] ERROR: Scraper close failure Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/twisted/internet/defer.py", line 662, in _runCallbacks current.result = callback(current.result, *args, **kw) File "/Users/.../.../..../shopspiderapp/shopspiderapp/pipelines.py", line 31, in close_spider item.unique_id = self.unique_id File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scrapy/item.py", line 112, in __setattr__ raise AttributeError(f"Use item[{name!r}] = {value!r} to set field value") AttributeError: Use item['unique_id'] = '0a55d509-bd36-49ba-b3c8-313fbadcd3de' to set field … -
Best way to handle Django Migrations with Git?
Maybe this has been asked, but, at least from the ones i've readed, i'm not convinced. The idea is that i'm on a project with multiple people working on the backend, and because people are assigned to solve different issues, each one make their own changes on the models, and that implies each one can have their own migrations, which might clash either by just having the same numbered migration as to make changes to the same tables. As a way to work with this, we devised a solution in which we create 3 branches: Master, QA and Develop, the first one is the one at production, Develop is the one where changes to test on our development server will be merged to test, and QA as an intermediate between the 2, the idea is that we will be working the changes on new branches(feature/xxxxxx) with their respective migrations, and when we need to upload to the developmente server, we will merge to develop, which will handle migration conflicts, and then, when the change is approved, we will delete the migrations of the new branch(feature/xxxxxx), merge it to QA, and delete that branch, and once QA is ready to be … -
Django show all objects and foreach object all compatible objects
I am new to Django and I want to create a compatibility model for mobile phone. I create a model with all phone details like name, screen size etc. and I thought of creating a new model for compatibility. The first model in Django class MobilePhone(models.Model): name = models.CharField(max_length=500) manufacturer = models.ForeignKey(MobileManufacturer, on_delete=models.PROTECT) dimensions = models.TextField(max_length=500, null=True, blank=True) display_type = models.CharField(max_length=500, null=True, blank=True) display_size = models.CharField(max_length=500, null=True, blank=True) and another model for compatibility class Compatibility(models.Model): comp_id = models.CharField(max_length=30) comp_type = models.CharField(max_length=30) mobile_id = models.ForeignKey('MobilePhone', on_delete=models.CASCADE) I fill the MobilePhone table with values id name manufacturer dimensions 1 Phone 1 Samsung 8x15 2 Phone 2 Samsung 8x15 3 Phone 3 Samsung 8x15 4 Phone 4 Samsung 8x15 and the Compatibility table with id comp_id comp_type mobile_id 1 100 screen 1 2 100 screen 2 3 101 screen 3 4 100 screen 4 the comp_id is the unique number I use to group the phones now with all_entries = MobilePhone.objects.all() get all mobile phones like - Phone 1 - Phone 2 - Phone 3 - Phone 4 what I want is - Phone 1 -- (same with Phone 2, Phone 4) - Phone 2 -- (same with Phone 1, Phone 4) - … -
Create dynamic amount of form-fields in HTML and extract them from request (Django)
I have a view which does the following: Scrape som prices from a product-page. Return a HTML page with a picture of each product incl. the price (like a check-out cart from a webshop) If the price is incorrect, the user should then be able to modify the price for each product, and atlast click one button, which then saves the products. Right now I have my view #views.py def my_products(request): user = request.user if request.method == "POST": form = MyForm(request.POST) if form.is_valid(): domain = form.cleaned_data["domain"] discount_pct = form.cleaned_data["discount_pct"] link = form.cleaned_data["link"] #Extract info for each link prices,links,images,errors = scrape_pagee(domain,link) context = { "data":zip(prices,links,images)} return render(request, "myapp/list_confirm.html",context=context) else: add_form = MyForm() context = { "add_form":add_form, } return render(request, "myapp/add_list.html",context=context) where prices, links are array of the given prices and the link to each product e.g prices = [600,200,159], links = ["www.webshop1.com/shirt","www.webshop2.com/jeans","www.webshop1.com/socks"] I think parsing the forms isn't a problem (I assume it is just parsing an array of Forms to the context and loop over them in the HTML page), but it's more how to submit them all in one "save" button, and how to distinguish them from eachother in the POST-request. -
I'm Fetching Audio File Via Ajax from Server for Search Function But Not Audio Playing After Render Player
Below is My Django CMS Code for Fetching Music Ringtone from Server [Database], Code Fetch Music Ringtones from Server Successfully But After Render Player With Ringtone Not Playing Audio. "Backend Code " def search_ringtone(request): data = {} for key, _ in request.GET.items(): data = json.loads(key) query = data.get("query", None) if query: ringtone_objects = Ringtone.objects.filter(name__icontains=query) ringtones = [] for item in ringtone_objects: ringtone_file = item.android_ringtone_file if item.android_ringtone_file else item.iphone_ringtone_file ringtones.append( { "category_public_url": item.category.page.get_public_url(), "category_name": item.category.name, "ringtone_url": item.page.get_public_url(), "ringtone_media_url": ringtone_file.url, "ringtone_type": "audio/mpeg" if item.android_ringtone_file else "audio/audio/x-m4r", "ringtone_name": item.name, "ringtone_download_count": item.download_count, } ) return JsonResponse({"result": True, "ringtone": ringtones, "query":query}) return JsonResponse({"result": False}) Player Function $(".ringtone-player .play-btn .fa-play-circle").on('click', function(current) { $(this).parent().find(".fa-play-circle").css("display", "none"); $(this).parent().find(".fa-snowflake-o").css("display", "inline-block"); $(".fa-play-circle").not(this).parent().find(".fa-snowflake-o").css("display", "none"); $(".fa-play-circle").not(this).parent().find(".fa-play-circle").css("display", "inline-block"); // ADD / REMOVE CLASS $(this).parent().parent().addClass("isPlaying"); $(".fa-play-circle").not(this).parent().parent().removeClass("isPlaying"); // ani $(this).parent().parent().find(".beat_animation ul li").css("animation-play-state", "running").css("opacity", "1"); $(".fa-play-circle").not(this).parent().parent().find(".beat_animation ul li").css("animation-play-state", "paused").css("opacity", ".1"); // PASUE CURRENT AUDIO TRACK WHEN PLAY NEXT/PREV AUDIO TRACK $("audio").each(function(e) { if (e !== current.currentTarget) { $(this)[0].pause(); } }); // PLAY CURRENT AUDIO TRACK $(this).parent().parent().find(".track audio")[0].play(); }); // PAUSE FUNCTION $(".ringtone-player .play-btn .fa-snowflake-o").on('click', function() { // HIDE PASE ICON $(this).parent().find(".fa-snowflake-o").css("display", "none"); // Show Play Icon $(this).parent().find(".fa-play-circle").css("display", "inline-block"); // PAUSE AUDIO TRACK $(this).parent().parent().find(".track audio")[0].pause(); }); $(audio).on('ended', function() { // HIDE PASE ICON $(".ringtone-player .play-btn .fa-snowflake-o").css("display", "none"); // … -
Python get time at specify GMT
I am trying to get the current time at a specific GMT ( like +3 -1 etc.). I have a script that runs on a remote server, and he needs to update the current time at another country which I can not find at the time_zone list. I tried import pytz pytz.all_timezones And look for the country and find it in the list; I know the county GMT is +3. import datetime from django.utils.timezone import now both now function is relevant to me, and I can not find how I find the now function with GMT +3 -
How to add ArrayField in Django?
my models.py class LiveClass_details(models.Model): standard = models.ForeignKey(LiveClass, on_delete=models.CASCADE) chapter_details = models.TextField(default='') mentor_id = models.ForeignKey(Mentor, max_length=30, on_delete=models.CASCADE) start_time = models.DateTimeField() end_time = models.DateTimeField() doubtClass = models.OneToOneField(DoubtClasses, on_delete=models.PROTECT, null=True, blank=True) isDraft = models.BooleanField(default=True) ratings = models.FloatField(default=0) no_of_students_registered = models.IntegerField(default=0) # registered_students = models.ManyToManyField(RegisteredNames, null=True, blank=True) no_of_students_attended = models.IntegerField(default=0) class Meta: verbose_name_plural = 'LiveClass_details' class RegisteredNames(models.Model): name = models.CharField(max_length=100, unique=True) liveclass_id = models.ForeignKey I am creating a endpoint where when a user register himself his name will get added to registered_students , so i had made a registered students ManyToMany Field hoping it will get updated when a user is registered but then i understand that it will contain all the names that are present in the RegisteredNames Model meaning names registered across all the liveclasses but i want only the names that are registered for a particular liveclass in the field so i need a array like field which i think is not possible so please help me in improving my logic, how can i achieve it -
Django reset password and verification with email using function based view
Django reset password and verification with email using function based view. -
App is Not showing up in Django 3.0 admin
I have put my app in the settings.py: 'jet', 'jet.dashboard', 'store.apps.StoreConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'tinymce', ] no error is there. I suspect the admin.py of my app: from django.contrib import admin from .models import Customer, Product, Order, OrderItem, Shipping, models from tinymce.widgets import TinyMCE class ProductAdmin(admin.ModelAdmin): formfield_overrides = { models.TextField: {"widget": TinyMCE()} } admin.register(Customer) admin.register(Product, ProductAdmin) admin.register(Order) admin.register(OrderItem) admin.register(Shipping) But the models are registered properly, and no error is there. I have use Django-3-jet module to customise my admin page. Help Will Be appreciated. App name: store. -
nginx 502 bad gateway, location of nginx.config
I am trying to upload my app to AWS (Python 3.8 running on 64bit Amazon Linux 2) through Elastic Beanstalk. My app has been developed with Django 3.0. OS is Ubuntu 20.04. I start with an empty Django project, this works fine with a green status health check, and Django admin works fine. When I add files from my app it throws the 502 bad gateway error. I have spent many days trying to fix this with no luck. var/log/nginx/error/log: 2021/06/29 15:29:42 [error] 4854#4854: *4084 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.20.250, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "35.176.238.136" django.config option_settings: aws:elasticbeanstalk:container:python: WSGIPath: FFfF_project.wsgi:application aws:elasticbeanstalk:environment:proxy:staticfiles: /static: static aws:elasticbeanstalk:application:environment: DJANGO_SETTINGS_MODULE: FFfF_project.settings PYTHONPATH: "/var/app/current:" 01_packages.config packages: yum: python3-devel: [] mariadb-devel: [] requirements.txt django==3.0 django-crispy-forms==1.9.2 Pillow==8.2.0 gunicorn==20.0.4 mysqlclient==1.4.6 config.yml branch-defaults: master: environment: FFfF-env group_suffix: null environment-defaults: FFfF-env: branch: null repository: null global: application_name: FFfF_project branch: null default_ec2_keyname: ff-KeyPair default_platform: Python 3.8 running on 64bit Amazon Linux 2 default_region: eu-west-2 include_git_submodules: true instance_profile: null platform_name: null platform_version: null profile: eb-cli repository: null sc: git workspace_type: Application I think the error is related to the nginx.config file, but I cannot find this file to either edit or modify. … -
Using D3.js to pick and choose data out of SQLite3 database
I am pretty new to coding and am trying to build a web app that is able to visualize Data out of a SQLite3 database. Is it possible to pick individual datapoints out of a 30k row database to visualize it as a stacked bar chart? I would like to build a search field where its possible to search for the datapoints. And idealy have an update field to update the visualization after picking the datapoints. Many Thanks in Advance -
Issue using progress_recorder (celery-progress): extends time of task
I want to use celery-progress to display progress bar when downloading csv files my task loop over list of cvs files, open each files, filtered data and produce a zip folder with csv filtered files (see code below) but depending where set_progress is called, task will take much more time if I count (and set_progress) for files processed, it is quite fast even for files with 100000 records but if I count for records in files, that would be more informative for user, it extends time by 20 I do not understand why how can I manage this issue for file in listOfFiles: # 1 - count for files processed i += 1 progress_recorder.set_progress(i,numberOfFilesToProcess, description='Export in progess...') records = [] with open(import_path + file, newline='', encoding="utf8") as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') csv_headings = ','.join(next(spamreader)) for row in spamreader: # 2 - count for records in each files processed (files with 100000 records) # i += 1 # progress_recorder.set_progress(i,100000, description='Export in progess...') site = [row[0][positions[0]:positions[1]]] filtered_site = filter(lambda x: filter_records(x,sites),site) for site in filtered_site: records.append(','.join(row)) -
Django Form Widgets SyntaxError: cannot assign to function call
I'm trying form widgets and I coded this class in my forms.py: class RawProductForm(forms.Form): title = forms.CharField(label='', widget=forms.TextInput(attrs={"placeholder: "Your title"})) description = forms.CharField( required= False, widget=forms.Textarea( attrs={ "class": "new-class-name two", "id": "my-id-for-textarea", "rows": 100, "cols": 20 } ) ) price = forms.DecimalField(initial=199.99) But I'm getting an error which I do not know how to solve: line 16 title = forms.CharField(label='', widget=forms.TextInput(attrs={"placeholder: "Your title"})) ^ SyntaxError: expression cannot contain assignment, perhaps you meant "=="? -
Is there a tool that syncs two parts of a code base so that updating one automatically updates the other?
I'm looking for a vim, vscode, or emacs tool/plugin that allows you two define a link two parts of the same codebase (same or different files), such that they stay synced up. If you update one of them, the other is updated to match it automatically. An example is with a Django project below. On the left (line 12) is a view called detail with a docstring that contains the url endpoint that calls that view. On the right (line 8) is the code that links that url to that function. The url endpoint is place/<int:place_id>/. If I update the url on the left or on the right to place/<int:place_id/asdf, the other one should update automatically. (This can occur live, or whenever the file is saved). Do you know of any tool that can do this live syncing of specific parts of code files?