Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django POST issue
I' ve been struggling with this for a while: I've create a Django App and have these models: Seasons Participants Objectives Partic_Obj Movements_Obj "Partic_Obj" is the table that join "Participants" and "Seasons". "Movements_Obj" is the table that i can't populate and should be containing the next data: When you add a participant, it need to create automatically one register in "Movements_Obj", for every objetive that corresponds to the season the participant is in. For example: A participant is included in Season 1, which has 5 objectives, then, when i add this participant to "Partic_Obj" i need the 5 registers to be created automatically in "Movements_Obj". Hope you can help me. Thanks! -
Huey ``db_task`` successfully registered by consumer but does not receive/execute any task
I have a Django project with huey as task queue. In my dev environment (see below), everything works as expected: [save] Django model instance saved [trigger] huey task triggerd [execute] huey task gets executed by running consumer (run_huey) But I could not get the third point [execute] working in my production environment. The only difference is DEBUG = False and the consumerrun_huey started by a systemd service unit (see below). The consumer does correctly recognize the db_task "schedule_virusscan", but does not pick up/receive any task triggered by my model save method (here: schedule_virusscan(self.id)) I use immediate=False for my huey instance, as I run run_huey even in my dev environment. I get this behavior for huey.FileHuey and huey.SqliteHuey. Question What am I missing that my consumer (only on production) does not execute or even receive any task? Setup # settings.py DEBUG = False # True in dev environment HUEY = { 'huey_class': 'huey.FileHuey', 'path': '/tmp/huey', 'immediate': False, 'immediate_use_memory': False, 'consumer': { 'workers': 2, # use 2 threads }, } # my-huey-systemd-service-unit.service [Unit] After=network.target [Service] Restart=always WorkingDirectory=/path/to/project ExecStart=/path/to/venv/bin/python3 \ /path/to/project/manage.py run_huey \ --logfile=/path/to/project/wagtailapiforms-huey.log ExecReload=/bin/kill -s SIGHUP $MAINPID ExecStop=/bin/kill -s SIGINT $MAINPID [Install] WantedBy=default.target # project/apps/core/tasks.py from huey.contrib.djhuey import db_task @db_task() def schedule_virusscan(attachment_id): … -
I want to use django with celery (redis). Do I need to install redis on the server of my django application ? or can I have a seperate redis server?
I installed redis on an ubuntu server following these guidlines. I want to use it with celery for a Django application. My Django application is running on another server. Now I'm confused however on how to set the Django settings, since everywhere I looked I could only find writings about a "localhost". Is it possible to use celery and django with redis on another server ? What Django settings.py do I need to add ? So how do -
django-webpack points to localhost in production
I use vue and django webpack loader for rendering a vue app in django template. it works well locally, but when I deploy it, it goes all wrong In template I have this: {% load render_bundle from webpack_loader %} {% render_bundle "chunk-vendors" %} {% render_bundle "app" %} which in production is rendered like that: <script type="text/javascript" src="http://localhost:8080/js/chunk-vendors.js" ></script> <script type="text/javascript" src="http://localhost:8080/js/app.js" ></script> The vue.config.js looks like that: const BundleTracker = require('webpack-bundle-tracker') const path = require('path'); const DEPLOYMENT_PATH = '/static/dist' module.exports = { publicPath: process.env.NODE_ENV === 'production' ? DEPLOYMENT_PATH : 'http://localhost:8080/', outputDir: '../_static/dist', configureWebpack: { plugins: [ new BundleTracker({path: __dirname, filename: 'webpack-stats.json'}), ], }, "transpileDependencies": [ "vuetify" ], chainWebpack: config => { config.resolve.alias .set('__STATIC__', 'static'); config.devServer .public('http://localhost:8080') .host('localhost') .port(8080) .hotOnly(true) .watchOptions({ poll: 1000 }) .https(false) .headers({ "Access-Control-Allow-Origin": ["*"] }); config.module.rules.delete('eslint'); }, } `` -
Auto Scrollable table is not working on Heroku
I'm working on on this auto scrollable text on my first django project, and it's running just fine locally but once deployed on Heroku, the text will show up as fixed and without format. table.scrollable-content tbody tr{ overflow:auto; display:block; height:200px; } table.scrollable-content tbody tr div{ animation-name: example; animation-duration: 5s; } table.scrollable-content tbody tr:hover div{ animation-name: example2; animation-duration: 5s; } {% load static %} <!DOCTYPE html> <html lang="en"></html> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous"> <link rel="stylesheet" href="{% static 'css/blog.css' %}"> <table class="scrollable-content"> <thead><tr><th>Lorem ipsum dolor sit amet</th></tr></thead> <tbody> <tr> <td> <div> llentesque imperdiet felis congue turpis porttitor, quis mollis quam viverra. Ut placerat mauris sit amet ante dictum, non tempor turpis gravida. Donec euismod, purus quis rutrum facilisis, mi metus sodales quam, non egestas odio quam semper libero. </div> </td> </tr> </tbody> </table> -
MYSQLCLIENT on MacOs
enter image description herei was trying to setup my django project and to migrate data i needed to install mysqlclient. i tried so much things to do fix but its still not working!! when i run brew search mysql it shows mysql-client installed but when i go on my django project and run python3 manage.py runserver it still shows did you installed mysqlclient then i tried pip install mysqlclient but still so many errors i dont understand what to do, im super pissed now my classes on hold because i cant continue without mysql client. im super newbiee in developement. PLEASE HELP! look error in attached picture in below link -
Access denied for user when running django tests
My django project migrate from sqlite3 -> MySQL running on a remote server. Migration completed successfully, site is working correctly, but i have problem with tests: alex@alex-YOGA11S:~/PycharmProjects/WebGalaxyIdent$ python manage.py test Creating test database for alias 'default'... Got an error creating the test database: (1045, "Access denied for user 'FrolovPV'@'%' (using password: YES)") -
Django UserCreationForm only displays inside my HTML file when i dont extend from my base HTML
As the title suggests im having some difficulty getting this form to display when i extend from base.html Code that doesn't display my form but displays my base.html {% extends 'base.html' %} {% block title %}Sign Up{% endblock %} {% block body %} <form method="post"> {% csrf_token %} {{ form }} <button type="submit">Sign up</button> </form> {% endblock %} code that does display my form properly {% block title %}Sign Up{% endblock %} {% block body %} <form method="post"> {% csrf_token %} {{ form }} <button type="submit">Sign up</button> </form> {% endblock %} -
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