Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
JSON Serialize for Following User
I have model.py as below. class User(AbstractUser): pass def serialize(self): return { "userid" : self.user.id, "username": self.username } def __str__(self): return f"{self.id} {self.username}" class Profile(models.Model): id = models.AutoField(primary_key=True) user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="user_profile") follower = models.ManyToManyField(User, blank=True, related_name="following") def __str__(self): return f"{self.user.username}" def serialize(self): return { "user_id": self.user.id, "user": self.user.username, "followers": self.follower.count(), "followers_usr": [user.username for user in self.follower.all()], "following": self.user.following.count(), "following_usr": [user.username for user in self.user.following.all()] } I would like to create a profile page showing user profile, follower and following count also list of users in follower and following. I'm using return JsonResponse in my views.py. Everything works well until "following_usr". I tried using "following_usr": self.user.following.all() and got this error: TypeError at /profile/1 Object of type QuerySet is not JSON serializable When I try "following_usr": [user.username for user in self.user.following.all()] got this error: AttributeError at /profile/1 'Profile' object has no attribute 'username' What is the proper way to do this? -
is it possible to use the azure blob storage as a postgresql database server in a django project?
I want to apply the PostgreSQL database in a azure blob storage In my Django Project. Because azure blob storage is cheaper than the SQL database on azure. Is technically possible ? -
Django Templates Not Rendering (Only Admin Page Shows)
Problem: My Django project displays only the default admin page when I run the server (python manage.py runserver). None of my custom HTML templates are rendering. What I've Tried: I've reset the URLs and views in all apps, including the main app. Expected Behavior: I expect to see my custom HTML pages displayed when the server starts. -
fastcgi_buffers vs. proxy_buffers when serving Django API using Daphne
I'm running two containers in a docker network (either through docker-compose or AWS ECS): Daphne for ASGI to Django Nginx for forwarding requests to Daphne and serving static files This is working fine. I can request API endpoints, use the Django admin site, load static files, etc. The problem is that some API responses are enormous (>10MiB), and while the endpoints returning these massive responses are very optimized and fast themselves, I'm getting timeouts due to buffering of the the response. I can tell by logs such as: [warn] 28#28: *252 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/1/00/0000000001 while reading upstream, client: 1.2.3.4, server: _, request: "GET /my-app/123/very-big-response/ HTTP/1.1", upstream: "http://172.17.0.2:9000/my-app/123/very-big-response/", host: "app.acme.com", referrer: "https://app.acme.com/" I have spent the past few hours reading about various nginx buffer settings, and while the docs explain fully what the options are and mean, I cannot find clear and reliable information on: Strategies for determining ballpark values for these parameters Which exact nginx directives to use To reiterate, I have two containers: #1 (daphne/django), and #2 (nginx). Container #1 (daphne/django) uses supervisord to run daphne. (Note before I continue: I'm fully aware of some other deviations from best practices here, like … -
Is this possible way to save the token in the existing table and use it for login logout change password forgot password also expirations works fine?
Should use the existing table for token to be stored and also for the reset end forget password token to be stored in the same table with expirations To implement a secure password change mechanism that involves storing JWT tokens in the database for verification, you need to modify the previous solution to save and validate the tokens from the database. Here's a step-by-step guide to implementing this: Step 1: Create a Model for Storing JWT Tokens Create a new model in your Django app to store JWT tokens. This model will include fields for the token, the user it’s associated with, and its expiration status. # models.py in your Django app from django.db import models from django.contrib.auth.models import User from django.utils import timezone class PasswordResetToken(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reset_tokens') token = models.CharField(max_length=255, unique=True) is_expired = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return f'Token for {self.user.username}' def has_expired(self): # Check if the token has expired (assuming 30 minutes expiry time) expiry_time = self.created_at + timezone.timedelta(minutes=30) return timezone.now() > expiry_time Step 2: Update the request_password_change View Modify the request_password_change view to generate a JWT token, save it in the database, and send it via email. # views.py in your Django … -
Why we need sync_to_async in Django?
The document said: The reason this is needed in Django is that many libraries, specifically database adapters, require that they are accessed in the same thread that they were created in. Also a lot of existing Django code assumes it all runs in the same thread, e.g. middleware adding things to a request for later use in views. But another question Is it safe that when Two asyncio tasks access the same awaitable object? said python's asyncio is thread safe. And as I know since the GIL still exist accessing one object from multiple thread should be thread safe. Can any one give a minimal example for why we have to use await sync_to_async(foo)() instead of directly foo() in django or other async apps? -
Django GeneratedField as ForeignKey with referential integrity
I'm trying to create a generated field that is also a foreign key to another table, while maintaining referential integrity at the database level. Basically, I'm trying to have the same effect as the following SQL, but in Django CREATE TABLE parent( id TEXT PRIMARY KEY ); CREATE TABLE child( id TEXT PRIMARY KEY, data JSONB, parent_id TEXT GENERATED ALWAYS AS (data->>'parent') STORED REFERENCES parent(id) ); I have successfully managed to create the generated field using Django 5.0 GeneratedField class Parent(models.Model): id = models.TextField(primary_key=True) class Child(models.Model): id = models.TextField(primary_key=True) data = models.JSONField() parnet_id = models.GeneratedField(expression=models.fields.json.KT('data__parent'), output_field=models.TextField(), db_persist=True) Now the problem is: how can I make this field also a foreign key? Because using ForeignKey would create a new column in the database that is not generated. I tried using ForeignObject, since it supports using an existing field as a foreign key, but the foreign key constraint was not created at the database level. class Parent(models.Model): id = models.TextField(primary_key=True) class Child(models.Model): id = models.TextField(primary_key=True) data = models.JSONField() parnet_id = models.GeneratedField(expression=models.fields.json.KT('data__parent'), output_field=models.TextField(), db_persist=True) parent = models.ForeignObject(Parent, from_fields=['parnet_id'], to_fields=['id'], on_delete=models.CASCADE) This generates the following SQL, which does not have a foreign key constraint CREATE TABLE "myapp_parent" ("id" text NOT NULL PRIMARY KEY); CREATE … -
Django ModuleNotFoundError: No module named 'anthropic'
I am trying to use the anthropic api within my django application. I am able to access and make requests to the API within a normal python file, but when I try doing the same within my django app (specifically my views.py file), it does not recognize anthropic as a module I have installed. Was wondering if anyone can help with this. I have tried including anthropic within my settings.py file but this didn't resolve the error. The anthropic docs for python sdk: https://github.com/anthropics/anthropic-sdk-python -
Django Prefetch Related Still Queries
I have the function def selected_location_equipment(self): qs = (Equipment.objects .prefetch_related('service_logs') .all()) return qs That returns a queryset with a few related fields grabbed. The problem is that when i access the prefetched data later in my code, it executes a query again. Ive stepped through the Django code and can see where it is checking the cache for the .all() in one spot and doesnt query, but then when its called here, it's almost like the cache is cleared. Debug Toolbar shows a query for each iteration of the loop as well. for e in equipments: last_service = list(e.service_logs.all())[-1] ... Here's the basic model definition for Equipment class ServiceLog(models.Model): equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE, related_name='service_logs') -
Error: Failed to Clone MapStore2 Submodule in GeoNode Docker Build
Question: I'm trying to build a GeoNode instance using Docker, but I'm encountering an error related to cloning the MapStore2 submodule from the geonode-mapstore-client repository. Below is the error output I'm receiving: bash => [django 12/16] RUN yes w | pip install --src /usr/src -r requirements.txt 225.0s => => # fatal: fetch-pack: invalid index-pack output => => # fatal: clone of 'https://github.com/geosolutions-it/MapStore2.git' into submodule path '/usr/src/django-geonode-mapstore-client/geonode_mapstore_client/client/MapStore2' failed => => # Failed to clone 'geonode_mapstore_client/client/MapStore2'. Retry scheduled Full Error Output: => ERROR [django 12/16] RUN yes w | pip install --src /usr/src -r requirements.txt && yes w | pip install -e . ... 202.5 error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8) ... 319.8 note: This error originates from a subprocess, and is likely not a problem with pip. Dockerfile Snippet: dockerfile Copier le code FROM geonode/geonode-base:4.1.0-ubuntu-22.04 LABEL GeoNode development team # Copy local GeoNode src inside container COPY /src/. /usr/src/geonode/ WORKDIR /usr/src/geonode # Configurer Git pour augmenter la mémoire tampon et gérer les faibles vitesses de téléchargement RUN git config --global http.postBuffer 524288000 && \ git config --global http.lowSpeedLimit 0 && \ git config --global http.lowSpeedTime 999999 RUN yes w | pip install … -
Wagtail: How to validate page model relations before saving
I have a page type where I want to define the title (and slug) automatically, based on some other fields and higher level model relations. To achieve that, I override the full_clean() method of my page class. The only thing that can go wrong is that the new page gets a slug that is already in use among the sibling pages. That is intended, only pages with unique field combinations that will influence the slug should exist. So, if a user tries to save a page with a duplicate combination of data fields, I want to display a nice and readable ValidationError. I understand that the full_clean() method is called multiple during editing/saving pages, following a kind of hierarchical approach, where the cleaning procedure starts with basic stuff and goes up to model relations. It seems that ValidationErrors are only caught in the UI and displayed nicely when they are not raised in the presumably last call of full_clean(), right after hitting the save button. When I raise a ValidationError when I have all the information at hand, it's not caught and the traceback is shown. Is there any way to handle a ValidationError gracefully if I only can raise … -
Django - Multiple images upload and delete
I'm looking at improving my code that allows the user to upload multiple images (files) linked to a record, and also delete them as needed. I am able to handle the first part (multiple images) with the following. models.py class APIvisit(ModelIsDeletable, SafeDeleteModel): _safedelete_policy = SOFT_DELETE created_date = models.DateTimeField(auto_now_add=True,editable=False, verbose_name=_("Créé le")) modified_date = models.DateTimeField(auto_now=True,editable=False, verbose_name=u"Modifié le") visitdate = models.DateField(_('Date de la visite'),default=datetime.date.today) [...] a lot of other fields class Meta: ordering = ('visitdate',) class APIvisitimage(ModelIsDeletable, SafeDeleteModel): _safedelete_policy = SOFT_DELETE created_date = models.DateTimeField(auto_now_add=True,editable=False, verbose_name=_("Créé le")) modified_date = models.DateTimeField(auto_now=True,editable=False, verbose_name=u"Modifié le") fk_visit = models.ForeignKey(APIvisit, on_delete=models.PROTECT, related_name=_('ImageVisite'), verbose_name=_('ImageVisite'), blank=False, null=False) image = models.FileField(upload_to="uploads/%Y/%m/%d/") forms.py from django.forms.widgets import ClearableFileInput class MultipleFileInput(forms.ClearableFileInput): allow_multiple_selected = True class MultipleFileField(forms.FileField): def __init__(self, *args, **kwargs): kwargs.setdefault("widget", MultipleFileInput()) super().__init__(*args, **kwargs) def clean(self, data, initial=None): single_file_clean = super().clean if isinstance(data, (list, tuple)): result = [single_file_clean(d, initial) for d in data] else: result = single_file_clean(data, initial) return result [...] class VisitImageForm(ModelForm): class Meta: model = APIvisitimage fields = ["image"] image = MultipleFileField(label='Choisir les photos', required=False) views.py class VisitEditView(PermissionRequiredMixin, UpdateView): permission_required = 'gestion.change_apivisit' model = APIvisit form_class = VisitForm template_name = 'visite/edition.html' success_url = '/visite/' def form_invalid(self, form): self.object_list = self.get_queryset() context = self.get_context_data(task_form=form) return self.render_to_response(context) def post(self, request, *args, **kwargs): self.object = self.get_object() … -
Django reusable Many-to-one definition in reverse
I'm struggling to make a Many-to-one relationship reusable. Simplified, let's say I have: class Car(models.Model): ... class Wheel(models.Model): car = models.ForeignKey(Car) ... Pretty straight forward. What, however, if I'd like to use my Wheel model also on another model, Bike? Can I define the relationship in reverse, on the "One" side of the relationship? Defining this as a Many-to-many on the Vehicles would mean the same Wheel could belong to multiple Vehicles, which is not what I want. Would I have to subclass my Wheel to CarWheel and BikeWheel only to be able to differentiate the Foreignkey for each relationship? Seems like there should be a cleaner solution. -
Django Multi-Database Setup: Error when Saving to Database
I am working on a Django project where I am using multiple databases, and I need to fetch data from a read-only MySQL database. I am using the using() method in Django to query data from this read-only database. However, when I attempt to save the queryset, I encounter the following error: The MySQL server is running with the --read-only option so it cannot execute this statement I have reviewed the Django documentation on multiple databases (link below), which suggests that specifying the using() method should allow for reading from and writing to different databases, but in this case, I'm only trying to read from the read-only database and save to another. Django documentation: https://docs.djangoproject.com/en/3.1/topics/db/multi-db/#:~:text=If%20you%20don%E2%80%99t%20specify%20using%2C%20the%20save Am I missing something here, or is there an additional configuration needed when dealing with a read-only MySQL database in Django? -
getting error while handling SSE in django
I have created a notification master in models.py then in signals.py i created event for post ssave and delete of notification while event is triggered in views.py iam creatin sse_view whose url is getting access infrontend. this url is raising the issue "Application instance <Task pending name='Task-21' coro=<ASGIStaticFilesHandler._call_() running at C:\Users\nihar\envs\backendenvt\Lib\site-packages\django\contrib\staticfiles\handlers.py:101> wait_for=<Future pending cb=[shield.<locals>._outer_done_callback() at C:\Users\nihar\AppData\Local\Programs\Python\Python311\Lib\asyncio\tasks.py:898, Task.task_wakeup()]>> for connection <WebRequest at 0x29eef9ae8d0 method=GET " i installed daphne, twisted, whitenoise in settings.py - INSTALLED_APPS = ['daphne'] MIDDLEWARE = ['whitenoise.middleware.WhiteNoiseMiddleware'] ASGI_APPLICATION = 'BACKEND.asgi.application' 3.view.py code- async def update_event_notifications_sse_view(request): mail_id = request.GET.get('mail_id') permission = AsyncNotificationAccessPermission() token = request.GET.get('token') if not await sync_to_async(is_valid_token)(token, mail_id): raise PermissionDenied("You do not have permission to access this resource.") if not await permission.has_permission(request, view=None): raise PermissionDenied("You do not have permission to access this resource.") async def event_stream(): yield f"data: {json.dumps(await notifications_list(mail_id))}\n\n" while True: event_occurred = await sync_to_async(update_event_notification.wait)() if event_occurred: try: yield f"data: {json.dumps(await notifications_list(mail_id))}\n\n" await sync_to_async(update_event_notification.clear)() # Clear the event flag except Exception as e: print(f"Error in event stream: {e}") break await asyncio.sleep(60) response = StreamingHttpResponse(event_stream(), content_type='text/event-stream') response['Cache-Control'] = 'no-cache' return response async def notifications_list(mail_id): if mail_id is not None: # Fetch notifications using sync_to_async queryset = await sync_to_async(lambda: Notifications.objects.filter(ToUser__MailID=mail_id).order_by('-CreatedAt'))() serialized_data = await sync_to_async(lambda: NotificationSerializer(queryset, many=True).data)() return serialized_data -
Getting output from joined tables with Django
I have the following example tables in MySQL DB being interacted with Django class Env(models.Model): name = models.CharField() class ExecutionDetail(models.Model): executionid= models.ForeignKey(Execution) job= models.CharField() class Execution(models.Model): name= models.ForeignKey(Env) executionid= models.CharField() envname= models.ForeignKey(Env) I select data using views def v_job_history(request, job): logger.debug("Calling history jobs") jobname=job myjobs = ExecutionDetail.objects.filter(job=job) template = loader.get_template('history_jobs.html') context = { 'myjobs': myjobs, } return HttpResponse(template.render(context, request)) Then in my HTML I try and display my data, e.g. {% for x in myjobs %} <tr> <td>{{ x.execution.envname}} </a></td> <td>{{ x.execution.name }} </a></td> <td>{{ x.job}}</td> </tr> {% endfor %} The problem is x.execution.env_name will return Environment object (2) etc. I have tried x.execution.env_name - returns objects. x.env.name, x.execution.env.name which return nothing. -
Django max_length validation for BinaryField causes KeyError in translation __init__.py
I have a simple model, something like class Notenbild(models.Model): bild_data = models.BinaryField(max_length=500000, editable=True) In admin.py class BinaryFieldWithUpload(forms.FileField): def __init__(self, *, max_length=None, allow_empty_file=False, **kwargs): super().__init__(max_length=max_length, allow_empty_file=allow_empty_file, **kwargs) def to_python(self, data): data = super().to_python(data) if data: image = Image.open(data) # some more processing with the image which I omitted here byte_array = io.BytesIO() image.save(byte_array, format='PNG') return byte_array.getvalue() return None def widget_attrs(self, widget): attrs = super().widget_attrs(widget) if isinstance(widget, FileInput) and "accept" not in widget.attrs: attrs.setdefault("accept", "image/*") return attrs @admin.register(Notenbild) class NotenbildAdmin(admin.ModelAdmin): fields = [ 'bild_data', 'vorschau', ] readonly_fields = ['vorschau'] formfield_overrides = { models.BinaryField: {'form_class': BinaryFieldWithUpload}, } @admin.display(description='Bild (Vorschau)') def vorschau(self, notenbild: Notenbild): encoded_image = base64.b64encode(notenbild.bild_data).decode('utf-8') return format_html( f'<p>{len(notenbild.bild_data)} bytes</p>' f'<img src="data:image/png;base64,{encoded_image}" style="max-width:40rem; max-height:16rem" />' ) which works fine for saving images through the admin interface, which fit the size limits. However, when trying to save a file which exceeds the size limit, I get a very strange error: KeyError at /admin/library/notenbild/368/change/ "Your dictionary lacks key 'max'. Please provide it, because it is required to determine whether string is singular or plural." Django Version: 5.1.1 Exception Location: /Users/alex/Repositories/ekd-cms/venv/lib/python3.12/site-packages/django/utils/translation/__init__.py, line 130, in _get_number_value which doesn't make any sense to me. The validation correctly caught the invalid data, but seems like this might be a … -
Annotating a Django QuerySet with the count of a Subquery
I'm building a job board. Each Job could have several associated Location objects. I have designed my Location and Job models as follows: class Location(BaseModel): slug = models.CharField(unique=True) city = models.OneToOneField(to="City", null=True) state = models.ForeignKey(to="State", null=True) country = models.ForeignKey(to="Country") class Job(BaseModel): title = models.CharField() description = models.TextField() locations = models.ManyToManyField( to="Location", related_name="jobs", through="JobLocation", ) So, a Location object has the flexibility to refer to our idea of a country (like United States), a state (like New York) or a city (like Manhattan). I populate the slug field of Location model like so: If Location object is a country, I use the country name. united-states If Location object is a state, I use the (state name + country name). new-york-united-states If Location object is a city, I use (city name + state name + country name). manhattan-new-york-united-states With slug field populated in this manner, I can simplify querying all the jobs in a particular location using the endswith lookup. For example, if there's a Python job in Manhattan, NY and a React job in Brooklyn, NY, I can get all the jobs in the state of New York like so: Job.objects.filter(locations__slug__endswith="new-york-united-states").distinct() Now, I would like to get a list of all … -
Why does my content-security-profile (django-csp) not work properly for a view/template loaded in a bootstrap modal? Works fine otherwise
I didn't include the template code because it is irrelevant. This is the script tag in the template: <script nonce="{{ CSP_NONCE }}" src="{% static 'js/mmImport.js' %}" defer data-mmimporturl="{% url 'mmImport' %}"> </script> Settings.py MIDDLEWARE = [ 'csp.middleware.CSPMiddleware' ....] # Content Security Policy', CSP_DEFAULT_SRC = ("'self'") CSP_IMG_SRC = ("'self'") CSP_STYLE_SRC = ("'self'") CSP_SCRIPT_SRC = ("'self'") CSP_INCLUDE_NONCE_IN = ('script-src')` So two scenarios... I load this view/template in a modal that is in the homepage. If I include 'unsafe-inline, no issues. It works. Form/view/template behaves normally. Without unsafe-inline and just the above policies, it gives the following error: [Error] Refused to execute a script because its hash, its nonce, or 'unsafe-inline' does not appear in the script-src directive of the Content Security Policy. (mmHomepage, line 0) I load the view as its own page/template; not a modal. Straight forward Django template. With CSP policies as above, the page works normally. No errors. I suspect it is the way a view/template is handled by bootstrap modals. Not sure where to look. I am new to Django-csp so not familiar with this. Just started familiarizing myself with the spec. I also tried bringing this js code into the template, so not calling a separate file. … -
Django Project "failed to install application dependencies" On Elastic Beanstalk
My coworker and I are attempting to upload a website we have been working on to AWS Elastic Beanstalk. We have tried using both console & command line techniques to create the instance and in both cases it ends with the 4 errors: Instance deployment failed to install application dependencies. The deployment failed. Instance deployment failed. For details, see 'eb-engine.log'. [Instance: i-0deb02382df3b0fd9] Command failed on instance. Return code: 1 Output: Engine execution has encountered an error.. Create environment operation is complete, but with errors. For more information, see troubleshooting documentation. Neither of us have experience with working with AWS before, and we spent several hours attempting to find solutions to this and nothing has worked. Our Django project has a Requirements.txt file, and a .ebextensions folder with a django.config file inside. enter image description here Contents of the django.config file: option_settings: aws:elasticbeanstalk:container:python: WSGIPath: GMIFY.wsgi:application Contents of the requirements.txt file: (made by doing pip freeze > requirements.txt) asgiref==3.7.2 awsebcli==3.21.0 botocore==1.35.34 cement==2.10.14 certifi==2024.8.30 charset-normalizer==3.3.2 colorama==0.4.6 Django==5.0.3 django-extensions==3.2.3 idna==3.10 jmespath==1.0.1 numpy==2.0.0 pandas==2.2.2 pathspec==0.10.1 psycopg2-binary==2.9.9 pypiwin32==223 python-dateutil==2.9.0.post0 pytz==2024.1 pywin32==307 PyYAML==6.0.2 requests==2.32.3 semantic-version==2.10.0 setuptools==75.1.0 six==1.16.0 sqlparse==0.4.4 termcolor==2.5.0 tzdata==2024.1 urllib3==1.26.20 wcwidth==0.2.13 whitenoise==6.6.0 We have tried accessing the logs, but there was no clear line(s) indicating what … -
How to get data from another model in DetailView
I'm trying to get objects from two models using DetailView. First, I set urls.py: from django.urls import path from . views import RetiroListView, RetiroDetailView from . import views urlpatterns = [ path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'), path('post/', views.post, name='post-post'), ] Here are my models: from django.db import models from django.utils import timezone from django.contrib.auth.models import User # Create your models here. class Post(models.Model): title = models.CharField(max_length=100) content = models.TextField() date_posted = models.DateTimeField(default=timezone.now) author = models.ForeignKey(User, on_delete=models.CASCADE) class Comment(models.Model): post = models.OneToOneField(Post, on_delete=models.CASCADE, primary_key=True) comment=models.CharField(max_length=100) class Tags(models.Model): post = models.OneToOneField(Post, on_delete=models.CASCADE, primary_key=True) comment=models.CharField(max_length=100) and the detail view: class PostDetailView(DetailView): model = Post Now, in my template, I can use: {{ object.title }} To access my post information but, I need to get some info from the other tables, in this view. I know I have to overwrite the get_context_data method, but I don't know how. -
Django Rest Framework TokenAuthentication not working "Invalid Token"
I'm successfully creating a token upon a user's login (using a CustomUser model that replaces the username field with email), but when using this token in subsequent requests, the response is "Invalid token". Below are excerpts from the relevant files: settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', 'goals', 'rest_framework', 'rest_framework.authtoken', ] ... REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.TokenAuthentication', ], } Views to register and login users in users/views.py class UserRegistrationView(generics.CreateAPIView): serializer_class = UserSerializer permission_classes = [permissions.AllowAny] class UserLoginView(APIView): def post(self, request): user = authenticate(username=request.data['email'], password=request.data['password']) if user: token, created = Token.objects.get_or_create(user=user) return Response({'token': token.key}) else: return Response({'error': 'Invalid credentials'}, status=401) The view I'm testing in goals/views.py: @api_view(['POST']) @permission_classes((IsAuthenticated, )) def create_topic(request): data = JSONParser().parse(request) serializer = TopicSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) else: return Response(serializer.errors, status=400) The test that fails in goals/tests.py class SerializerTests(TestCase): def setUp(self): email = "normal@user.com" pw = "foo" self.user = CustomUser.objects.create_user(email=email, password=pw) self.user_data = {'email':email, 'password':pw} login_response = self.client.post('/users/login/', data=json.dumps(self.user_data), content_type="application/json") print(login_response.data) self.token = login_response.data['token'] self.headers = {'Authorization': 'Token {self.token}'} def test_create_topic(self): self.topic = {'name':'topic_1', 'created_by':self.user.pk} response = self.client.post('/goals/topic/create/', data=json.dumps(self.topic), content_type="application/json", headers=self.headers) #should this be replaced by some DRF components? self.assertEqual(201, response.status_code) Unfortunately response.data simply states {'detail': ErrorDetail(string='Invalid token.', code='authentication_failed')}. This … -
Django custom function taking a long time to run for the view
I have a function below that generates a navigation so it can be using in the view. It is pulling in the articles, article category, and article subcategory. The main issue is that with about 15 category and 46 subcategory and 33 articles it is taking like 7-10 seconds to finish loading this function. What is the best approach for handling this so that it loads faster? def generate_navigation_list(request): articles = Article.objects.all() article_category = ArticleCategory.objects.all() articles_sub_category = ArticleSubCategory.objects.all() # Create a navigation list structure navigation_list = [] # Iterate over categories for category in article_category: category_dict = { 'id': category.id, 'title': category.name, 'subcategory': [] } # Filter subcategories for the current category filtered_subcategories = articles_sub_category.filter(category=category) # Iterate over filtered subcategories for subcategory in filtered_subcategories: subcategory_dict = { 'id': subcategory.id, 'title': subcategory.name, 'articles': [] } # Filter articles for the current subcategory filtered_articles = articles.filter(sub_category=subcategory).order_by('order') # Add articles to the subcategory dictionary if there are articles if filtered_articles.exists(): # Check if there are any articles for article in filtered_articles: article_dict = { 'title': article.title, 'slug': article.slug } subcategory_dict['articles'].append(article_dict) # Append subcategory dictionary to category dictionary category_dict['subcategory'].append(subcategory_dict) # Append category dictionary to navigation list if it has subcategories with articles if category_dict['subcategory']: … -
xhtml2pdf position header and content
how to position top content after a header, if header height is auto depending the content of header. If the top position of the content is not automatic, it will overlap if there is a change in size. style.css @page { size: a4 portrait; @frame header_frame { -pdf-frame-content: header_content; left: 2cm; width: 17cm; top: 1.5cm; height: auto; #keep changing } @frame content_frame { left: 2cm; width: 17cm; top: 10cm; #here height: 14cm; } } <div id="header_content"> header content height is changing depends on header content </div> <div id="content_content"> content of pdf </div> i have try to change top position to auto but cant change -
Django Admin Grappelli Adding Custom Navigation in Header
I want to be able to remove the whole header which includes the grp-navigation and grp-context-navigation found in the inspect element and add a custom navigation bar. This custom navigation bar is being used on my non-admin front facing application. I am using Django 4.2.7 and Grappelli 3.0.8 . Whats the best approach?