Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django on GAE and AWS RDS PostgreSQL
Is it possible to use an AWS RDS PostgreSQL database with a Django app hosted on Google App Engine standard (Python 3)? Currently, any code that tries to connect to the RDS database hangs, but I can connect to the RDS database from my machine. -
Django saves wrong time to database
I have the following model: class lockTime(models.Model): timefield = models.DateTimeField() label = models.CharField(max_length=50, primary_key=True) from the django admin page, when I save a lockTime object, with the time 00:00:00, it is saved to my backend database as 04:00:00 I have updated my settings.py timezone and do not get a message saying "you are 4 hours behind server time": LANGUAGE_CODE = 'en-us' TIME_ZONE = 'America/Toronto' USE_I18N = True USE_L10N = True USE_TZ = True -
Not able to send webcam video to template(.html) file using StreamingHttpResponse in Django 2.2
I recently started django and I wanted to see the video from the laptop camera to Django 2.2 based web app. I was successful in viewing the cam video by directly sending response to web using function display_livefeed. Following is my code of views.py of app 'camerafeed' class mycamera(object): def __init__(self): self.frames = cv2.VideoCapture(0) def __del__(self): self.frames.release() def get_jpg_frame(self): is_captured, frame = self.frames.read() retval, jframe = cv2.imencode('.jpg', frame) return jframe.tobytes() def livefeed(): camera_object = mycamera() while True: jframe_bytes = camera_object.get_jpg_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jframe_bytes + b'\r\n\r\n') @condition(etag_func=None) def display_livefeed(self): return StreamingHttpResponse( livefeed(), content_type='multipart/x-mixed-replace; boundary=frame' ) I used path('monitor/', display_livefeed, name='monitor'), to see the video streaming on http://127.0.0.1:8000/monitor/ and it works perfectly >>Image from the streaming video<< Now I wanted to display on the html template just line it is done here: https://www.pyimagesearch.com/2019/09/02/opencv-stream-video-to-web-browser-html-page/ but this was done by using Flask. But I wanted to do the same using Django and got stuck. Here is the html file from the above link. <html> <head> <title>Pi Video Surveillance</title> </head> <body> <h1>Pi Video Surveillance</h1> <img src="{{ url_for('video_feed') }}"> </body> I tried to do like this using by making this function: def video_feed(request): return StreamingHttpResponse( livefeed(), # Calling livefeed() function content_type='multipart/x-mixed-replace; boundary=frame' ) … -
Pulling API data into Django Template that has <a></a> tags embedded, is there a way of wrapping the text in an HTML tag?
I'm reading a very large API, one of the fields I need, have "a" tags embedded in the item in the dictionary and when I pull it into my template and display it, it shows the "a" tags as text. exp: "Bitcoin uses the <a href="https://www.coingecko.com/en?hashing_algorithm=SHA-256">SHA-256</a> hashing... ...such as <a href="https://www.coingecko.com/en/coins/litecoin">Litecoin</a>, <a href="https://www.coingecko.com/en/coins/peercoin">Peercoin</a>, <a href="https://www.coingecko.com/en/coins/primecoin">Primecoin</a>*" I would like to wrap this in HTML so when it displays on the page it has the actual links rather than the 'a' tags and the URL. What I'm looking to get: "Bitcoin uses the SHA-256 hashing... ...such as Litecoin, Peercoin, Primecoin*" -
How to serialize a JSON field with multiple permissible keys?
I am trying to serialize a json field, with list values. Something like this: { "source_1": ["key_1"]. "source_2": ["key_3", "key_4"] } I am trying to serialize this using nested serializers, stuck in the middle: models.py class ModelA(models.Model): AVAILABLE_SOURCES = [ "source_1", "source_2" ] SOURCE1_KEYS = ["key_1", "key_2"] SOURCE2_KEYS = ["key_3", "key_4"] custom_keys = JSONField() serializers.py class CustomKeySerializer(serializers.Serializer): # TODO: Here I need to check if all the sources and # keys corresponding to them are valid pass class ModelASerializer(serializers.ModelSerializer): custom_keys = CustomKeySerializer() class Meta: model = ModelA fields = ('__all__',) If at all this cannot be done like this, what can be the other possible ways to do this? -
how to store my converted PDF tabular data into csv into database?
I have converted pdf tables data into CSV in my Django projects using Camelot and it automatically stores in my root directory. now how I'm gonna put my CSV data into my MySQL database? I have created my Model as the CSV file row's name. can anyone help to give ideas? ''' def tables_extract(file_name): filename_with_path = '/pdfs/{}'.format(file_name) basename = os.path.basename(filename_with_path) basename_without_ex = os.path.splitext(basename)[0] tables = camelot.read_pdf(filename_with_path, pages="1-end") table= tables[2].df csv = table.to_csv("{}.csv".format(basename_without_ex)) return csv ''' -
Speed up multiple files upload (jQuery POST)
So, I'm trying to upload a large file (size around 2MB) from the browser to my server running Django, through jQuery's $.post(), like so: async function validateFile(fileElement){ let url = "validate_file/"; let data = new FormData(); data.append("input_name", fileElement.name); let file = fileElement.files[0]; data.append(fileElement.name, file); try { let res = await $.post({ url: url, data: data, processData: false, contentType: false }); return res; } catch (err) { console.error("Validation failed = ",err); return false; } } But the file upload seems extremely slow. Chrome's network timeline shows that uploading the file is taking a huge amount of time. chrome network timeline (attached image) Many users from different regions have reported the same issue, so I think the problem lies somewhere other than my network conditions. The user would be uploading multiple such large files, so such a huge delay is unacceptable. Is there some issue with my AJAX request, or are there some ways/libraries to speed up the file upload ? I'm using jQuery v3.4.1 and Django v2.1.5 -
Django Filters Library
Hi everybody I'm trying to use django_filters library to filter through a list of items. Anyway when the user enter the word he wants to filter by, it doesn't show anything. What am I doing wrong? Hope someone could help! Thanks a lot this is the filters.py import django_filters from .models import Info from django_filters import CharFilter class InfoFilter(django_filters.FilterSet): disco = CharFilter(field_name='disco', lookup_expr='icontains') class Meta: model = Info fields = ['band', 'disco'] this is the view.py: class searchesView(TemplateView): template_name = "search/searches.html" def post(self, request, *args, **kwargs): print('FORM POSTED WITH {}'.format(request.POST['srh'])) srch = request.POST.get('srh') if srch: sp = Info.objects.filter(Q(band__icontains=srch) | Q(disco__icontains=srch)) paginator = Paginator(sp, 10) page_number = request.GET.get('page') page_obj = paginator.get_page(page_number) myFilter = InfoFilter(request.GET, queryset=sp) sp = myFilter.qs return render(self.request, 'search/searches.html', {'sp':sp, 'page_obj': page_obj, 'myFilter':myFilter }) else: return render(self.request, 'search/searches.html') and last this is the template: {% if sp %} <form method="get"> {{myFilter.form}} <input type="submit" value="FILTER"/> </form> {% for k in sp %} <div class="container_band" id="display"> <div class=album_band> <!-- insert an image --> {%if k.cover%} <img src= "{{k.cover.url}}" width="100%"> {%else%} <img src="{{no}}" width="100%"> {%endif%} </div> <div class="info_band"> <!-- insert table info --> <table> <tr><th colspan=2><h3>{{k.band}}</h3></th></tr> <tr><td> Disco: </td><td> {{k.disco}} </td></tr> <tr><td> Anno: </td><td> {{k.anno}} </td></tr> <tr><td> Etichetta: </td><td> {{k.etichetta_d}} </td></tr> <tr><td> … -
Django admin set value of read only field from external service
I have a model whereby an id to store is generated by an external service, a 'job_id'. On creation, a post request is sent to this external service, and the job_id is sent back. The model is created only if this job_id is received. Within the django admin, this job_id is set to read only, as this will be created by an external service. I have placed the request to the external service in the clean function: def clean(self): if not self.cleaned_data.get('job_id'): response = requests.post(external_service, params=params, data=data) if response.status_code != 200: raise ValidationError(_("external_service unavailable. Please try again")) self.cleaned_data.update({'job_id': self.__extract_job(response)}) However I cannot seem to add the job_id to the cleaned data, I keep getting: NOT NULL constraint failed: external_service.job_id I have also tried self.cleaned_data.setdefault('job_id', self.__extract_job(response)) but I cannot seem to attach the job_id data to the field. -
Cannot filter out logs from the console
I am trying to filter out certain endpoint logs from the console. My settings.py looks like this: def skip_rss_requests(record): if record.args and record.args[0].startswith('GET /api/feed/rss/'): print("HEEERRRRREEEE") return False return True LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'skip_rss_requests': { '()': 'django.utils.log.CallbackFilter', 'callback': skip_rss_requests } }, 'formatters': { 'simple': { 'format': '[%(asctime)s] %(levelname)s|%(name)s|%(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S', }, }, 'handlers': { 'console': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'filters': ['skip_rss_requests'], # 'stream': sys.stdout, 'formatter': 'simple' }, However, I am still seeing these logs in my console output despite the condition being met: [2020-04-28 13:31:11] INFO|django.request|GET /api/feed/rss/ [2020-04-28 13:31:13] INFO|django.request|GET /api/feed/rss/ - 200 HEEERRRRREEEE Any ideas as to why these records are still being logged? -
Django deployment to Postgres with AWS
I am having an issue with making migrations to my newly configurated database on amazon server. When running python manage.py makemigrations I get the error: conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: FATAL: password authentication failed for user "ubuntu" FATAL: password authentication failed for user "ubuntu" I am stucked and being frankly a beginner and I am confused because although I run a virtual ubuntu machine to host my django app, I don't have a "ubuntu" user. Here is my .env file: SECRET_KEY = secretkey.. DEBUG = False NAME = dbexostock USER = pierre PASSWORD = mypassword HOST = hostnameprovidedbyamazonrds settings.py 'default': { 'ENGINE': 'django_tenants.postgresql_backend', 'NAME': config('NAME'), 'USER': config('USER'), 'PASSWORD' : config('PASSWORD'), 'HOST': config('HOST'), 'PORT': '5432', } } and one of my form needs to write in the database such as: engine = create_engine('postgresql://pierre:mypassword@:hostnameprovidedbyamazonrds:5432/dbexostock', connect_args={'options': '-csearch_path={}'.format(dbschema)}) metricsdb = metrics.to_sql('dashboard_metrics', engine, if_exists='replace') I am out of things to verify, I would appreciate any help on the issue -
Creating a new related object from a predefined parent object in a one to many relation in django
Hi I am making a car dealership project in django and I would like to be able to have a functionality of creating a review on a car just like how one can create a comment on a post in a blog app or social site. In my case the parent object is the car and the related object the review. The ideal outcome would be for one to click on review on the car and the web app to know which car i am reviewing. This is what I had tried so far car Models.py from django.db import models from django.urls import reverse from categories.models import Category # Create your models here. class Car(models.Model): image = models.ImageField(upload_to='images/cars/') make = models.CharField(null=False, blank=False, max_length=30) model = models.CharField(null=False, blank=False, max_length=30) year = models.IntegerField(null=False, blank=False) transmission = models.CharField(null=False, blank=False, max_length=30) category = models.ForeignKey(Category, on_delete=models.CASCADE) def __str__(self): return self.model def get_absolute_url(self): return reverse('car_detail', args=[str(self.id)]) car review models.py from django.db import models from django.urls import reverse from users.models import CustomUser from cars.models import Car # Create your models here. class Review(models.Model): title = models.CharField(max_length=20) description = models.TextField(max_length=160) date_added = models.DateTimeField(auto_now_add=True) user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) car = models.ForeignKey(Car, on_delete=models.CASCADE) def __str__(self): return self.title def get_absolute_url(self): return … -
Unsupported lookup 'lower' for CharField or join on the field not permitted
I'm attempting to perform a slightly more advanced lookup on a queryset, as outlined here: https://docs.djangoproject.com/en/3.0/topics/db/search/ My queryset is as follows: queryset = queryset.filter( Q(user__username__unaccent__lower__trigram_similar=search) | Q(user__first_name__unaccent__lower__trigram_similar=search) | Q(user__last_name__unaccent__lower__trigram_similar=search) ) I've included the django.contrib.postgres app in INSTALLED_APPS in my settings.py. However, I receive the following error: django.core.exceptions.FieldError: Unsupported lookup 'lower' for CharField or join on the field not permitted. Does anyone know the correct procedure for search a search? I am using Django >3 and postgres engine version 11. -
Python timedelta to Postgres interval
I have a Django DurationField: timerep = models.DurationField(null=True, blank=True) The value for it comes from input <input type="number" name="timereph" value="{{ timereph|floatformat }}" min="0" max="99.9" step="0.1" class="numberinput form-control" id="timereph"> and gets recorder def form_valid(self, form): check_closed = self.request.POST.get('check_closed', None) timereph = self.request.POST.get('timereph', None) self.object = form.save(commit=False) if check_closed is not None: if timereph == '' or timereph is None: self.object.timerep = timezone.now() - self.object.date_added if timereph != '' and timereph is not None: self.object.timerep = timedelta(hours=float(timereph)) self.object.save() Everything worked fine on SQLite. But when i switched to Postgres i started getting an error (doesn't matter which if condition passes: ProgrammingError at /mwo/order/1/edit column "timerep" is of type double precision but expression is of type interval LINE 1: ..."timerep" = '0 days 32... ^ HINT: You will need to rewrite or cast the expression. It seems that in Postgres DurationField is stored as interval. Is there a function i could use to convert Python timedelta to this interval type. -
Django - how to 'remember' some SQL queries
There's this line in the Django documentation: "When running with DEBUG turned on, Django will remember every SQL query it executes. This is useful when you’re debugging, but it’ll rapidly consume memory on a production server." So if I want to remember some queries when DEBUG=False on my server, how do I do it? Use Redis/memcached I guess? But they are key-value stores, so I will need to go over several files and change the code, right? What if I just want to cache a SQL query that's repeated often. The documentation around caching is mostly about template caching or storing key-value pairs. I couldn't find anything about caching full, often used SQL query. -
How django REST Framework works regarding cache and SQL requests
Working currently with DRF for my project, I decided to use a cache system (REDIS in that case) in order to limit the number of SQL requests directly to the DB. So I saw/read many pages on that topic, but I didn't find out my answer. on the DRF web site, they talk about to use the cache_page with the help of the decorator @method_decorator. Ok it works correctly but in the HTTP header, an expiration_date field appears, indicating to the browser to put in its cache the whole page and do not request anything until the end of the expiration date. So if the expiration is around current time + 5 minutes, during 5 minutes we "live" with the web browser cache, and if something change during that 5 minutes, we could not know it. Are you agree with that behavior of that decorator? Since I would like to be informed about any changes during that time, I update my code to cache all needed SQL requests. Like this if something change, I flush the cache and the next time, I will get the up-to-date datas. Here is an light example of a ModelViewSet: queryset = MyModel.objects.all().select_related(...).prefeth_related(...) cache.set('MyKey', queryset) … -
An unexpected effect appears when using django's Q
In [25]: Project.objects.filter(Q(change__date__date__range=('2020-04-28','2020-04-28'))&~Q(change__se__isnull=True)).count() Out[25]: 8 In [26]: Project.objects.filter(Q(change__date__date__range=('2020-04-28','2020-04-28'))&Q(change__se__isnull=False)).count() Out[26]: 9 enter image description here I don't know what the difference is -
GET Response with several models
I have two ViewSets. They have unique serializer and model. For example, CitiesViewSet and TypesViewSet: CitiesViewSet - [{"name":"Moscow"},{"name": "Kazan"} etc...] TypesViewSet - [{"id": 1, "name": "sample"}, {"id": 2, "name": "sample"} etc... ] I want to combine this ViewSets to one GET Response. For example, I can do GET request and I will get looks like - { "cities": [ {"name": "Moscow"}, {"name": "Kazan"} etc... ], "types": [ {"id": 1, "name": "sample"}, {"id": 2, "name": "sample"} etc. ] } How to create it? -
Django Model Foreign Key to text
There is a problem, there is a Book model and in it title is the name of the book, and author is the ForeignKey of the Author model where there is a name field. I need to get a Python list and for this I do books = list(Book.objects.values('title', 'author'), but the result is a list [{'title': 'Harry Potter', 'author': 1}]. How do I make sure that the author is instead of the unit? -
Display a checkbox choice as "disabled" in django 3.0. ModelForm
I've been looking around for similar issues and I've yet to come across a solution to my specific problem. What I aim to do is display a checkbox like this. Where only the signed in (authenticated) user has the ability to select/deselect the checkbox corresponding to his profile. In this case, mindspace is authenticated I reached this point by trying this solution: But as one of the comments suggest the create_option method doesn't get called through the CheckboxSelectMultiple class. As a result I messed a bit with the SelectMultiple class, like-so. class CheckboxCustom(forms.SelectMultiple): def __init__(self, *args, **kwargs): self.input_type = 'checkbox' self.checked_attribute = {'checked': True} self.template_name = 'django/forms/widgets/checkbox_select.html' self.option_template_name = 'django/forms/widgets/checkbox_option.html' self.allow_multiple_selected = True self.request = kwargs.pop("request") self.option_inherits_attrs = False # print(options super(CheckboxCustom, self).__init__(*args, **kwargs) def create_option(self, *args, **kwargs): options_dict = super().create_option(*args, **kwargs) selected = options_dict['selected'] print(selected) option_attrs = self.build_attrs(self.attrs, attrs) if self.option_inherits_attrs else {} # if selected: # options_dict['attrs']['multiple'] = True if options_dict['label'] != str(self.request.user.profile): options_dict['attrs']['disabled'] = True # print(val) return options_dict And used this as a widget. However, no matter if I select or deselect one of the two checkboxes (from the corresponding profile), the other will get deselected both in the template and the database. Same thing happens … -
Django one to many get all underlying records
I'am creating a django application with a database including the following tables: I created the models as following: class Group(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) class Meta: db_table = 'Group' class Filters(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) group_id = models.ForeignKey(Group, on_delete=models.CASCADE, db_column='GroupId') class Meta: db_table = 'Filters' The goal is to call an endpoint and return a list with all Groups and underlying Filters. Is it possible to achieve this? If possible, I want it in a Serializer class. I know I can get the group of a filter record, but is this also possible the otherway around? -
FileField is valid even when empty
I have a parent and child model: class Parent(models.Model): name = models.SlugField(max_length=45, null=False) tags = TaggableManager() class Child(models.Model): version = models.CharField(null=False, max_length=10, default='1.0') upload = models.FileField( upload_to=hack_upload_path, blank=False, null=False ) parent = models.ForeignKey( Parent, on_delete=models.CASCADE, related_name='versions' ) class Meta: get_latest_by = "upload_date" verbose_name = "version" verbose_name_plural = "versions" def __str__(self): return f'v{self.version}' A parent basically has multiple children which are just file uploads with some more data. So I have a form where I want to create a Parent and one Child at the same time, so I made this form and view: class ParentForm(ModelForm): class Meta: model = Parent fields = [ 'name', 'tags' ] ChildFormset = inlineformset_factory( Parent, Child, fields=( 'version', 'upload' ), extra=1, max_num=1, can_delete=False, ) class ParentCreateView(LoginRequiredMixin, CreateView): form_class = ParentForm template_name = 'new.html' def get_context_data(self, **kwargs): data = super().get_context_data(**kwargs) if self.request.POST: data['versions'] = ChildFormset( self.request.POST, self.request.FILES, instance=self.object ) else: data['versions'] = ChildFormset(instance=self.object) return data def form_valid(self, form): form.instance.seller = self.request.user context = self.get_context_data() versions = context['versions'] if versions.is_valid(): # It comes here even though the upload field is empty! self.object = form.save() versions.instance = self.object versions.save() return super().form_valid(form) else: return self.render_to_response( self.get_context_data( form=form, versions=versions ) ) And It passes the if versions.is_valid(): check even though … -
How to add for loop iteration django templates
I have a template page and I have used models required in views.py file. The template page consists of cricket statistics and it consists of tb, tr tags along with div tags. My question is where should I use the for loop template tags to replace the values as per the model data. {% extends 'base.html' %} <!DOCTYPE html> <html> {% block content %} <head> <meta charset="utf-8"> {% load staticfiles %} <link rel="stylesheet" href="{% static 'css/details.css' %}"> <style> td,tr { height: 10px; width: 10px; } th, td { padding: 10px; } </style> </head> <body> <div class="page"> <div id="page-wrapper" class="container" style="display:inline-block;"> <div id="shosh" class="ad-unit shosh-embed" style="height:0; width:980px; text-align:center;"></div> <div style="margin-top:20px;"></div> <div> <div class="cb-col cb-col-100 cb-bg-white"> <div class="cb-col cb-col-20 cb-col-rt"><img height="152" width="152" title="profile image" src="{{PlayerStructure_details.imageUri.url}}"></div> <div class="cb-col cb-col-80 cb-player-name-wrap"> <h1 itemprop="name" class="cb-font-40">{{PlayerStructure_details.firstname}} {{PlayerStructure_details.lastname}}</h1> <h3 class="cb-font-18 text-gray">{{PlayerStructure_details.country}}</h3></div> </div> <div class="cb-col cb-col-100 cb-bg-grey"> <div class="cb-col cb-col-33 text-black"> <div class="cb-hm-rght"> <div class="cb-font-16 text-bold"> Personal Information</div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Born</div> <div class="cb-col cb-col-60 cb-lst-itm-sm"> {{PlayerStructure_details.BirthDate}} </div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Birth Place</div> <div class="cb-col cb-col-60 cb-lst-itm-sm"> {{PlayerStructure_details.BirthPlace}} </div> <div class="cb-col cb-col-40 text-bold">Jersey No</div> <div class="cb-col cb-col-60"> {{PlayerStructure_details.JerseyNumber}} </div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Role</div> <div class="cb-col cb-col-60 cb-lst-itm-sm"> {{PlayerStructure_details.Role}} </div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Batting Style</div> … -
Django - Retrieve unique rows by latest date/column
I have the following table: class AccountData(models.Model): id = models.BigIntegerField(primary_key=True) date = models.DateField() last_update = models.DateTimeField() account = models.ForeignKey('Account', models.DO_NOTHING, db_column='account', related_name="values") value = models.DecimalField(max_digits=65535, decimal_places=65535, blank=True, null=True) Given a queryset/list of ID's (unknown amount how many ID's, could be None/0) Is there a way to get the latest row for each account, then sum the value? The part I'm really struggling with is only retrieving the latest row per account. I know I can get the accounts that are in the given queryset by doing: accounts = [1, 4, 65] AccountData.objects.filter(account__in=accounts) Just need to figure out how I can only get the rows where "date" is as close to "now" as possible. Thus resulting in having unique rows (only 1 row per account) -
Must i install django every time am starting a project in pycharm
i am just starting a new project on my pycharm i start a new project and run my server but am getting this error message couldn't import django, Must i install django for every project am creating?