Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to get latest unique entries from sqlite db with the counter of entries via Django ORM
I have a SQLite db which looks like this: |ID|DateTime|Lang|Details| |1 |16 Oct | GB | GB1 | |2 |15 Oct | GB | GB2 | |3 |17 Oct | ES | ES1 | |4 |13 Oct | ES | ES2 | |5 |15 Oct | ES | ES3 | |6 |10 Oct | CH | CH1 | I need a Django query to select this: |1 |16 Oct | GB | GB1 | 2 | |3 |17 Oct | ES | ES1 | 3 | |6 |10 Oct | CH | CH1 | 1 | So this is unique (by Lang) latest (by DateTime) entries with the number of occurrences (by Lang). Is it possible to do this with a single SQL or Django-ORM query? -
Serve Static files from Google Cloud Storage Bucket (for Django App hosted on GCE)
I am trying to serve the static Files for my django App from Cloud Storage Bucket but don't know the exact process. Can someone please suggest a proper way to do so ? Steps I did: Uploaded all the static files on Google Cloud Storage Bucket(www.example.com) using gsutil command. Edited /etc/apache2/sites-available/default-ssl.conf File. File Content: <VirtualHost *:443> ServerName example.com ServerAdmin admin@example.com # Alias /static /opt/projects/example-google/example_static Alias /static https://storage.googleapis.com/www.example.com/static <Directory /opt/projects/example-google/example_static> Require all granted </Directory> <Directory /opt/projects/example-google/example/example> <Files wsgi.py> Require all granted </Files> </Directory> WSGIDaemonProcess example python-path=/opt/projects/example-google/example:/opt/projects/example-google/venv/lib/python2.7/site-packages WSGIProcessGroup example WSGIApplicationGroup %{GLOBAL} WSGIScriptAlias / /opt/projects/example-google/example/example/wsgi.py SSLEngine on SSLCertificateFile /etc/apache2/ssl/example.com.crt SSLCertificateKeyFile /etc/apache2/ssl/example.com.key SSLCertificateChainFile /etc/apache2/ssl/intermediate.crt </VirtualHost> Settings.py File: # Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' # STATIC_URL = 'https://storage.googleapis.com/www.example.com/static/' STATIC_ROOT = os.path.join(BASE_DIR, '../example_static') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, '../example_media') STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), MEDIA_ROOT,) Any suggestion on what all additional changes are required for this task ? Thanks, -
Django-rq-scheduler: how to set interval by seconds from the django-admin
We are using django 1.10, django-rq, django-rq-scheduler. In the django admin one can set an "interval unit" but there's no "seconds". Tried entering a floating point value as (0.1) with "minutes" but submission fails with "Enter a whole number." error. Any ideas how to schedule tasks by seconds? Thank you -
How to make test case fail if a django template has a rendering error that would silently fail in production
I'm aware that django test cases are done with DEBUG=False and TEMPLATE_DEBUG=False, and that I can change it to True for a specific function, using from django.test.utils import override_settings @override_settings(DEBUG=True) def test_one_function(self): # This test should be failing and is not. # If I did not test manually I would'nt know ! pass But maybe there is a better, more generic solution that apply for eveything at once ? I have an error in my template : I included another template and the link is broken. If I manually check with DEBUG=True I get a TemplateDoesNotExist error. But during my test case the url is rendered without the broken include, it does not throw an error, and the http_status is 200. I already tested the very generic included template somewhere else, so I don't want to add test to see if what is inside was rendered correctly. But I want to see rendering fails, that's what my test are for ! I tried to set TEMPLATE_STRING_IF_INVALID to an Exception (found here), but it does not seem to be working for a broken include. Is there a way to make all rendering error raise en exception during tests, without breaking the … -
Django add form data in two models
Im trying create a new book and review from on request.POST. The issue here is the issue is that the data needs to go to two models with foreign keys. Here is the request.POST: def add(request): if request.method == 'POST': result = Review.objects.addBook_and_Review( user=request.session['id'], title=request.POST['title'], author=request.POST['author'], new_author=request.POST['new_author'], review=request.POST['review'], rating=request.POST['rating'] ) return redirect('add') else: return render(request, 'books/add.html') And here is the the custom manager and the two models (Review and Book). Note the Review model with foreign Keys. class ReviewManager(models.Manager): def addBook_and_Review(self, **kwargs): #custom manager code here return True class BookManager(models.Manager): print('hit book manager') pass class User(models.Model): name = models.CharField(max_length=200) alias = models.CharField(max_length=200) email = models.EmailField() pw_hash = models.CharField(max_length=200) created_at = models.DateField(auto_now_add=True) updated_at = models.DateField(auto_now=True) objects = UserManager() class Book(models.Model): title = models.CharField(max_length=200) author = models.CharField(max_length=200) created_at = models.DateField(auto_now_add=True) updated_at = models.DateField(auto_now=True) objects = BookManager() class Review(models.Model): review = models.CharField(max_length=1000) rating = models.CharField(max_length=200) created_at = models.DateField(auto_now_add=True) updated_at = models.DateField(auto_now=True) user = models.ForeignKey(User, on_delete=models.CASCADE) book = models.ForeignKey(Book, on_delete=models.CASCADE) objects = ReviewManager() -
django forms - set widget name
I need to change the name of the form element generated by Django forms. When I do class SemanticBlockForm(forms.ModelForm): class Meta: model = SemanticBlock fields = ('confirm_question', 'tags') widgets = { 'tags': forms.SelectMultiple(attrs={'name': 'some_custom_name', 'id': 'some_custom_id'}) } my input gets rendered like this <select multiple="multiple" class="form-control" id="some_custom_id" name="tags"> <option value="1">aaa</option> <option value="2">bbb</option> <option value="3">ccc</option> </select> why does django ignore my name setting? and how to fix this? (Specifically, I need to change the name tags to tags[] because otherwise I can't serialize it to a json list with jQuery.) -
x.objects.all() empty, user.x_set.all() not empty
This is probably really simple but I don't think I've come across this problem before. I have an object in my database called WeatherForecast, this object has a foreign key to User, so the user can have a number of different forecasts displayed in a template. class WeatherForecast(models.Model): owner = models.ForeignKey(User, null=True) I create the forecasts and set the foreign key as such: newForecast = WeatherForecast.objects.create(owner = request.user) When I call WeatherForecast.objects.all(), it returns an empty query set. However, when I call user.weatherforecast_set.all() it returns the WeatherForecasts with the correct foreign key. I'm probably missing something obvious, but why is this happening? -
Django-rq-scheduler: call function with arguments from django-admin
We are using django 1.10, django-rq, django-rq-scheduler. In the django admin one can set a callable function, but how is it possible to send specific param-values to that function (e.g. in the screenshot call dir_listener.dir_scanner.scan_all_directories(3, 'kuku') )? Thank you -
Using multiple columns as ForeignKey to return in another table
I'm new to Django so I make 3 simple tables to return a WishList. The thing is that I want whenever user asks for WishList, his/her user_id is used to make a SELECT query to return his/her own WishList. And I want to get product title and product url from my WishList table. I'm using to_field but with that way I only can get product title back. I don't know much about Django so help me! Product class Product(models.Model): class Meta: unique_together = (('id', 'title'),) title = models.CharField(max_length=200, unique=True, help_text='Name of the product') url = models.CharField(max_length=300, default='', help_text='Url of the product') def __str__(self): return 'Product: {}'.format(self.title) WishList class WishList(models.Model): class Meta: unique_together = (('user', 'product'),) user = models.ForeignKey(fbuser, on_delete=models.CASCADE, help_text='Facebook user', to_field='user_id') product = models.ForeignKey(Product, to_field='title', db_column='title', on_delete=models.CASCADE) def __str__(self): return 'WishList: {}'.format(self.user) -
Logout with python-social-auth
I am using python-social-auth for my third-party login and logout. Sign in with Facebook, Twitter and Google Plus were a success at first (it will ask my username/email and password). My problem is when I log out and then sign in again through either of them, I will be signed in automatically without even asking my username/email and password again. Am I not logged out? This is my disconnect pipeline: SOCIAL_AUTH_DISCONNECT_PIPELINE = ( 'social.pipeline.disconnect.allowed_to_disconnect', 'social.pipeline.disconnect.get_entries', 'social.pipeline.disconnect.revoke_tokens', 'social.pipeline.disconnect.disconnect', ) This is my logout view: from django.contrib.auth import logout as auth_logout def logout(request): auth_logout(request) return render_to_response('logged-out.html', {}, RequestContext(request)) -
How to add space after label in ModelForms of django
I want to add space after "Mobile No: ",meaning in between label and text area. But by using django ModelForms I am not able to do that, it shows "Mobile No:" without any space between label and textarea. This is how I have described it in models.py file. phone = models.CharField(max_length=10, verbose_name="Mobile No", validators=[mobileno]) This is how it is showing by default. How can I add space between label and textarea. Thanks for you help. -
Serve uploaded files from NGINX server instead of gunicorn/Django
I have separate servers one running NGINX and other running gunicorn/Django, I managed to serve static files from NGINX directly as recommended from Django documentation, but I have an issue with files uploaded by users, which will be upload to server has gunicorn, not the server has NGINX, thus users can't find their files and browse them. How to upload files from Django to another server? or How to transfer files from other server after uploading to NGINX? Note: I don't have the CDN option, I'll server my statics from my servers. -
Efficient way to perform a quesry on a jsonfield in django
am trying to extract some data on a jsonfield in django, but the queryset that i have written is taking forever to perfom this action.The database that am querying has over 10 million records :( can is their a more efficent way to do this? Here is the query from django.contrib.postgres.fields import JSONField from maidea.apps.upload_service.models import MobileUploadHouseholdLog rejected_hh_somalia = MobileUploadHouseholdLog.objects.filter(status=4, household__contains={'info':{'location':{'label':'Hantiwadag'}}}) my models.py class MobileUploadHouseholdLog(MAIDEAModel): objects = MobileUploadHouseholdLogManager() UNPROCESSED = 0 IMPORTED_TO_SCOPE = 1 ERROR = 2 DUPLICATE = 3 DISCARDED = 4 IMPORT_STATUS = ( (UNPROCESSED, _('New')), (IMPORTED_TO_SCOPE, _('Import successful')), (ERROR, _('Import failed')), (DUPLICATE, _('Duplicate upload')), (DISCARDED, _('Discarded upload')), ) uuid = UUIDField(verbose_name=_("Unique ID"), auto=True, version=4, help_text=_('unique id')) household = JSONField(_("JSON for a single household"), help_text=_('Contains data for a single Household'), blank=True, null=False) status = models.IntegerField(_("Processing status"), choices=IMPORT_STATUS) mobile_upload_log = models.ForeignKey(MobileUploadLog, verbose_name=_('Upload session where the Household is coming from'), related_name='mobile_upload_logs') processed_datetime = models.DateTimeField(null=True, blank=True, verbose_name=_('Timestamp when household was processed')) error = models.TextField(_("Error trace for this import"), help_text=_('Contains the exception of why this household cannot be imported'), blank=True, null=False) def save_error(self, error): self.status = MobileUploadHouseholdLog.ERROR self.error = error self.processed_datetime = timezone.now() self.save() def save_imported(self): self.status = MobileUploadHouseholdLog.IMPORTED_TO_SCOPE self.processed_datetime = timezone.now() self.save() def save_duplicate(self): self.status = MobileUploadHouseholdLog.DUPLICATE self.processed_datetime = timezone.now() self.save() … -
cant load external js file in html
I have tried all answers available but none can solve my problem. I have a html page in which I am drawing svg using js. now I want to store this js in external file and call it (my html and js files are in same folder). <script src="show1.js"></script> on doing so, my internal js cannot find functions defined in external js and gives 'ReferenceError: DrawLine is not defined'. I even tried using alert in external js to check whether it was being loaded or not, but even alert is not working. is there any settings that i will have to check? kindly help. My code is really huge. I am posting this snippet instead that shows the internal and external js. <head> <script> //only variables are declared <script> </head> <body> <script src="show1.js"></script> <script> drawline(10,10,20,20,'black',4); </script> </body> show1.js: function drawline(x1,y1,x2,y2,c,w) { //do stuff } Note:these pages are part of django project, and so my html and js are stored in the template folder of the project. -
Radio button along with char field
I need to create a django form where field is the combination of radio button and text box. Say: 0 choice 1 TEXT BOX - contains model form field value 0 choice 2 TEXT BOX - contains model form field value (Please refer attachment) I want to get the value of Text box on selection of choices (choice 1 or choice 2) Both the Textboxes (CharField in forms.py)are set to required=False as in forms only one will have the value. Thanks. -
python url not found - Accessing views.py from AJAX
New to Python and Django and I'm trying to make a simple ajax call from a button click to pass certain data to my views.py, however, when I try to make a url as seen on my ajax code below, the documentId.id does not append unless I directly append in without the "?id=". {%for document in documents%} {{document.filename}} <input type="button" id="{{document.id}}" onclick="loadData(this)" name="load-data" value="Add"/> {%endfor%} <script type ="text/javascript"> function loadData(documentId){ $.ajax({ url:"upload-data/load" + "?id=" + documentId.id, data: {'documentId': documentId}, type: 'GET', success: function(){ window.location.href = "http://127.0.0.1:8000/url/locations"; } }); } </script> This gives me then an error that says the url cannot be found. I have a line in my urls.py below: url(r^"upload-data/load/([0-9]+)/$', views.loadFile, name="load-data"), Other than this method, I am stumped as to how I am going to extract my data to my views.py. def loadFile(request): documentId = request.GET.get('id') newLayer = Layer(get_object_or_404(Document, pk = documentId)) newLayer.save() layers = Layer.objects.all() return render(request, 'url/loaded.html', { 'layers': layers}) The persisting error in the console would be: http://127.0.0.1:8000/upload-data/load/ [HTTP/1.0 404 Not Found] -
Correct way of sending JSON Data with encoding in Python
I am developing API's on Django, and I am facing a lot of issues in encoding the data in python at the back-end and decoding it at front-end on java. Any standard rules for sending correct JSON Data to client application efficiently? Thanks -
Django Channels HttpClient testing error
I have been trying to create a test for one of the consumers in channels' multichat example project: https://github.com/andrewgodwin/channels-examples/tree/master/multichat. The consumer function looks like this: @channel_session_user def chat_join(message): print(message.content) room = get_room_or_error(message[MsgFields.ROOM], message.user) if NOTIFY_USERS_ON_ENTER_OR_LEAVE_ROOMS: room.send_msg(None, message.user.nickname, message.user.id, MSG_TYPE_ENTER) room.websocket_group.add(message.reply_channel) message.channel_session[ChannelSession.ROOMS] = list(set(message.channel_session[ChannelSession.ROOMS]).union([room.id])) recent_msgs = Message.objects.filter(room=room.id).order_by(ChatModelFields.CREATED_AT)[:10] history = [] for msg in recent_msgs: history.append({ ChatModelFields.CONTENT: msg.content, ChatModelFields.CREATOR: msg.creator, MsgFields.MSG_TYPE: MSG_TYPE_MESSAGE, }) message.reply_channel.send({ 'text': json.dumps({ MsgFields.USER_JOIN: str(room.id), 'history': history, }) }) Now, the interesting error I kept running into goes like this. In my test function version 1: def test_chat_send(self): user = HubUser.objects.create_user( self.TEST_EMAIL, password=self.TEST_PASSWORD, nickname=self.TEST_NICKNAME ) room = ChatRoom.objects.create(creator=self.TEST_NICKNAME) msg_join = { MsgFields.ROOM: 1, MsgFields.COMMAND: 'join', } msg_send = { MsgFields.MESSAGE: self.TEST_MSG, MsgFields.CREATOR: user, MsgFields.ROOM: 1, MsgFields.COMMAND: 'send', } c = HttpClient() c.login(username=self.TEST_EMAIL, password=self.TEST_PASSWORD) c.send_and_consume(RECEIVER_CHANNEL, content=msg_join, fail_on_none=True) The test gives me the following error: Traceback (most recent call last): File "J:\Web\HubService\src\chat\tests\tests.py", line 60, in test_chat_send c.send_and_consume(RECEIVER_CHANNEL, content=msg_join, fail_on_none=True) File "C:\Users\Doge\Envs\dogeenv\lib\site-packages\channels\tests\base.py", line 127, in send_and_consume return self.consume(channel, fail_on_none=fail_on_none) File "C:\Users\Doge\Envs\dogeenv\lib\site-packages\channels\tests\base.py", line 118, in consume raise AssertionError("Can't find consumer for message %s" % message) AssertionError: Can't find consumer for message <channels.message.Message object at 0x0000000006086B70> Test function version 2: def test_chat_send(self): user = HubUser.objects.create_user( self.TEST_EMAIL, password=self.TEST_PASSWORD, nickname=self.TEST_NICKNAME ) room = ChatRoom.objects.create(creator=self.TEST_NICKNAME) msg_join = … -
how to convert date-time to epoch in python?
I want this particular date-time to be converted to epoch but it is giving me format error. what format should i use to do the following so that it can be json rendered. "2016-10-14 14:34:14+00:00". I am using python 2.7 and django 1.10 -
Catching NotImplementedError not executing code in except block
PostgreSQL is able to have .distint('field name') database queries, however Sqlite isn't, so I created a try/except block which should run a more simple query if the user is using sqlite3. try: qs = qs.filter(tag__istartswith=self.q).order_by('tag').distinct('tag') except NotImplementedError: qs = qs.filter(tag__istartswith=self.q) So if the user is using Sqlite I would expect the simple query in the except block to get executed, however the exception is thrown and the simple query never gets executed: raise NotImplementedError('DISTINCT ON fields is not supported by this database backend') NotImplementedError: DISTINCT ON fields is not supported by this database backend Do you have any idea why this isn't working as expected? Thanks -
Pass Value from View to controller and display in template
I need to pass the latest value from a database field to the controller. I'm also in due to perform certain calculations with respect to the value I get from the view in the controller. I'm not aware how to use the value from the view to the controller. I'm new to django. Any help would be appreciated. Below is my code, In my views.py class SiverifyAddReviewView(JSONResponseMixin, TemplateView): template_name = "siverify_add_review.html" l_id = SiverifyVerificationSiteRevision.objects.latest('id') def get_context_data(self, *args, **kwargs): context = super(SiverifyAddReviewView, self).get_context_data(*args, **kwargs) context['ngapp'] = "ReviewMod" return context In the below line, SiverifyVerificationSiteRevision is the model name and I need to the latest value of the field id. l_id = SiverifyVerificationSiteRevision.objects.latest('id') Is this the right method to get the latest value from the model? And Over to controller, I need to perform the below function. Controller: {{ngapp}}.controller( "SiVerifyAddReviewController", function($scope, $http, $modalInstance, r_header, context){ $scope.today= function dat() { var d = new Date(); return 'Review_' + **l_id** + ((d.getFullYear() + ' ' + (d.getMonth() + 1) + ' ' + d.getDate()).replace(/ /g, ''));} $scope.arform['revtitle']= $scope.today();} As in the return statement( l_id), I need to get the latest value from the views and get in the template. My template code is, Template: … -
Remove elements from DB that are not in an array, adding elements from an array that are not in DB
I'm currently trying to create a view in Django to receive and process JSON data from a POST request. I'm currently having issues trying to add/update multiple NotificationEmail objects in my database. What I would like to do is to check if the emails inside the array (email) sent via the POST request are already stored in a NoticationEmail object as the emailAddress element for that object. If it already exists as the emailAddress for a NotificationEmail object, I would like to keep it. If the email inside the array is not stored in any NotificationEmail object as the emailAddress value, I would like to add a new NotificationEmail object with this email as the emailAddress value. And finally, if there's any NotificationEmail for that specific id (given through the POST request) with an emailAddress value that is not listed in the emails array I want to delete it. I'm currently just getting an error message for the for loops I've created, so I can't run the server and try it out. I've checked that without the for loops it does receive the POST request and is able to add the full array of emails as an emailAddress value inside … -
gunicorn not starting on port 80 but starting on port 8000
I am using gunicorn and trying to write the upstart script. I am testing the command in the command line and for port 80 it just gets errors command gunicorn --bind 0.0.0.0:80 --workers 3 myapp.wsgi:application log [2016-10-19 02:36:51 +0000] [12752] [INFO] Starting gunicorn 19.6.0 [2016-10-19 02:36:51 +0000] [12752] [ERROR] Retrying in 1 second. [2016-10-19 02:36:52 +0000] [12752] [ERROR] Retrying in 1 second. [2016-10-19 02:36:53 +0000] [12752] [ERROR] Retrying in 1 second. [2016-10-19 02:36:54 +0000] [12752] [ERROR] Retrying in 1 second. [2016-10-19 02:36:55 +0000] [12752] [ERROR] Retrying in 1 second. [2016-10-19 02:36:56 +0000] [12752] [ERROR] Can't connect to ('0.0.0.0', 80) any ideas why this is not working? Sometimes it works for port 8000. -
Error importing storage module.whitenoise.django
I'm trying to deploy a basic Django app to Heroku, but am getting an error when I try to deploy. It looks like the error is with whitenoise. I have six installed as part of my requirements so it should handle urllib.parse. Here is the error: remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/files/storage.py", line 290, in get_storage_class remote: raise ImproperlyConfigured('Error importing storage module %s: "%s"' % (module, e)) remote: django.core.exceptions.ImproperlyConfigured: Error importing storage module whitenoise.django: "No module named urllib.parse" remote: remote: ! Error while running '$ python manage.py collectstatic --noinput'. Here is the full stack: remote: remote: -----> Python app detected remote: -----> Uninstalling stale dependencies remote: Uninstalling DateTime-4.1.1: remote: Successfully uninstalled DateTime-4.1.1 remote: Uninstalling simplejson-3.8.2: remote: Successfully uninstalled simplejson-3.8.2 remote: -----> Noticed cffi. Bootstrapping libffi. remote: $ pip install -r requirements.txt remote: Collecting altgraph==0.10.2 (from -r requirements.txt (line 1)) remote: Downloading altgraph-0.10.2.tar.gz (481kB) remote: Collecting bdist-mpkg==0.5.0 (from -r requirements.txt (line 2)) remote: Downloading bdist_mpkg-0.5.0.tar.gz remote: Collecting bpython==0.12 (from -r requirements.txt (line 3)) remote: Downloading bpython-0.12.tar.gz (130kB) remote: Collecting csvkit==0.7.3 (from -r requirements.txt (line 4)) remote: Downloading csvkit-0.7.3.tar.gz remote: Collecting Cython==0.19.2 (from -r requirements.txt (line 5)) remote: Downloading Cython-0.19.2-cp27-cp27m-manylinux1_x86_64.whl (4.0MB) remote: Collecting dbf==0.94.3 (from -r requirements.txt (line 6)) remote: Downloading dbf-0.94.003.tar.gz (79kB) remote: Collecting … -
Grabbing selected choice from Django template form
i have been googling for 2 days now without success. Im new to python and especially Django and just cant figure out how to grab a selection from a dropdown list from my html template. I would like to avoid using any javascript/ajax/ and even the use of Models (db stuffs) as this would complicate my existing code and Im already lost. ^__^ Firstly my environment: Python2.7 Django1.5 CentOS 6 views.py def get_cas_options(): ... ... # this function works and lets say returns: [a,b,c,d] ... return cas def get_cas_page(request): form = {} if form.is_valid(): .... .... form["cas_selected"] = get_cas_options() .... return render(request, 'templates/client_cas.html', form) client_cas.html <form method="post"> {% csrf_token %} <b>CAS:</b> <select name="cas_selected"> {% for option in cas_selected %} <option value="{{option.id}}" selected="selected">{{option}}</option> {% endfor %} <option value="all">all</option> </select> </form> The code provided works. As in I do get a drop down on my html page with options: [a,b,c,d]. What i have attempted was to add: client_cas.html <input type='submit' name='search!'/> and views.py user_cas_selected = form['cas_selected'] problem is no matter what i select from the drop down, I always get the entire list returned to user_cas_selected var so i end up with this: [a,b,c,d] and have no idea what the user selected. …