Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Maximum of an annotation after a group by
I would like to compute the maximum of "a_priority" for each group of (b, c) pairs. a_priority is an annotation based on a case/when mapping strings to priority values. from django.db.models import Max, Case, When, IntegerField qs = MyObject.objects.all() qs = qs.annotate( a_priority=Case( When(a='A', then=1), When(a='S', then=2), When(a='Q', then=3), output_field=IntegerField() ) ) qs = qs.values("b", "c").annotate(Max("a_priority")) I get the following error: KeyError: 'a_priority' I believe the qs.values("b", "c") filters out my annotation a_priority. Behavior is different with any actual field, providing the max of the field. My django version is 1.10 on python 3. -
Django 1.10 404.html template not found
As far as I have seen in the Django documentation, to show a custom 404 page all you have to do is put a 404.html in the root templates directory. So my project structure is: django_project |_ config | |_ settings.py | |_ urls.py | |_ templates |_ base.html |_ index.html |_ 404.html In settings.py I have the following settings for the templates: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ["templates", ] 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'contact.views.contact_info', ], }, }, ] for the 'DIRS' parameter I also used os.path.join(BASE_DIR, "templates"). This had the exact same outcome. I also used TEMPLATE_DIRS = ( os.path.join(BASE_DIR, "templates"), ) This lead to a deprecation warning. In urls.py I also did not really do anything special: from django.conf.urls import url, include from django.contrib import admin from django.views.generic import TemplateView urlpatterns = [ url(r'^$', TemplateView.as_view(template_name="index.html"), name="index"), url(r'^admin/', admin.site.urls), ] Template inheritance is working perfectly in other apps, so the templates directory is found. In settings: DEBUG=False What am I missing here? -
Product Bid view customize the serializer
I am bit new to DRF. I have Product and Bid model classes. When the product user logged in the system should give the bidders phone numbers and for others the normal bid view will be visible. How can I do this in DRF. My Model classes class Product(Model): category = models.ForeignKey(Category) title = models.CharField() condition = models.CharField() price = models.DecimalField() description = models.TextField() user = models.ForeignKey(User) class Bid(Model): price = models.DecimalField() product = models.ForeignKey(Product) user = models.ForeignKey(User, verbose_name='Bidder') status = models.CharField() class User(Model) telephone = models.CharField() My Current view class. class ProductBidListView(generics.ListAPIView): serializer_class = BidSerializer def get_queryset(self): return Bid.objects.filter(product_id=self.kwargs.get('product')) ProductBidListView is now returning all the bid information for the products. But I want to add if the request.user == product owner and bid status is accepted then the user should be able to see the phone number of that bid. How can I do it. Do I need to do custom APIView ? or anything can I add it inside Bid Serializer to_representation ? -
MySQLdb execute() works with unicode but not with subclass of unicode
I am building a library that extends unicode class with some other methods, I discovered that MySQLdb is failing when using my unicode class. This works : In [23]: c.execute("""SELECT * FROM django_site WHERE domain LIKE %s""",(u'éric',)) Out[23]: 0L But this not : In [24]: class UnicodeExtended(unicode): ...: pass ...: In [25]: c.execute("""SELECT * FROM django_site WHERE domain LIKE %s""",(UnicodeExtended(u'éric'),)) --------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call last) <ipython-input-25-9145669d2b00> in <module>() ----> 1 c.execute("""SELECT * FROM django_site WHERE domain LIKE %s""",(UnicodeExtended(u'éric'),)) /usr/lib/python2.7/dist-packages/MySQLdb/cursors.pyc in execute(self, query, args) 205 args = dict((key, db.literal(item)) for key, item in args.items()) 206 else: --> 207 args = tuple(map(db.literal, args)) 208 if not PY2 and isinstance(query, bytes): 209 query = query.decode(db.unicode_literal.charset) /usr/lib/python2.7/dist-packages/MySQLdb/connections.pyc in literal(self, o) 302 303 """ --> 304 s = self.escape(o, self.encoders) 305 # Python 3 doesn't support % operation for bytes object. 306 # We should decode it before using %. /usr/lib/python2.7/dist-packages/MySQLdb/connections.pyc in string_literal(obj, dummy) 213 # Note: string_literal() is called for bytes object on Python 3. 214 def string_literal(obj, dummy=None): --> 215 return db.string_literal(obj) 216 return string_literal 217 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128) Is it possible to use a sublass of unicode into sql … -
unable to see ng-model
i am trying to use angular js with django server here is the index.html {% load staticfiles %} <html ng-app="blog"> <head> <script type="text/javascript" src="{% static 'js/libs/angular.min.js' %}"></script> <script type="text/javascript" src="{% static 'js/modules/app.module.js' %}"></script> <script type="text/javascript" src="{% static 'js/modules/app.config.js' %}"></script> </head> <body> <input type="text" ng-model="name"> <p>hi , {{name}}</p> </body> </html> here is app.module.js 'use strict' angular.module('blog' , []); here is app.config.js 'use strict' angular.module('blog').config(function(){}); the ng-model variable name doesnt show up at all.No error occurs and nothing comes up on typing anything in the input box What can be the case and how can i recover from it ? -
Django : ModelFormset validation not properly work
models.py class OrderImage(TimeStampedModel): image = models.ImageField() forms.py class OrderImageForm(forms.ModelForm): class Meta: model = OrderImage fields = ('image',) Then, I submit my form without any image file in my.html And I break the views.py with embed() for debugging. views.py . . . def post(self, request, *args, **kwargs): from django.forms import modelformset_factory OrderImageFormSet = modelformset_factory( OrderImage, form=OrderImageForm ) order_image_formset = OrderImageFormSet( request.POST, request.FILES, queryset=OrderImage.objects.none() ) from IPython import embed; embed(); First, check request.FILES In [5]: request.FILES Out[5]: <MultiValueDict: {}> Here, I test two things. 1. order_image_formset validation. In [4]: order_image_formset.is_valid() Out[4]: True it shows True, even though there is no image file. **2. OrderImageForm validation ** In [1]: order_image_form = OrderImageForm(request.FILES) In [2]: order_image_form.is_valid() Out[2]: False In [3]: order_image_form.errors Out[3]: {'image': ['필수 항목입니다.']} It says, image field is required. I don't know why modelformset doesn't check validation correctly. Any Idea? -
Django & ReportLab - Thread-safe issue status
I look for a pdf library fully compatible with Django (1.9) and ReportLab seems ok except the thread-(un)safe issue. I'm very worried with that because I can't afford to invest too much time in a solution that works half. I didn't find any recent post about this issue. The most recent one was post 3 years ago and has no answer (asking the relevancy to use StreamingHttpResponse). All the other were post >4 years ago. Does anyone has a status? Is ReportLab still not thread-safe? What is the behavior of ReportLab when it fails? (raise an exception? print a wrong pdf file? other?) I also found this post with a possible solution (rl_config._reset())... does anyone could say if it does resolve the thread-safe issue? Yannick -
Django: ModelForm doesn't call clean_xxxx when validated?
models.py class OrderImage(TimeStampedModel): image = models.ImageField() forms.py class OrderImageForm(forms.ModelForm): class Meta: model = OrderImage fields = ('image',) def __init__(self, *args, **kwargs): self.helper = FormHelper() self.helper.form_tag = False super(OrderImageForm, self).__init__(*args, **kwargs) def clean_image(self, *args, **kwargs): from IPython import embed; embed(); I break clean_image with from IPython import embed; embed(); And I test it, def test_a(self): form = OrderImageForm({ 'image':'b' }) form.is_valid() But it doesn't break!.. Why it doesn't call clean_xxx? Because it is ModelForm? -
django-ckeditor, failed to load mathjax
I failed to load mathjax within django-ckeditor This post contains my virtualenv config, CKEDITOR_CONFIGS, printscreen, and an element from the Source page. virtualenv >pip freeze Django==1.10.2 django-appconf==1.0.2 django-ckeditor==5.1.1 django-compressor==2.1 django-debug-toolbar==1.6 Pillow==3.4.2 psycopg2==2.6.2 rcssmin==1.0.6 rjsmin==1.0.12 sqlparse==0.2.1 CKEDITOR_CONFIGS CKEDITOR_CONFIGS = { 'default': { 'skin': 'moono', # 'skin': 'office2013', 'toolbar_Custom': [ {'name': 'document', 'items': [ 'Subscript', 'Superscript', ]}, {'name': 'source', 'items': [ 'Source', ]}, ], 'toolbar': 'Custom', 'mathJaxLib': '//cdn.mathjax.org/mathjax/2.2-latest/MathJax.js?config=TeX-AMS_HTML', 'height': 200, 'width': 600, 'extraPlugins': ','.join(['mathjax', ]), }, } According to https://github.com/django-ckeditor/django-ckeditor/issues/256 I've changed ckeditor-init.js , I also tried various combinations, including {'name': 'math', 'items': ['mathjax', ]}, and {'name': 'math', 'items': ['Matjax', ]} in 'toolbar_Custom' list. Printscreen Source As you can see the panel contains all config I setup in config but mathjax. However, the page source contains the "toolbar_Basic", "toolbar_Full" & "toolbar_Custom". I'm not sure if Basic and Full should be present, according to my config. <div class="django-ckeditor-widget" data-field-id="id_false_answer_text" style="display: inline-block;"> <textarea cols="40" id="id_false_answer_text" name="false_answer_text" rows="10" required data-processed="0" data-config='{"toolbar_Basic": [["Source", "-", "Bold", "Italic"]], "toolbar_Full": [["Styles", "Format", "Bold", "Italic", "Underline", "Strike", "SpellChecker", "Undo", "Redo"], ["Link", "Unlink", "Anchor"], ["Image", "Flash", "Table", "HorizontalRule"], ["TextColor", "BGColor"], ["Smiley", "SpecialChar"], ["Source"]], "filebrowserUploadUrl": "/ckeditor/upload/", "skin": "moono", "filebrowserWindowWidth": 940, "filebrowserWindowHeight": 725, "width": 600, "height": 200, "filebrowserBrowseUrl": "/ckeditor/browse/", "language": "en-us", "toolbar": "Custom", … -
Django remember me doesn't work
I want, to close session when browser is closed, but if user checked remember me, session must be open after browser close, so i did this one but it doesn't work, it always closes. my settings.py: SESSION_EXPIRE_AT_BROWSER_CLOSE = True my views.py if request.POST['rememberme']: settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False -
Django haystack filtering by foreign key id
I want to filter my search query by default_category.pk. Here is my SearchIndex: class ProductIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) content_auto = indexes.EdgeNgramField(model_attr='content') default_category = indexes.IntegerField() def get_model(self): return Product def index_queryset(self, using=None): return self.get_model().objects.all() I tried both default_category = indexes.IntegerField(model_attr='default_category__pk') and def prepare_default_category(self, obj): return obj.default_category.pk Here is my view code products = SearchQuerySet().models(Product).filter(content=auto_query, default_category=category_id) But this queryset is empty. If I remove default_category=category_id from filter then the list is not empty. And if i try to see dir(products[0]), there are […, amount', 'app_label', 'brand', 'category_id', 'content_type', 'default_category_id', 'description', 'distance', 'get_additional_fields', 'get_stored_fields', 'id', 'log', 'model', 'model_name', 'object', 'pk', 'price', 'sale', 'score', 'searchindex', 'shop_id', 'stored_fields', 'title', 'verbose_name', 'verbose_name_plural'] So here are some fields, those not described in SearchIndex and field default_category_id (I don't know, where it is from, it seems to be always None). What I do wrong here? Maybe I need to write something in product_text.txt or I don't understand something important? I use django-1.9.8 and haystack version is (2, 5, 0). -
Amazon EC2 configurations explaination
A user has the power to choose its EC2 configurations which is CPU, memory, storage and networking capacity.. I am a backend-developer and I lack knowledge on the server side.. What are these configurations for? How will my code affect these configurations? Let's say I am running an API web service on an instance made of python that does mathematical computations -
log Post request data at middleware in django framework 1.9
I want to log each api request in the logger in django. For this purpose, I have defined a middleware class which is as follow: my middleware.py class HandleExceptions(object): start_time = 0 def process_request(self, request): #global start_time #print request.body self.start_time = datetime.now() def readlines(self): return list(iter(self)) def process_response(self, request, response): #print request.content #print request.POST.data #print request.readlines() lg.logInfoData(request.method,request.get_full_path(),self.start_time,'request.data') return response def process_exception(self, request, exception): ErrorResponse = {} if isinstance(exception,otExc.ControllerError): ErrorResponse['Error_code'] = otc.otErrorCodes.cntrlr elif isinstance(exception, otExc.ServiceError): ErrorResponse['Error_code'] = otc.otErrorCodes.service elif isinstance(exception, otExc.DatabaseError): print "coming in middleware ... database error" ErrorResponse['Error_code'] = otc.otErrorCodes.database else: excDetails = excInf.extractExceptionInfo() print "coming in middleware ... middleware error" print exception ErrorResponse['Error_code'] = otc.otErrorCodes.middleware ErrorResponse['Error_msg'] = 'Application Error!' lg.logInfoData(request.method,request.get_full_path(),self.start_time,'request.data') lg.logErrorData(request.method, str(excDetails['methodName']), str(excDetails['fileName']), request.get_full_path(), ErrorResponse['Error_code'], str(exception),str(excDetails['lineNum']),'request.data') return HttpResponse(json.dumps(ErrorResponse),content_type="application/json") lg.logInfoData(request.method,request.get_full_path(),self.start_time,'request.data') lg.logErrorData(request.method, str(exception.methodName), str(exception.fileName), request.get_full_path(), ErrorResponse['Error_code'], str(exception.exp),str(exception.lineNum),'request.data') ErrorResponse['Error_msg'] = exception.msg return HttpResponse(json.dumps(ErrorResponse),content_type="application/json") I want to log the information with the data in the POST request in process_response or process_exception methods and I have tried many things but unable to do so. Everytime I get the following error : WSGIRequest has no attribute 'content' // or 'data' Pleas help someone. -
difference between to_python method in formfield and modelfield in django
I am new to python django framework. I am learning forms and models. I have came across to_python method. It's defined in both Field class of form as well as field class of models. I am confused regarding, why two versions of to_python exists. I believe that to_python must be there at one place only(either in forms/models). I have read docs, but it doesn't say anything about the same. Can anyone throw light on the same ? Btw, apologyzing for bad english and such a newbie question. Thanks in advance. -
Errno 13-Permission denied: '/User'
I got this error message when I trying to add a profile photo for a user in django admin(127.0.0.1:8000/admin) Can anyone help me to figure out the reason ? OSError at /admin/role/role/6/ [Errno 13] Permission denied: '/User' Request Method: POST Request URL: http://127.0.0.1:8000/admin/role/role/6/ Django Version: 1.8.2 Exception Type: OSError Exception Value: [Errno 13] Permission denied: '/User' Exception Location: /Users/wangjohnson/Envs/TUAN/lib/python2.7/os.py in makedirs, line 157 Python Executable: /Users/wangjohnson/Envs/TUAN/bin/python Python Version: 2.7.10 Python Path: ['/Users/wangjohnson/superpm', '/Users/wangjohnson/Envs/TUAN/lib/python27.zip', '/Users/wangjohnson/Envs/TUAN/lib/python2.7', '/Users/wangjohnson/Envs/TUAN/lib/python2.7/plat-darwin', '/Users/wangjohnson/Envs/TUAN/lib/python2.7/plat-mac', '/Users/wangjohnson/Envs/TUAN/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/wangjohnson/Envs/TUAN/Extras/lib/python', '/Users/wangjohnson/Envs/TUAN/lib/python2.7/lib-tk', '/Users/wangjohnson/Envs/TUAN/lib/python2.7/lib-old', '/Users/wangjohnson/Envs/TUAN/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/wangjohnson/Envs/TUAN/lib/python2.7/site-packages'] model.py class Role(models.Model): SEX_CHOICES = { ('MALE','MALE'), ('FEMALE','FEMALE'), } name = models.CharField(max_length=20) role = models.CharField(max_length=30) sex = models.CharField(max_length=10,choices=SEX_CHOICES) intro = models.TextField() age = models.IntegerField() product = models.ForeignKey(Product,related_name='roles') photo = models.ImageField(upload_to='photos') def unicode(self): return self.name settings.py MEDIA_ROOT = '/User/wangjohnson/superpm/media/' -
Installing custom Django in Mac in custom Path where no root access
I am completely new to the python development. i do not have root access to my Mac. Was able to successfully install Python3.4 by untarring the tar file and using .configure and make commands. Few issues i am facing. I installed Django by downloading the tar and 'python setup.py install'. The last message says Django is successfully installed. 1. I cannot start a project using 'django-admin.py startproject project' It says no module named zlib 2. Suspecting the reason to be obsolete python version, in downloaed 3.5 tar and did the same steps. Still the same error when starting Django project- but i was able to see that ,django-admin.py still takes first installed python 3.4- Not sure why. 3. when i import django in python commandline , it says 'No module named 'django'' this is the path where i do the installations. and i do not have root access. /Users/parameswar/customPython Any help is appreciated.Thanks -
How to test external url or links in a django website?
Hi I am building a blogging website in django 1.8 with python 3. In the blog users will write blogs and sometimes add external links. I want to crawl all the pages in this blog website and test every external link provided by the users is valid or not. How can i do this? Should i use something like python scrapy? -
Django login required between two servers
I have two servers. Server A - this is whole bussines logic and API for mobile application. Server B - in simply words, this is a webpage. And now, I need to authenticate user on B but, by the 'user-data' from A. E.G. On A server I have user with loggin/password. And I need to use this same login/password on B server. There is some nice solution for that? Or just use tokens? -
Using Django with Pybbm, how do I set default Avatar picture?
I'm adding pybbm forum to my django page. Overall, the integration has been pretty simple, however i'm having some issues on the default avatar picture. As recommended in the Pybbm documentation, i'm doing the following. settings.py PYBB_DEFAULT_AVATAR_URL = 'allauthdemo.forumapp.avatar.avatarpic' avatar.py from django.contrib.auth import get_user_model user = get_user_model() class avatarpic(): return user.profile.avatar_url This isn't working and returns an error for the picture. I'm not how to even test it, any ideas on how to get it displaying correctly? -
Django STATIC_URL only works without leading slash
Django 1.10 using runserver in development For some reason, no files can be found in my static directory unless I have it listed as such: STATIC_URL = 'static/' if not os.environ.get('DEPLOYED'): STATIC_ROOT = os.path.join(BASE_DIR, "static/") But all documentation and everything I have done before says it should be /static/ which makes me think I'm doing something wrong elsewhere. I am using Django's runserver URLS: if settings.DEBUG: from django.conf.urls.static import static from django.contrib.staticfiles import views urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) -
Django channels Group send dies after sending a certain number of times
I am having some strange behavior where: Group('chat-'+ name).send({'text': json.dumps(m)}) fails after performing this send 10 or more times. What is a common reason for this? I do not see any error messages on my worker, other than it does not log that it is sending any information. Normally I see: 2016-10-29 04:02:37,800 - DEBUG - ws_protocol - Sent WebSocket packet to client for !websocket.send.wTXtcZZQ This message will not display after a certain number of sends. I am doing some updating behavior where this send will occur every 5 seconds. -
Choices in field depend on the choices of another field
Been struggling with this for a while now and can't seem to get it. Say i have the following models: class Person(models.Model): name = models.CharField(max_length=100) address = models.CharField(max_length=300) class CD(models.Model): PERIODICITY_CHOICES = ( ("Month", "Month"), ("Quarter", "Quarter"), ) MONTH_CHOICES = ( ("Jan", "Jan"), ("Feb", "Feb"), ("Mar", "Mar"), ("Apr", "Apr"), ("May", "May"), ("Jun", "Jun"), ("Jul", "Jul"), ("Aug", "Aug"), ("Sep", "Sep"), ("Oct", "Oct"), ("Nov", "Nov"), ("Dec", "Dec"), ) QUARTER_CHOICES = ( ("Q1", "Q1"), ("Q2", "Q2"), ("Q3", "Q3"), ("Q4", "Q4"), ) periodicity = models.CharField( max_length=30, choices=PERIODICITY_CHOICES ) period = models.CharField( max_lenght=30, choices=MONTH_CHOICES + QUARTER_CHOICES ) person = models.ForeignKey(Person) Suppose i want an inline_formset were i can record personal info and the periodicity of the CD's which will be returned monthly or quaterly. I wish to select periocidity and according to the choice period should be presented as months or quarters. How can i achieve this? I tried defining a Media method for the model as it seems to be done for the admin.model but it doesn't work with this inline_formset. Please help. -
Django dynamic forms validation
So I am new to Django and I have created a View which uses a total of 8 forms dynamically. There is one base form which is always displayed and then there are 7 more forms that are displayed only if user selects that option from a drop down (drop down is in the HTML template). I am now trying to validate the form fields and when I display all 8 forms statically, the validation (clean methods) for each of the forms work perfectly! However when I change that to dynamically display the forms based on user selection, the validation for the base form fails every time. Any idea why that could be happening? I could provide instances of the forms/view if that would help! -
DRF The submitted data was not a file. Check the encoding type on the form - ImageField
I have the following model User serializer: The team field is a ForeignKey in the User model and it's serialized for optimize their query in performance with the setup_eager_loading function class UserSerializer(serializers.ModelSerializer): team = serializers.StringRelatedField() def setup_eager_loading(queryset): queryset = queryset.select_related('team',) class Meta: model = User fields = ('url', 'username','password','first_name','last_name', 'photo','team','position','last_login',) My viewset to User is the folowing: from rest_framework import viewsets from rest_framework import filters from .models import User from .serializers import UserSerializer # Viewsets define the behavior of the view class UserViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ queryset = User.objects.all().order_by('-date_joined') serializer_class = UserSerializer filter_fields = ('username','is_player', 'first_name','last_name','team','email',) When I go to an user instance in my api serializers and I want update via raw data media type: application/json, I get the following message: In my User model the image field is defined of this way: class User(AbstractBaseUser, PermissionsMixin): #... other attributes photo = models.ImageField(upload_to='avatars', blank=True) How to can I update any field/attribute in my User serialized model via JSON raw data without appear this message about of encoding type in the photo field In this question talk about of base64 encoded string in relation to DRF expected in the update request, but … -
How do I use requests POST to send text and run function based on text?
I'm using Django with python requests and my question is based on my inexperience with it. When I run a request.GET command I would like to transfer two variables (strings). Depending on the variables passed, a function will be run that ends with a file being downloaded. I have an API set up great running my request.GET commands without variables perfect. I'm not sure if I should point my request.GET to a different address, or just need to add logic within Django somehow?