Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Wagtail CMS slugurl and pageurl not working - link returns 'None'
I am building my third Wagtail CMS site (v.1.8) but have never been able to figure out the slugurl & pageurl tags to create internal links and have just hardcoded all the links even though hardcoding goes against the grain in my workflow. Can someone please help me out. and yes, I did add {% load wagtailcore_tags %} to my page. For demonstration purposes, I am going to use a very simple model that has is going to give my client a template with just a title and a body as one of their page templates. The page is aptly called StandardPage(Page) and a template named standard_page.html. I created an about page and want to provide a link to the page in my header and footer. I know I can hardcode it; but that is not what I would to do. I have tried {% slugurl page.about %} but link returns None - I even tried {% slugurl standardpage.about %} thinking I was missing something but of course it returns None as well. I always used named routes in all my Django projects and not being able to figure this out is driving me crazy. Here is my model for … -
Optimize Django page load
I have troubles with optimizing my page. I've created a minimal example with just one table and it takes about 2 seconds to load (according to django-debug-toolbar and 2196 queries. There are three main models - Product, Occurence and Price. Could you help me with optimizing? There is 2196 Queries in this simple example. For example stats_get_avg_price has to load all Scan objects from the database, but then, another row last_scan_price is executing query again to get the last scan. It should be enough to load all scans just one time but how? Even better would be to load all Scans related to Product so there would be only one database lookup. I've tried to do all filtering inside the DB but there are methods in the model Occurence which I can't prefetch or preload. VIEW def fake_product(request,id): product = get_object_or_404(mainapp_models.Product, id=id) occ_table = OccurencesTable(product.occurences.order_by("site__name")) RequestConfig(request).configure(occ_table) context = {'table' : occ_table} return render(request, 'main_app/dashboard/product_fake.html', context=context) TEMPLATE {% extends "main_app/dashboard/base.html" %} {% load django_tables2 %} {% block content %} {% render_table table %} {% endblock %} TABLE class OccurencesTable(tables.Table): site = tables.columns.TemplateColumn("""<a href="{{ record.url }}">{{ record.site.name }}</a>""",accessor='site.name', verbose_name=u'Site') avg_price = tables.columns.TemplateColumn("""{{ record.stats_get_avg_price.pref_currency_string }}""",accessor='stats_get_avg_price.price', verbose_name='AVG price') last_scan_price = tables.columns.TemplateColumn("""{{ record.get_last_scan.price.pref_currency_string }} """,accessor='get_last_scan.price.amount', … -
Page loading is very slow - how to properly optimize?
I'm working on a big page with different product statistics. I use there multiple charts, tables etc. It takes for example 5 seconds to load the page. This is a very simplified models.py PRODUCT class Product(models.Model): user = models.ForeignKey(User, null=False, blank=False, related_name='products') name = models.CharField(max_length=200) def get_occurences(self): return self.occurences.prefetch_related("scans__price__currency") def get_occurences_count(self): return self.occurences.all().count() def get_all_scans(self): return [item for sublist in [x.scans.all() for x in self.get_occurences()] for item in sublist] def get_all_valid_scans(self): return [item for sublist in [x.get_valid_scans() for x in self.get_occurences()] for item in sublist] def get_last_scans(self): scans = [] for occ in self.get_occurences(): scan = occ.get_last_scan() if scan: scans.append(scan) return scans @property def last_scan_time(self): scans = sorted([x for x in self.get_last_scans() if x.datetime],key = lambda x: x.datetime, reverse=True) Scan.objects.filter(occurence__product=self,price__amount__isnull=False) return str(scans[0].datetime) def get_last_min_scan(self): sorted_last_scans = [x for x in self.get_last_scans() if x.valid] sorted_last_scans.sort(key=lambda x: x.price.eur_price) return sorted_last_scans[0] if sorted_last_scans else None def get_last_min_price(self): last_scan = self.get_last_min_scan() if last_scan: return last_scan.price.eur_price return None def get_active_occurences(self): return self.get_occurences().filter(active=True) OCCURENCE class Occurence(models.Model): product = models.ForeignKey(Product, related_name='occurences', on_delete=models.CASCADE) _last_scan = models.OneToOneField('Scan',null=True,blank=True,related_name='+') currency = models.ForeignKey('Currency',related_name='occurences') def get_last_scan(self): try: last = self.scans.select_related("price__amount").order_by('datetime').last() except: last = None return last def get_last_valid_scan(self): try: last = self.scans.exclude(price__isnull=True).order_by('-datetime').first() except: last = None return last def get_second_last_valid_scan(self): scans = self.scans.exclude(price__isnull=True).order_by('-datetime').select_related("price") … -
Django QuerySet displays improper string
I have a problem when I'm rendering a ManyToMany field in my view. #Models: from django.db import models class Ingredient(models.Model): content = models.CharField(max_length=300, default='') def __str__(self): return self.content class Drink(models.Model): ingredient = models.ManyToManyField(Ingredient) title = models.CharField(max_length=300) description = models.TextField(max_length=500) def __str__(self): return self.title #View: from django.shortcuts import render from .models import Drink, Ingredient def index(request): drinks = Drink.objects.all() context = {'drinks': drinks} return render(request, 'drinks/index.html', context) #Template: {% for i in drinks %} <h3>{{ i.title }}</h3> <p><b>Description:</b> <br>{{ i.description }}</p> <p>{{ i.ingredient.all }}</p> {% endfor %} Here is my web-site: Screw driver Description: Good drink made of pure vodka and California cultured oranges. <QuerySet [<Ingredient: Vodka>, <Ingredient: Orange juice>]> I cannot find any proper way to filter or format the last string. Is there anyone who has an idea? -
Container Port Not Exposed in Kubernetes
I'm trying to connect a Postgres pod with my Django app pod but somehow my Django app pod port is not exposed. In my app's Dockerfile I added EXPOSE 8000, however when I ssh into my pod I cannot serve my app (returns an error asking if a postgres server is up and running on port 5432 and accepting TCP/IP connections on port 5432) and when run nmap I get this root@app-kbqd8:~/app# nmap 12.3.4.567 Starting Nmap 7.01 ( https://nmap.org ) at 2017-01-06 15:24 UTC Nmap scan report for postgres-service.default.svc.cluster.local (12.3.4.567) Host is up (0.00063s latency). All 1000 scanned ports on postgres-service.default.svc.cluster.local (10.0.0.171) are filtered My app's service is: --- kind: Service apiVersion: v1 metadata: name: app-service spec: selector: name: myapp ports: - port: 80 targetPort: 8000 protocol: TCP For my postgres pod --- kind: Deployment apiVersion: extensions/v1beta1 containers: - name: postgres image: user/postgres imagePullPolicy: IfNotPresent ports: - containerPort: 5432 and its service --- kind: Service apiVersion: v1 metadata: name: postgres-service spec: ports: - port: 5432 targetPort: 5432 protocol: TCP selector: component: postgres role: master name: postgres -
Customize related objects for many-to-many field in Django
There are games entity, each of them could have 1 or more platforms. Also each game could have 1 or more links to related games (with their own platforms). Here it looks like in models.py: class Game(TimeStampedModel): gid = models.CharField(max_length=38, blank=True, null=True) name = models.CharField(max_length=512) platforms = models.ManyToManyField( Platform, blank=True, null=True) ... #here is the self-referencing m2m field related_games = models.ManyToManyField( "self", related_name="related", blank=True) And this model are served with this code in admin.py: @admin.register(Game) class GameAdmin(AdminImageMixin, reversion.VersionAdmin): list_display = ("created", "name", "get_platforms"... ) list_filter = ("platforms", "year",) #I'm interested in changing the field below filter_horizontal = ("related_games",) formfield_overrides = { models.ManyToManyField: {"widget": CheckboxSelectMultiple}, } def get_platforms(self, obj): return ", ".join([p.name for p in obj.platforms.all()]) I need to extend filter_horizontal = ("related_games",) part of admin.py - to add a platform information of each game in related games widget. It should look like (game name and platforms list): "Virtual Fighter (PS4, PSP, PS3)". The application uses Django 1.7 and Python 2.7 Thank you for your attention. -
Django - Event Sourcing - State of the world
Trying to migrate my mindset to "Event Sourcing" in Django, taking this for inspiration: https://www.youtube.com/watch?v=e2ikLB1Ka3Q The author states that this is not an exact implementation of Martin Fowler's principles: https://martinfowler.com/eaaDev/EventSourcing.html However, it's quite interesting. If I use the database to store the events, how can I handle (persist/query) the state of the world? Other database? Cache? Memory? -
Using django with pm2
I have just changed from express(nodejs) to django(python) because i started hating the async part. But the only thing I miss about nodejs was the wonderful process manager pm2 and I was wondering if I could use pm2 together with django or flask. I've already looked on google for an example or a solution but without any results. Is it possible to use them together and if so, how? (I am on ubuntu) -
Django query for mutliple time aggregations
I have a table of: Location | date | value (X) I wish to produce summaries of a value (X) for a data set by day, week, month and year, for multiple locations, using the Django ORM, with a single query. e.g: Location | Mon | ... | Fri | Week Avg. | Month Avg. | Year Avg. Location1 | 0.5 | ... | 0.8 | 0.7 | 1.1 | 1.5 Location2 | .... Currently, I have to do this by multiple queries, one over days of the week, and then one each for the aggregations over the week, month and year. Is there a smart way to do this using a single query (without returning the year's full data from the database)? Thanks -
Django 1.10: When rendering a ModelForm after update, ImageFields are missing when they're changed
# models.py class Profile(models.Model): name = models.CharField(max_length=255) image = models.ImageField(upload_to=image_path, blank=True, null=True) # forms.py class ProfileForm(ModelForm): class Meta: model = Profile fields = '__all__' # views.py def profile(request, id): p = get_object_or_404(Profile, pk=id) profile_form = ProfileForm(request.POST or None, files=request.FILES or None, instance=q) if request.method == 'POST': if profile_form.is_valid(): profile_form.save() return render(request, 'profile.html', {'form': profile_form}) #profile.html <form method="post" action="" enctype="multipart/form-data" > {% csrf_token %} {{ form }} <input type="submit" value="Submit" /> </form> If a user GETs /profile/1 and 1 exists, they get the form prefilled with all the values, including the image (if any) If the user then POSTs updates to any of the fields except the image (clearing or changing), The page renders correctly after the update. However, if the user changes the Image (clear or change), the change is not reflected in the rendered result: The image is always shown as empty. a quick GET solves the situation and displays the changed situation. Does anyone know why the image field doesn't show the update in this situation? -
How to use a tsvector field to perform ranking in Django with postgresql full-text search?
I need perform a ranking query using postgresql full-text search feature and Django with django.contrib.postgres module. According to the doc, it is quite easy to do this using the SearchRank class by doing the following: >>> from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector >>> vector = SearchVector('body_text') >>> query = SearchQuery('cheese') >>> Entry.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank') This probably works well but this is not exactly what I want since I have a field in my table which already contains tsvectorized data that I would like to use (instead of recomputing tsvector at each search query). Unforunately, I can't figure out how to provide this tsvector field to the SearchRank class instead of a SearchVector object on a raw data field. Is anyone able to indicate how to deal with this? -
How to write a Pandas Dataframe to existing Django model
I am trying to insert data in a Pandas DataFrame into an existing Django model, Agency, that uses a SQLite backend. However, following the answers on How to write a Pandas Dataframe to Django model and Saving a Pandas DataFrame to a Django Model leads to the whole SQLite table being replaced and breaking the Django code. Specifically, it is the Django auto-generated id primary key column that is replaced by index that causes the errors when rendering templates (no such column: agency.id). Here is the code and the result of using Pandas to_sql on the SQLite table, agency. In models.py: class Agency(models.Model): name = models.CharField(max_length=128) In myapp/management/commands/populate.py: class Command(BaseCommand): def handle(self, *args, **options): # Open ModelConnection from django.conf import settings database_name = settings.DATABASES['default']['NAME'] database_url = 'sqlite:///{}'.format(database_name) engine = create_engine(database_url, echo=False) # Insert data data agencies = pd.DataFrame({"name": ["Agency 1", "Agency 2", "Agency 3"]}) agencies.to_sql("agency", con=engine, if_exists="replace") Calling 'python manage.py populate' successfully adds the three agencies into the table: index name 0 Agency 1 1 Agency 2 2 Agency 3 However, doing so has changed the DDL of the table from: CREATE TABLE "agency" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(128) NOT NULL) to: CREATE TABLE data_explorer_agency ( … -
How could l better fit these thumbnails to the unused vertical space on this Bootstrap gallery layout?
I am hoping to find a way to use a single for-loop to populate a variable number of variable-height thumbnails onto a gallery page. This is how my Django/Bootstrap template looks so far: <div class="row"> <!-- List of uploaded documents --> {% if documents %} {% for document in documents %} <div class="col-sm-6 col-md-4" > <div class="thumbnail" > <div class="row"> <div class="col-sm-12 col-md-12"> <div class="text-center">{{ document.title.rendered|title }}</div> </div> <div class="col-sm-12 col-md-12"> <a href="{{ document.docfile.url }}"> <img style="max-height: 150px" src="{{ document.docfile.url }}" alt="{{ document.docfile.name }}"> </a> {{ document.desc.rendered }} </div> </div> <hr> </div> </div> {% endfor %} {% else %} <p>No documents.</p> {% endif %} </div> My trouble is that bootstrap's row and column system keep things evenly stacked which creates many patches of unused screen and I am not certain how to adjust it. This is the actual screenshot: This is what I was hoping to achieve: Thank your for any help. -
Access google analytics data with Django
I'm trying to build a super simple dashboard to show to users their Google Analytics data well formatted. I'm using oAuth2Client and Django 1.10.4 and Python 3.5. I've followed the example within the documentation and now I have a very simple app, the landing page will ask you to login, you click on a link to authorise, the Google page loads and asks you if you want to share your GA data and if you accept you are redirect to a page you can see only if you are logged in. All good so far. However I can't manage to actually get users data, what's the best way to get for example the list of properties in a user's account or even better the number of page views a property had in the last week? This is my code so far: /pools/models.py from django import http from oauth2client.contrib.django_util import decorators from django.views.generic import ListView # from polls.models import GoogleAnalytic from django.http import HttpResponse # Google Analytics from apiclient.discovery import build # def index(request): # return http.HttpResponse("Hello and Welcome! </br> </br> Click <a href='/profile_enabled'> here</a> to login") @decorators.oauth_required def get_profile_required(request): resp, content = request.oauth.http.request( 'https://www.googleapis.com/plus/v1/people/me') return http.HttpResponse(content) @decorators.oauth_enabled def get_profile_optional(request): if … -
How to Change video resolution in python
I am developing a Video sharing Website using Django, how to change the video quality of uploaded video -
PyCharm - Can't create App Engine Application using DJango
The last few days I tried to create an App Engine Application based on DJango using PyCharm Professional 2016.3.2.. But everytime I try to create a new App Engine Application using Django as an third-party library PyCharm throws an Error and tells 'Create App Engine Application - Incorrect or corrupted App Engine SKD: cannot finde DJango installation'. I already tried to install everything again, including Python, PyCharm, Django and the SDK, but it still doesn't work. I would really appreciate any kind of help, because this problem drives me crazy. -
Why is setting a django FileField from existing file on the same partition slow?
In my Django application I have to deal with huge files. Instead of uploading them via the web app, the users may place them into a folder (called .dump) on a Samba share and then can choose the file in the Django app to create a new model instance from it. The view looks roughly like this: class AddDumpedMeasurement(View): def get(self, request, *args, **kwargs): filename = request.GET.get('filename', None) dump_dir = os.path.join(settings.MEDIA_ROOT, settings.MEASUREMENT_DATA_DUMP_PATH) in_file = os.path.join(dump_dir, filename) if isfile(in_file): try: with open(in_file, 'rb') as f: object = NCFile.objects.create(sample=sample, created_by=request.user, file=File(f)) return JsonResponse(data={'redirect': object.get_absolute_url()}) except: return JsonResponse(data={'error': 'Couldn\'t read file'}, status=400) else: return JsonResponse(data={'error': 'File not found'}, status=400) As MEDIA_ROOT and .dump are on the same Samba share (which is mounted by the web server), why is moving the file to its new location so slow? I would have expected it to be almost instantaneous. Is it because I open() it and stream the bytes to the file object? If so, is there a better way to move the file to its correct destination and create the model instance? -
Any smart way to create a checkout for shopping in Django?
I'm trying to spin my head around how my checkout part of my page should run. Below is the process of checkout: review cart --> write in personal info --> payment my problem is how do I make sure a user can't jump directly to payment without having signed in their address? Is it possible to only be able to access a page if you have been redirected? I'm more curious if there's some "best practise" in this type of situation. -
Mix Count, Case and Distinct on a specific field
I have the following model: class NoteLink(models.Model): note_source = models.ForeignKey(Note, related_name="links_sourced") note_target = models.ForeignKey(Note, related_name="links_targeting") author = models.ForeignKey(User, related_name="links_created") is_public = models.BooleanField(default=False) I would like to count the amounts of links a note has that are public, so I have the following annotate: Note.objects.annotate(public_links=Count(Case( When(links_sourced__is_public=True, then=1), output_field=IntegerField())) ) The issue is that several users can create a link with the same source and target. So this annotate counts links with a different author, even if the source and target are the same. I would like to only count the the links with a distinct note_source and note_target. I know that Count has a distinct=True option. But how can I mix it with my Case to ignore even when the author is different? Or in other words, how to only count the ones with a different note_target? N.B: I am not using PostgreSQL but MySQL, so I cannot run distinct() on a specific field. -
Django TemplateSyntaxError: Error during template rendering
I am working on a Python/ Django project, having not really used either much before. I am currently getting a TemplateSyntaxError when clicking a link on one of the pages on my website. The URL that this link takes you to (the URL for the broken page) is: costing/id/payment-report/overview & the exception value says: Invalid block tag on line 87: 'date_to_display', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag? The template (HTML file) itself for this page, doesn't actually have this variable anywhere in it. The location of the template in the folder structure is: costing/templates/costing/reports_post_deposit.html, however, there is another template file at costing/pdf2_base.html, which does contain this variable within the structure: <body> ... {% block content_payment_schedule %} {% if not webview %} <div> <table> ... <tr> ... <td> {% date_to_display %} </td> </tr> </table> </div> {% endif %} ... {% endblock %} ... </body> So I can see that the variable it's complaining about is used within this template. The page at costing/id/payment-report/overview is used to generate a PDF file from information stored in the database, and information provided by the user. The structure of this PDF file is defined in the costing/pdf2_base.html … -
Filter an object with choice filed values in django
I am new to django. I have following choice field in my model Account. GENDER_CHOICES = ( (1, 'Male'), (2, 'Female'), (3, 'Other'), ) I want to search an user on the basis of gender. How can i search by either "male" or "female" as in database only 1 and 2 is saved. -
How to automatically change model fields in django
I have a model and I want to know if it is possible to set a condition that triggers a change in the model field. For example, I have a model class BillboardTracker(models.Model): client_name = models.CharField(max_length=400) entry_date = models.DateTimeField(default=datetime.now) duration = models.PositiveIntegerField() expiry_date = models.DateField() is_expired = models.BooleanField(default=False) I want to know if it is possible to have a function in the model that makes is_expired equals to True when the expiry date is up. I tried this def expire(self): if datetime.now == self.expiry_date: self.is_expired = True but it's not working. Is it possible to implement this? -
crispy form integration in Django using crispy_forms_materialize not working
I'm unable to overwrite default column setting of crispy_forms_materialize (as far as I know crispy form materialize is using s6 as default column layout). But my requirement is different I want few of my form field to render as s12, s3, and s8. I'm unable to overwrite default layout. enter image description here Any help or lead is really appreciated. I've one more question Is it a good idea to use [DropzoneJS][4] to upload the file(video, pdf, mp3) to Amazon S3 and use return absolute path of media and store it in Postgres database? Any help or lead is really appreciated. -
Force ConnectionError with Django cache in Tests
I have code which handles a ConnectionError when Django cannot connect to the Cache, which I'd like to test. I've hit an issue that without actually disabling the real cache, I can't simulate it within the tests. I have tried using the Django settings override: with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}): However the above only simulates a cache, and not that it isn't available. Is there a way to do this? Thanks in advance. -
How can I semplify this Django view? Made a mess with dicts and lists
I have an app that shows some questions to the user. It has to reply in input text fields and then there's a view that checks all these results (the one below). This view perfectly works but I know I made a mess with dictionaries and lists, since I'm not very good at those. I thought to create those just to prepare a results variable that then I'll print on the HTML page. How can I semplify it? def check(request): # Initialize results results = [] i = 0 for input_name, input_value in request.POST.items(): # Remove cfsr from inputs if input_name.isdigit(): # Get the question from the input name question = Question.objects.get(pk=input_name) # Get the related correct answer to that question answer = question.answer # Create results results.append({'question_text': question.text, 'user_answer': input_value, 'correct_answer': answer.text}) # Check if user answer is correct if input_value == answer.text: results[i]['is_correct'] = True else: results[i]['is_correct'] = False i += 1 context = {'results': results} return render(request, 'quiz/results.html', context)