Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
get_serving_url silently fails with django-storages, app engine, python 3.x
I am trying to get the seving_url for a project that uses django-storages with google cloud storage for media files. I am trying to serve the files with get_serving_url, but I get a silent failure here with no text logged in the exception. The blobkey generates correctly from what I can see however the image = images.get_serving_url(blobkey, secure_url=True) raises an exception with no error text. This is what I have done: #storage_backends.py class GoogleCloudMediaStorage(GoogleCloudStorage): """GoogleCloudStorage suitable for Django's Media files.""" def __init__(self, *args, **kwargs): if not settings.MEDIA_URL: raise Exception('MEDIA_URL has not been configured') kwargs['bucket_name'] = setting('GS_MEDIA_BUCKET_NAME') super(GoogleCloudMediaStorage, self).__init__(*args, **kwargs) #this works fine def url(self, name): """.url that doesn't call Google.""" return urljoin(settings.MEDIA_URL, name) #https://programtalk.com/python-examples/google.appengine.api.images.get_serving_url/_ #This does not work yet def serving_url(self, name): logging.info('serving url called') if settings.DEBUG: return urljoin(settings.MEDIA_URL, name) else: # Your app's GCS bucket and any folder structure you have used. try: logging.info('trying to get serving_url') filename = settings.GS_MEDIA_BUCKET_NAME + '/' + name logging.info(filename) blobkey = blobstore.create_gs_key('/gs/' + filename) logging.info('This is a blobkey') logging.info(blobkey) image = images.get_serving_url(blobkey, secure_url=True) return image except Exception as e: logging.warn('didnt work') logging.warn(e) return urljoin(settings.MEDIA_URL, name) I have appengine-python-standard installed and I have wrapped my application #main.py from antiques_project.wsgi import application from google.appengine.api import … -
Wagtail 4 FieldPanel issue with ForeignKey
I have the following setup in wagtail 4.0.4: @register_snippet class Copyright(models.Model): class CopyrightType(models.TextChoices): PH1 = "PH1", _("Phase 1") PH2 = "PH2", _("Phase 2") type = models.CharField( max_length=3, choices=CopyrightType.choices, unique=True, ) class ReportPage(Page): copyright = models.ForeignKey( Copyright, to_field="type", default="PH2", on_delete=models.SET_DEFAULT, ) Now the set up works just fine. But if I add the FieldPanel: class ReportPage(Page): copyright = models.ForeignKey( Copyright, to_field="type", default="PH2", on_delete=models.SET_DEFAULT, ) promote_panels = Page.promote_panels + [ FieldPanel("copyright"), ] I get the following error: ValueError: Field 'id' expected a number but got 'PH2'. Is there any way to tell the FieldPanel to look at the field referenced in the "to_field" option? -
Django apache image loading slow
I have django deployed with apache2. I'm using static files collected using "./manage.py collectstatic"; noticed png images are loading very slow. Image size is only 130Kb and its taking 2500ms to load. Following is html and css. <img class='image1' src="{% static 'index_background.png' %}" style="width:100%;"> <div class="index_feature">test</div> <img class='image2' src="{% static 'index_banner.png' %}"> .indexbar1 { position: relative; top: 0; left: 0; } .imgage1 { position: relative; top: 0; left: 0; } .image2{ position: absolute; width: 600px; top: 35%; right: 5%; } .index_feature { position: absolute; top: 65%; left: 5%; font: 50px lato-regular, Arial, Helvetica, sans-serif; font-weight: 900; line-height: 150%; color: white; } apache2 is deployed on ubuntu 20.04 vm with 16vcpu and 32gb memory. I could see the same issue while using vscode to run django locally without apache2. I'm missing something in django and need some help. -
Static files are served on django admin site but not on the main website after migrating from Django 1.5 to 2.2.5
My trouble is the following. I did a migration from django 1.5 to 2.2.5. All fine on development environment, but when I deployed it to the internet, static files were being served on admin site but not on the main website. I debug this problem and realize that scheme of the request is passed from http to https and console panel shows the following messages: The scheme is https instead of http. But in django admin site the static files are served under http scheme. For this reason, the static files can be loaded on the site. Also, the login page is loaded correctly, but when I reach the next page, the same problem occurs. I am deploying the application on server without SSL certificates for testing. Initially the web server was apache but I changed it to nginx. I thought the problem was the web server but I was wrong. I want the application can serve the static files correctly under http protocol. -
Optimizing Django Aggregation Over Subsets
I'm currently trying to do some summary statistics calculations for date-based subsets of a large "competition" table (~3M rows) stored in SQLite. Specifically, I'm trying to calculate statistics for: this year last year the lifetime of the competitor Here's a model breakdown: class Competitor(models.Model): # ... ID is the only important part here class Venue(models.Model): # ... ID is the only important part here class Division(models.Model): venue = models.ForeignKey(Venue) # ... class Level(models.Model): division = models.ForeignKey(Division) # ... class Class(models.TextChoices): STANDARD = "Standard", _("Standard") JWW = "JWW", _("JWW") class Run(models.Model): competitor_id = models.ForeignKey(Competitor, related_name="runs", db_index=True) date = models.DateField(verbose_name="Date created", db_index=True) MACH = models.IntegerField(..., db_index=True) PACH = models.IntegerField(..., db_index=True) yps = models.FloatField(..., db_index=True) score = models.IntegerField(..., db_index=True) qualified = models.BooleanField(..., db_index=True) division = models.ForeignKey(Division, db_index=True) level = models.ForeignKey(Level, db_index=True) cls = models.CharField(max_length=..., choices=Class.choices) # ... Other fields that aren't relevant For each Competitor, I want to generate summary statistics that describe their performance this year, last year, and over all time, and store that in a Report model: class CurrPrevLifetime(models.Model): curr_yr = models.FloatField(default=0) prev_yr = models.FloatField(default=0) lifetime = models.FloatField(default=0) class Report(models.Model): ... = models.ForeignKey(CurrPrevLifetime, related_name=...) # repeat as needed for as many fields need this My current aggregation setup looks like … -
I am add url look like en but rosetta did not translate static html word
I am add url look like en but rosetta did not translate static html word How i can resolve this problem I a add all settings for rosetta but it did not work -
How to access the request object inside a custom logging formatter in Django?
I have setup a custom formatter to log my data into a text file in JSON format: class CustomJsonFormatter(jsonlogger.JsonFormatter): def add_fields(self, log_record, record, message_dict): super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict) log_record['timestamp'] = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') log_record['level'] = record.levelname log_record['location'] = record.name I would like to be able to automatically access my request object within this formatted since I have a "unique request identifier" that I would like to add to my logs. This way I know which log lines belong to which request. class CustomJsonFormatter(jsonlogger.JsonFormatter): def add_fields(self, log_record, record, message_dict): super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict) log_record['timestamp'] = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') log_record['level'] = record.levelname log_record['location'] = record.name log_record['request_id'] = request.uuid <--- Something like this Is there a way to achieve this without having to manually pass the request object to every single log line? Many thanks in advance -
How to print data from two different methods next to each other?
I hava a django application and I have two methods that returns different text. And now the data from the two methods are printed under each other. But I want to have them next to each other. So this is the combined twom methods in one method: def combine_methods(self, file_name): self.extractingText.extract_text_from_image(file_name) return '\n'.join(self.filter_verdi_total_number_fruit(file_name)) + " " + '\n'.join( self.filter_verdi_fruit_name(file_name)) And this is the output: 16 360 6 75 9 688 22 80 160 320 160 61 Watermeloenen Watermeloenen Watermeloenen Watermeloenen Watermeloenen Appels Sinaasappels Sinaasappels Sinaasappels Sinaasappels Sinaasappels Sinaasappels But I want to have it like: 16 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen 360 watermeloenen -
Browser cant find file or directory in Django
I am trying to create a qr code and store it on disk. Running the program from my pc works fine, but when I run it on the server (deploy is done in docker), I get this error: [Errno 2] No such file or directory: '/app/static/QR /QR-1-9.png' and the browser also points me to this line of my code with open(UPLOADS_PATHS, 'wb') as f: The route is defined in the settings file like this: QR_ROOT = os.path.join(BASE_DIR, 'static/QR') And this is the view where the qr is stored on disk nombre_archivo = f'QR-{request.user.instituto.id}-{idRecibo}.png' UPLOADS_PATHS = os.path.join(settings.QR_ROOT, nombre_archivo) with open(UPLOADS_PATHS, 'wb') as f: f.write(response.content) I try this on settings.py: QR_ROOT = os.path.join(BASE_DIR, './static/QR') but it does not work -
How to access server data in a javascript file that is loaded by Django admin pages
I need to access server data in a javascript file that gets loaded with an admin page. In my case, I'll need a json variable that is set in the settings.py file (in the production version, this json-variable gets loaded from env-variable which means that the content of the variable is known at startup-time but is not hard coded). I use the javascript to validate and populate some fields in an admin-page - that is a better UX than doing it server side. Is it possible to add script-tag to the admin html-pages' head section, that contains server data, i.e. non-static data? -
Unable to find user django-user: no matching entries in passwd file
I was following a tutorial on docker and django. Not really hard but at one time, i get on this error and i don't understand where is the error on the dockerfile. I understand the error given by docker but dont understand what is wrong in the Dockerfile. Docker file : FROM python:3.9-alpine COPY ./requirements.txt /requirements.txt COPY ./app /app WORKDIR /app RUN python -m venv /py && \ /py/bin/pip install --upgrade pip && \ /py/bin/pip install -r /requirements.txt && \ adduser --disabled-password --no-create-home django-user ENV PATH="/py/bin:$PATH" USER django-user And here is the error : Creating django-deploy-gap_app_run ... done Error response from daemon: unable to find user django-user: no matching entries in passwd file Any help is welcome Thank's ! -
what is the using of Signals in Django?
I'm new to Django and I'm watching a tutorial on youtube, and I have a problem, he using ready functions I don't understand what is the meaning of these functions. my question is .. can anyone explain the (Signals, Senders, and receivers) and their use? if you can recommend someone course for Django I will appreciate it -
Access private GitHub Django repositories on requirements.txt when using AWS Codebuild
I am trying to use AWS Codebuild and am having problems accessing private python Django github repositories from my requirements.txt file. Getting this error "Permission denied (publickey). fatal: Could not read from remote repository." Is there a solution to enable access? -
Possible Race Condition when limiting model creation by count
I have a django view function representing a redemption process of a prize. I'm trying to eliminate the possible race condition when setting a redemption limit based on the count of a linked model (Redemption). At a high level my view function looks like this: def redeem(request, prize_id): # Get a prize model that contains a limit attribute prize = Prize.objects.get(id=prize_id) # Check the count of a set of another model representing redemption objects if (prize.redemption_set.count() >= prize.redemption_limit): return error_page("Reached redemption limit") else: # Run some API calls that redeem the prize # Create a redemption object in the DB redemption = Redemption(prize=prize) redemption.save() So my main concern is when concurrent requests come in I can see the possibility of extra redemptions occurring if the count isn't updated by the time another request comes in. I was looking at select_for_update() and atomic requests but I don't want errors to occur if the model is locked. I mainly want to make sure the redeem calls enter a queue and are rejected appropriately when reaching the redemption limit. I'll have multiple web workers and my DB is mySQL. Thanks for any tips! -
Face error in install Django REST framework
When I write: pip install djangorestframework In CMD show me: Fatal error in launcher: Unable to create process using '"H:\Full Stack\Django\Django_All\project1\env\Scripts\python.exe" "H:\Full Stack\Django\Django_All\project1\studybud\env\Scripts\pip.exe" install djangorestframework': The system cannot find the file specified -
<slug> path converter not working, but literal <slug> in URL works
When I try to visit this URL: http://127.0.0.1:8000/slugtest/ I get 404 page. Here's the message that I get: Using the URLconf defined in scrap.urls, Django tried these URL patterns, in this order: admin/ api-auth/ getpage/ ^<slug>/$ [name='ArticleInfoViewSet-list'] ^<slug>/(?P<slug>[^/.]+)/$ [name='ArticleInfoViewSet-detail'] __debug__/ The current path, slugtest/, didn’t match any of these. However, if I use http://127.0.0.1:8000/<slug>/ the page loads perfectly, but that's not intended behavior. models.py class Article(models.Model): url = models.CharField(max_length=255,validators=[RegexValidator(regex=website_regex)]) slug = models.CharField(max_length=255, null=True) unique_id = models.CharField(max_length=255, unique=True, null=True) views.py class ArticleInfoViewSet(ModelViewSet): serializer_class = ArticleInfoSerializer lookup_url_kwarg = 'slug' lookup_field = 'slug' def get_queryset(self): queryset = Article.objects.prefetch_related('data') return queryset serializer.py class ArticleInfoSerializer(serializers.ModelSerializer): data = ArticleDataSerializer(many=True) class Meta: model = Article fields = ['url', 'data', 'slug'] lookup_field = ['slug'] read_only_fields = ['url', 'data', 'slug'] urls.py from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), path('api-auth/', include('rest_framework.urls')), path('getpage/', include('getpage.urls')), path('', include('getpage.urls2')), path('__debug__/', include('debug_toolbar.urls')), ] urls2.py from . import views from rest_framework_nested import routers router = routers.SimpleRouter() router.register('<slug>', views.ArticleInfoViewSet, basename='ArticleInfoViewSet') urlpatterns = router.urls What am I doing wrong here? -
Nginx Gunicorn powered server unable to access media in a separate postgresql server
Long story short, my BE dev left midway through setting up my apps' BE server and I'm trying to pick up the pieces to find out what is left to do. I'm by no means any form of a developer, so I apologize in advance for asking silly questions and if I am lacking any details needed to better understand my problem. Will do my best updating this question and be as concise as I can as I go along. Problem The Django admin panel runs in https, but is unable to access the files stored in the database. Current configuration Virtual machine 1 - Docker, Nginx, Gunicorn, Django (admin panel) Virtual machine 2 - postgresql (database) Here are the following file configurations docker-compose.yml version: "3" services: theapp: image: theapp-be/python-3.10-full container_name: theapp-be build: ./ command: bash -c "./deploy.sh" volumes: - ./:/usr/app/ - ./ssl/:/etc/ssl/theapp-BE/ expose: - "80" restart: always nginx: image: theapp-be/nginx container_name: theapp-be-nginx build: ./nginx volumes: - ./:/var/www/theapp-BE/ - ./ssl/:/etc/ssl/theapp-BE/ - ./nginx/config/:/etc/nginx/conf.d/ - ./nginx/log/:/var/log/nginx/ ports: - "80:80" - "443:443" depends_on: - theapp restart: always default.conf (nginx) # Upstreams upstream wsgiserver { ip_hash; server theapp-be:80; } # Redirect to HTTPS server { listen 80; listen [::]:80; server_name theapp; access_log /var/log/nginx/theapp-access.log; error_log … -
Question about sorting by date inside a database using Django
I have a database, in which it's possible to find products by their name. The DB has ID, category, name, amount and date defined, and I was trying to create a separate search field that would let me search those items by the date they were added. The models.py looks like this: class Expense(models.Model): class Meta: ordering = ('-date', '-pk') category = models.ForeignKey(Category, models.PROTECT, null=True, blank=True) name = models.CharField(max_length=50) amount = models.DecimalField(max_digits=8, decimal_places=2) date = models.DateField(default=datetime.date.today, db_index=True) def __str__(self): return f'{self.date} {self.name} {self.amount}' And views.py looks like this: class ExpenseListView(ListView): model = Expense paginate_by = 5 def get_context_data(self, *, object_list=None, **kwargs): queryset = object_list if object_list is not None else self.object_list form = ExpenseSearchForm(self.request.GET) if form.is_valid(): name = form.cleaned_data.get('name', '').strip() if name: queryset = queryset.filter(name__icontains=name) return super().get_context_data( form=form, object_list=queryset, summary_per_category=summary_per_category(queryset), **kwargs) I've added the "date" field under the "name", following it's structure, but I kept getting the 'datetime.date' object has no attribute 'strip' error. Is there a different format for defining the date? When I've also added it to the search field in forms.py, it was seen there as a string. I've also found a similar post from How to make date range filter in Django?, but it didn't explained … -
In Django, restrict user view using class based views
I have this url pattern: path("user/<int:pk>", MyAccountView.as_view(), name='my_account'), And this view: class MyAccountView(DetailView): model = CustomUser When the user is logged Django redirect to that URL. The problem is that any user can access other users. For example, if the logged user has pk 25, he can access the view of user with pk 26 by writing in the browser url box: localhost:8000/user/26 I want that each user can access to his user page only, so if user with pk 25 try to access the url with pk 26, the access should be denied. Can you point me in some direction of how this is done? The Django documentation is very confusing in this respect. Thanks. -
How to combine two methods for uploading file?
I have a Django application And I have a upload functionality. And I have two methods that shows the extracted text: def filter_verdi_total_number_fruit(self, file_name): self.extractingText.extract_text_from_image(file_name) regex = r"(\d*(?:\.\d+)*)\s*\W+(?:" + '|'.join(re.escape(word) for word in self.extractingText.list_fruit) + ')' return re.findall(regex, self.extractingText.text_factuur_verdi[0]) def filter_verdi_fruit_name(self, file_name): self.extractingText.extract_text_from_image(file_name) regex = r"(?:\d*(?:\.\d+)*)\s*\W+(" + '|'.join(re.escape(word) for word in self.extractingText.list_fruit) + ')' return re.findall(regex, self.extractingText.text_factuur_verdi[0]) But as you can see. There are some duplicate code. Like: file_name and: re.findall(regex, self.extractingText.text_factuur_verdi[0]) So I try to combine this two methods in one method: def combine_methods(self, file_name): self.filter_verdi_total_number_fruit(file_name) self.filter_verdi_fruit_name(file_name) and then I try to call the combined method in the views.py: if uploadfile.image.path.endswith('.pdf'): content ='\n'.join(filter_text.combine_methods(uploadfile.image.path)) But then I get this error: can only join an iterable Exception Location: C:\Users\engel\Documents\NVWA\software\blockchainfruit\main\views.py, line 50, in post Raised during: main.views.ReadingFile Question: how can I change this? -
Unable to do exception handling in Django forms
I have made a ModelForm in which the user is supposed to enter date and time. I want to make sure that the time is in the correct format so I wrote convert_to_time function so that it can raise a ValidationError when the format of time is wrong but it is not working. I seem that the Exception part is not working. I mean the control is never going inside the Exception part. Here is the ModelForm import datetime class BookingForm(forms.ModelForm): class Meta: model = Booking fields = ['check_in_date', 'check_in_time', 'check_out_time', 'person', 'no_of_rooms'] def clean(self): cleaned_data = super().clean() normal_check_in = cleaned_data.get("check_in_time") str_check_in = str(normal_check_in) format = '%H:%M:%S' try: print("vukwqa") #This gets printed in the terminal. datetime.datetime.strptime(str_check_in, format).time() except Exception: print("srhni") #This does not get printed in the terminal. raise ValidationError( _('%(value)s Wrong time format entered.'), code='Wrong time format entered.', params={'value': str_check_in}, ) Is it not possible to do exception handling in forms.py because if it were a normal python program the control does go inside the exception part. Is there a different way to do it? Can someone please help? -
Django filtering in the filtered relate model
I HAVE THESE MODELS: # Create your models here. import datetime from django.conf import settings from django.db import models from django.db.models import Q from django.utils import timezone from django.contrib.auth.models import User # Create your models here. from datetime import date class Docs(models.Model): doc_id = models.CharField ('ID документа', max_length=200,null=True, unique=True) doc_title = models.CharField ('Название документа', max_length=200,null=True) hyp_name = models.URLField('Ссылка на документ') update_date = models.DateField (auto_now=True,verbose_name='Дата обновления документа') date_per = models.IntegerField('Срок действия прохождения') def __str__(self): return self.doc_id class Meta: verbose_name = 'Документ' verbose_name_plural = 'Документы' class Curicls(models.Model): curic_id = models.CharField ('ID курикулы', max_length=100, null=True,unique=True) cur_title = models.CharField ('Название курикулы', max_length=100, null=True) doc_id = models.ManyToManyField(Docs) def __str__(self): return self.curic_id class Meta: verbose_name = 'Курикула' verbose_name_plural = 'Курикулы' class Rolesl(models.Model): LEARN = 'A' CHECK = 'B' RANG_DIF = [ (CHECK, "ОЗНАКОМИТЬСЯ"), (LEARN, "ИЗУЧИТЬ") ] role_name = models.CharField('Название роли', max_length=100, unique=True) cur_id = models.ManyToManyField (Curicls, max_length=200, verbose_name='ID курикулы') rang = models.CharField(max_length=1, choices=RANG_DIF, default='', verbose_name='Ранг') def __str__(self): return self.role_name class Meta: verbose_name = 'Роль' verbose_name_plural = 'Роли' class Emp(models.Model): emp_name = models.OneToOneField(User, on_delete= models.CASCADE) per_no = models.IntegerField('Pers.No.') last_name = models.CharField('Last Name', max_length=50) first_name = models.CharField('First Name', max_length=50) emp_mail = models.EmailField('Email', max_length=100) roles_name = models.ManyToManyField(Rolesl) def __str__(self): return str(self.emp_name) class Meta: verbose_name = 'Работник' verbose_name_plural = 'Работники' … -
Event loop is closed while runing Celery tasks
I got the following RuntimeError when I run Celery worker, I'm new to Celery and I don't understand the source of this bug and why the task exception is not retrieved. My Celery tasks run but if I click on any trigger, the error shows up frequently and I ended up with another opertationalError that said: django.db.utils.OperationalError: database is locked [2022-10-25 13:22:58,428: ERROR/ForkPoolWorker-2] Task exception was never retrieved future: <Task finished name='Task-77' coro=<Connection.disconnect() done, defined at /home/.../lib/python3.8/site-packages/redis/asyncio/connection.py:819> exception=RuntimeError('Event loop is closed')> Traceback (most recent call last): File "/home/.../lib/python3.8/site-packages/redis/asyncio/connection.py", line 828, in disconnect self._writer.close() # type: ignore[union-attr] File "/usr/lib/python3.8/asyncio/streams.py", line 353, in close return self._transport.close() File "/usr/lib/python3.8/asyncio/selector_events.py", line 692, in close self._loop.call_soon(self._call_connection_lost, None) File "/usr/lib/python3.8/asyncio/base_events.py", line 719, in call_soon self._check_closed() File "/usr/lib/python3.8/asyncio/base_events.py", line 508, in _check_closed raise RuntimeError('Event loop is closed') -
Create editable spreadsheet-like table in Django site
I am making a planning site that uses a spreadsheet-like table where every day is a row and every school subject is a column. The table rows are all the days of a year and the columns are all the school subjects. The table cells have to be editable and stylable, so you can add and remove and highlight homework. Basicallly just like google spreadsheets. You have to be able to instantly edit the cell upon clicking on it. These edits have to be saved to a database, so that other users can follow along with the edits. But how can I save everything onedit to the database? How can I create this spreadsheet functionality in a Django website? I already tried html tables with contenteditable. Or should I use input forms? Could anyone provide with an example for this? -
Problems with Django Templatetag `regroup`
When using the regroup templatetag I encountered a problem where a Queryset would produce an empty list ([]) as a result. A similar Queryset (e.g. a Set with the same columns and filters however one filtervalue differed) worked perfectly fine. My Query looks like this: GraZeichen.objects.filter(gtid__id=gtid).values("id", "findno").exclude(findno=None, formid=0).annotate(values_id=GroupConcat( "formid", separator=',')).annotate(values_en=GroupConcat("fombez_en", separator=',')) and my Template looks like this: {% regroup zeichen|dictsort:'values_de' by values_en as zeichen_by_val %} {% for valgroup in zeichen_by_val %} {{ valgroup.grouper }} ({{ valgroup.list|length }}) <div> {% for zeichen in valgroup.list %} <div> <a href="alink"> <img ...> </a> </div> {% endfor %} </div> {% endfor %} As I said it works perfectly fine for some gtids, but if I compare the printed Queryset of gtids where it worked with the ones where it did not I can see no difference. What are the cases, where regroup would return an empty list?