Posts

Showing posts with the label Django

Add Request.GET Variable Using Django.shortcuts.redirect

Answer : Since redirect just returns an HttpResponseRedirect object, you could just alter that: response = redirect('url-name', x) response['Location'] += '?your=querystring' return response Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py) I don't know of any way to do this without modifying the urls.py . I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering... You might want to write a thin wrapper to make this easier. Say, custom_redirect def custom_redirect(url_name, *args, **kwargs): from django.core.urlresolvers import reverse import urllib url = reverse(url_name, args = args) params = urllib.urlencode(kwargs) return HttpResponseRedirect(url + "?%s" % params) This can then be called from your views. For e.g. return custom_redirect('url-name', x, q = 'something') # Should ...

Assign User-objects To A Group While Editing Group-object In Django Admin

Answer : The save method above won't work if you add a new group and simultaneously add users to the group. The problem is that the new group won't get saved (the admin uses commit=False) and won't have a primary key. Since the purpose of save_m2m() is to allow the calling view to handle saving m2m objects, I made a save object that wraps the old save_m2m method in a new method. def save(self, commit=True): group = super(GroupAdminForm, self).save(commit=commit) if commit: group.user_set = self.cleaned_data['users'] else: old_save_m2m = self.save_m2m def new_save_m2m(): old_save_m2m() group.user_set = self.cleaned_data['users'] self.save_m2m = new_save_m2m return group yourapp/admin.py from django import forms from django.contrib import admin from django.utils.translation import ugettext_lazy as _ from django.contrib.admin.widgets import FilteredSelectMultiple from django.contr...

Cannot Get Django-debug-toolbar To Appear

Answer : I had the same problem but managed to fix it following dvl's comment on this page. Here is a summary of the fix: In settings.py if DEBUG: MIDDLEWARE += ( 'debug_toolbar.middleware.DebugToolbarMiddleware', ) INSTALLED_APPS += ( 'debug_toolbar', ) INTERNAL_IPS = ('127.0.0.1', ) DEBUG_TOOLBAR_CONFIG = { 'INTERCEPT_REDIRECTS': False, } In the project urls.py, add this url pattern to the end: from django.conf import settings if settings.DEBUG: import debug_toolbar urlpatterns += [ url(r'^__debug__/', include(debug_toolbar.urls)), ] Some information for news users as me, when dev on virtual or remote machine Add this ligne in a views.py file print("IP Address for debug-toolbar: " + request.META['REMOTE_ADDR']) When the views is call, you can see the client IP in the shell You have to add this IP the settings.py file INTERNAL_IPS...

Apps Aren't Loaded Yet. With Signals

Answer : In django 1.9 # it is not need edit __init__.py apps.py from django.apps import AppConfig class YourAppConfig(AppConfig): name = 'your_app_name' verbose_name = 'Your App Name' # optional def ready(self): from your.app.path import signals # import your signals.py settings.py # include path to YourAppConfig class: INSTALLED_APPS = [ 'your_app.apps.YourAppConfig', # ..., ] see django docs Try registering signals when the app first loads. In you __init.py__ file: default_app_config = 'yourappname.apps.YourAppConfig' In apps.py of the app: from django.apps import AppConfig class YourAppConfig(AppConfig): name = 'yourappname' def ready(self): from yourappname import signals Do this for every app involved in registering signals. Read this for more info. There are several things to try: remove "from django.contrib.auth import get_user_model" has you do...

Aggregation Of An Annotation In GROUP BY In Django

Answer : Update: Since Django 2.1, everything works out of the box. No workarounds needed and the produced query is correct. This is maybe a bit too late, but I have found the solution (tested with Django 1.11.1). The problem is, call to .values('publisher') , which is required to provide grouping, removes all annotations, that are not included in .values() fields param. And we can't include dbl_price to fields param, because it will add another GROUP BY statement. The solution in to make all aggregation, which requires annotated fields firstly, then call .values() and include that aggregations to fields param(this won't add GROUP BY , because they are aggregations). Then we should call .annotate() with ANY expression - this will make django add GROUP BY statement to SQL query using the only non-aggregation field in query - publisher . Title.objects .annotate(dbl_price=2*F('price')) .annotate(sum_of_prices=Sum('dbl_price')) ....