Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Using mock in testing of django application to override a function
I have view function that uses nmap to scan the devices in the network. views.py import nmap def home(request): y=nmap.PortScanner() data = y.scan(hosts="192.168.1.*", arguments="-sP") context[status]=data['status']['addresses']['ipv4'] return render_template('home.html',context) Now i want to test this for no devices, 1 device connected and 2 or more device connected. I need to override data in tests.py. I was thinking that it can be done using mock function. I can override it in tests.py but when simulate responses it not get override in view function. How can i test this nmap function ? -
Docker/Django/Celery/RabbitMQ execute old versions of my code that was removed
I am running my Django app inside of a docker. I am using a background job as well as a periodic job with Celery + RabbitMQ, running in separate containers from the main app. Everything works locally with "heroku local". When running my app on an Ubuntu instance on Digital Ocean, I noticed that the background task and periodic task are executing old versions of my code. Specifically, I removed a field from my Django model last week, and the old code was referencing that deleted field so an error occurs. However, my new code no longer makes any reference to the missing field. Here are a few things I tried: rebuild and restart docker (didn't work) delete all .pyc files (didn't work) purge all celery tasks (didn't work) Restart my digital ocean instance (didn't work) Moving all my code and docker environment to a brand new digital ocean instance. (THIS WORKED!) I have encountered this problems twice now, and I am hoping to find a better solution than moving to a new machine every time this error happens. I am guessing that Celery or RabbitMQ has cached the old code somewhere that I was not aware of. Thanks in … -
Will the fields of Solr Documents only be saved as lists?
I have created a document in Solr and provided some data in it. The data are saved as list in the document as show below: "response":{"numFound":2,"start":0,"docs":[ { "ad_id":[1], "hash":["1"], "campaign_id":["1"], "fingerprint":[1], "id":"44c458eb-0674-4fce-ad54-e16234c868bf", "_version_":1577315711035899904}] }} I am currently providing a static data to Solr form django like below: solr.add([ { "hash": "1", "ad_id": "1", "campaign_id": "1", "fingerprint": "1", } ]) I want the data to be saved as below (not in list form): "response":{"numFound":2,"start":0,"docs":[ { "ad_id":"1", "hash":"1", "campaign_id":"1", "fingerprint":"1", "id":"44c458eb-0674-4fce-ad54-e16234c868bf", "_version_":1577315711035899904}] }} Is it possible? If it is, how can I send my data so that it wont ba saved as list? -
Removing prefix u in list (there are extra u as a prefix)
i have read up on remove the character 'u' in a list but it does not work in my case : [u"[u'DUTY,", u'RHODE', u'ISLAND', u'STATE', u'ASSOCIATION', u'OF', u'FIRE', u"OF']"] Output : DUTY RHODE ISLAND STATE ASSOCIATION OF FIRE OF Thanks -
500 (Internal Server Error) GET and request POST from JQuery/ajax to heroku server
I have my Django application deployed on Heroku in a free dyno Currently, I am sending some GET and POST requests from JQuery-1.12.4 version In my local development version of my application, the requests to my REST endpoints services it’s works Here an example of the fetch of posts of an user, consuming from ajax to endpoint rest service This is my localhost Django server I deploy on Heroku free dyno this same user interface, but the behavior is the following: The server throw an error 500, although was happen some inusual or strange Previously, when I create an user for first time in my Heroku deployment to this user, the create and fetch post functionality it’s works O.K. like in my local environment development. Heroku accept GET and POST request from JQuery AJAX ONLY to this user, such as follow in this picture: I Unknown the reason which Heroku like production/testing server deployment does not accept or accept limited AJAX requests. Is possible (I think so, and will be too logic) that this behavior to be due to I am using free dyno heroku server? Why only accept ajax request to just an user and other users like the … -
django request method dynamically
I know that request.method will give the type either 'GET' or 'POST' as string and to get any particular parameter we give request.GET.get('parameter') or request.POST.get('parameter') Now I want to get the GET or POST dictionary dynamically avoiding If conditions. -
Django model fields names are missing
Why my django model fields names are mising in my form? the fields are rendered on my template but its respective field names are missing e.g names,description,category etc.Can anyone suggest whats going on? -
Django Rest Framework Serializer popped off the validated_data after is_valid() call, causing KeyError in create
I am just following the document to test to write a nested serializer from here http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers Below is my testing code print(data_to_save) my_as = AlbumSerializer(data=data_to_save) my_as.is_valid() print(my_as.validated_data) my_as.save() data_to_save is this {'album_name': 'Peter ABC', 'artist': 'Peter', 'tracks': [OrderedDict([('order', 1), ('title', 'song 1'), ('duration', 11)]), OrderedDict([('order', 2), ('title', 'song 2'), ('duration', 11)])]} It has a key 'tracks' After calling is_valid(), the printed validated_data becomes OrderedDict([('album_name', 'Peter ABC'), ('artist', 'Peter')]) Key 'tracks' kind of popped. It causes save() error class AlbumSerializer(serializers.ModelSerializer): slug_field='title' ) tracks = TrackSerializer(many=True, read_only=True) class Meta: model = Album fields = ('album_name', 'artist', 'tracks') def create(self, validated_data): print(validated_data) tracks_data = validated_data.pop('tracks') album = Album.objects.create(**validated_data) for track_data in tracks_data: Track.objects.create(album=album, **track_data) return album tracks_data = validated_data.pop('tracks') will fail. The print(validated_data) inside create() also has 'tracks' key suddenly disappear {'album_name': 'Peter ABC', 'artist': 'Peter'} May I know why? My Django version 1.11.4 final and Django Rest Framework version is 3.6.3 -
Django Rest framework nested serializer not returning relation values in validated_data
I have defined everything correctly. (GET is working correctly) I can get the user detail through nested serializer. But when I do PUT I am not getting validated_data in update method which is in UserSerializer. models.py class UserProfile(models.Model): user = models.ForeignKey(User, related_name='users', on_delete=models.CASCADE) phone_number = models.CharField(max_length=15) known_languages = models.CharField(max_length=100) date_updated = models.DateTimeField(auto_now=True) user_domain = models.CharField(max_length=100, default=None) serializers.py class UserProfileSerializer(serializers.ModelSerializer): class Meta: model = UserProfile fields = ['phone_number', 'known_languages', 'user_domain'] class UserSerializer(serializers.ModelSerializer): users = UserProfileSerializer() class Meta: model = User fields = ['first_name', 'last_name', 'username', 'email', 'users'] def update(self, instance, validated_data): import pdb;pdb.set_trace() #here I am not getting users value. it is returning empty list return instance views.py def put(self, request, pk): user_obj = self.get_object(pk) request.POST._mutable = True request.data['users'] = { 'phone_number': request.data.pop('phone_number'), 'user_domain': request.data.pop('user_domain'), 'known_languages': request.data.pop('known_languages') } request.POST._mutable = False serializer = UserSerializer(user_obj, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) Can any one help on it...... -
Creating an admin panel for Django site
What I would like to have is an admin panel that I create where only users with permissions set to "admin" in the UserLevel model can edit certain aspects of the website. However, while I think I have everything coded out properly it isn't working. The way I'm trying to implement this already work on the main content section of my site. My best guess is that since I'm trying to cross-use views between apps something got mixed up, although that may be wrong. Also please keep in mind that while I definitely started with clean code which emulated the code from the working parts of the site this code may not so clean because I have been playing around with it so much that I'm just flat out confused... so any help would be much appreciated. Project urls.py: """colors URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the … -
Django order_by on charfield with alphanumeric characters and foreign key lookup
I have a situation where I need to pass through an ordered table to my template, trouble is the table has some rows which are numeric, others which are text and others that are a combination of both. The first column on which the table is sorted has a foreign key relationship. This is my query at the moment: placing = ClubPlacing.objects.filter(event=event).order_by('division__code','placing') Both division__code and placing have rows which are numeric and text. (in models as a charfield) When the data is displayed I have 1, 10, 11, 2, 3, 4, 5... ...CT, O1 (that last one is the letter O) The placing column displays 1, 10, 11, 2, 3... ...no comp I am using Django 1.8 and Postgres on Linux. -
Can't load keras model in Django (OSError at )
I'm trying to load keras model in Django. This is my simple view to store loaded keras model. from django.core.cache import cache from keras.models import load_model model_cache_key = 'md_d' # Create your views here. def main_water_view(request): # load model model = cache.get(model_cache_key) if model is None: model = load_model('rmsprop.h5') <- errors here!! (OSError at /water/) cache.set(model_cache_key, model, None) # save trained model into the cache # model.predict() # Convert it to the normal array context = {'array_context': 'a'} return render(request, 'main_water.html', context) And the error prints... "OSError at /water/ Unable to open file (File signature not found)" p.s) model is Sequential model Thank you so much! -
Heroku/Django Static files are ignored or not used
I'm new to making web apps in general and I finally got my app deployed (hurray!), but none of the Bootstrap CSS is showing. Depending on how I have my settings.py set up, I get different errors/warnings: PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.9/howto/static-files/ STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') STATIC_URL = '/static/' # Extra places for collectstatic to find static files. STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, '../mkmt/static'), ) Like this, I get a bunch of errors saying something along the lines of: Found another file with the destination path 'fonts/glyphicons-halflings-regular.ttf'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path. (it does this for all the bootstrap files). Anyway, the CSS doesn't show. If I comment out the last 3 lines (which doesn't make sense theoretically), those warnings go away (but the CSS still doesn't show). So I'm thinking based off the previous error that the program is using that directory earlier on and then blocking the static files declaration that I need. Or something. Any help would be great. Because there's obviously a lot of code that could be … -
Using Django ORM inside an AWS Lambda function
I have an existing Django application data stored under Postgres RDS. Now I want to query/update the data via a lambda(AWS) function with Django style ORM. I know in theory it is possible if, Package the whole Django library with the lambda function(zipped) Declare all the models inside package Start using Django ORM as we do normally. I wanted to know if anyone has done this? Examples or write-ups are much appreciated -
Page not found with "empty slug" pattern on Django
In my application, I want to show a list of items based on certain url. For example, when I type mysite.com/restaurants/chinese/, I want to show all the Chinese restaurants. If I type mysite.com/restaurants/american/, I want to show all the American restaurants and so on. But when I type mysite.com/restaurants/ I want to show all the restaurants, so I wrote this code: urls.py from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^restaurants/', include('restaurants.urls')), ] restaurants/urls.py from django.conf.urls import url from django.views.generic import ListView from .views import RestaurantLocationListView urlpatterns = [ url(r'^(?P<slug>\w+)/$', RestaurantLocationListView.as_view()), ] restaurants/views.py from django.db.models import Q from django.shortcuts import render from django.views.generic import ListView from .models import RestaurantLocation class RestaurantLocationListView(ListView): def get_queryset(self): slug = self.kwargs.get('slug') if slug: queryset = RestaurantLocation.objects.filter( Q(category__iexact=slug) | Q(category__icontains=slug) ) else: queryset = RestaurantLocation.objects.all() return queryset It's everything working well, except when I put only mysite.com/restaurants/. This gives me an 404 error instead the list of all restaurants and I don't know why. Can you guys help me? -
Displaying images in django 1.11
I made an app were an admin can add pictures. I get this error when i run the django server: The 'photo' attribute has no file associated with it. in the command line i get this error: ?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DEBUG. -
How to get equally distanced rows based on datetime from a django query?
I am developing a web application which stores hundreds of date times in datetimefield. I want to fit the datetimes into a line chart. As there can be hundreds of datetimes in the table. I want to minify the data by selected 10 equally distanced datetimes from the entire table. Here is my code: data = Model.objects.all() datetime_first = data.first().datetime datetime_last = data.last().datetime datetime_diff = (datetime_last-datetime_first).seconds diff= datetime_diff/5; datetimes=[] for i in range(0,5): datetimes.insert(i,datetime_first + i*datetime.timedelta(seconds=diff)) data = Model.objects.filter(datetime__in=datetimes) The problem is that it only returns one row in data as a result? Any idea what is the problem with it? -
Mongodb doesn't get restarted with subprocess call
I'm trying to restart mongodb from a browser. Here is my code written with django framework: def restart_db_mongo(request): if request.method == 'POST': restart_cmd = ["sudo","systemctl","restart","mongodb"] p = subprocess.Popen(restart_cmd,stdout=subprocess.PIPE) (output, err) = p.communicate() if err is not None: return JsonResponse({"err_code":1,"err_description":str(err)},safe=False) status_cmd = ["sudo","systemctl","status","mongodb"] p = subprocess.Popen(status_cmd,stdout=subprocess.PIPE) (output, err) = p.communicate() if 'active (running)' in output: return JsonResponse({"err_code":0,"err_description":"Restarted successfully!"},safe=False) else: return JsonResponse({"err_code":1,"err_description":str(err)},safe=False) else: return JsonResponse({"err_code":2,"err_description":"Bad request"}, safe=False) My problem is that mongodb doesn't get restarted. In fact, it finishes the first subprocess call without an error, but the status still remains inactive. In interactive environment, I've verified that the commands work. I'm using apache2 to run my web application. -
How to move JavaScript string containing HTML code to it's own file
In HTML, you can write JavaScript code in the HTML file using the script tag, and later extract that script to it's own file. I need to do the opposite. I have a JavaScript file containing HTML code stored as a string variable. For example: var foo = '<p>lots of code here</p>' How can I extract the HTML code to its own file and link it back to its original position in the JavaScript file? The section of code that I am working on is a collapsible element of another HTML document that is part of a Django view. -
Django trying to save data into new model after saving in another
Hi Guys I am struggling for the past 3 days getting the following working: I can not test this on localhost as the payment gateway needs to be on a live server. The data does get saved to the payfast model but not the bookings model My code as follows Bookings View class ViewBookingSummery(LoginRequiredMixin, FormView): login_url = '/login/' def get(self, request, slug, pk, guests): if pk: events_data = Events.objects.get(pk=pk) user = User.objects.get(pk=request.user.id) if events_data: formset = EventBookingForm() forms = PayFastForm(initial={ # required params: 'amount': int(events_data.price) * int(guests), 'item_name': events_data.title, 'name_first': user.first_name, 'name_last': user.last_name, 'email_address': user.email, 'custom_str1': user.id, 'custom_str2': events_data.id, 'custom_str3': events_data.date, 'custom_str4': guests, # optional params: 'return_url' : settings.DOMAIN_NAME + '/bookings/', 'cancel_url' : settings.DOMAIN_NAME + '/booking-summery/' + slug + '/' + pk + '/' + guests + '/' # ... etc. }, user=user) return render(request, 'bookings/booking-summery.html', {'events_data': events_data, 'guests': guests, 'formset': formset, 'forms': forms}) else: return HttpResponseRedirect('/') else: return HttpResponseRedirect('/') My Payment Gateway model (Saves in the database after successful payment) class PayFastOrder(models.Model): # see http://djangosnippets.org/snippets/2180/ __metaclass__ = readable_models.ModelBase # Transaction Details m_payment_id = models.AutoField(primary_key=True) pf_payment_id = models.CharField(max_length=40, unique=True, null=True, blank=True) payment_status = models.CharField(max_length=20, null=True, blank=True) item_name = models.CharField(max_length=100) item_description = models.CharField(max_length=255, null=True, blank=True) amount_gross = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True) … -
Where to put a custom validator in django?
I am working on implementing the solution outlined here: How to use custom password validators beside the django auth password validators? The answer is very helpful except for one aspect: Where do I put the code for the validator?! In AUTH_PASSWORD_VALIDATORS I see the line: 'NAME': registration.validators.CustomPasswortValidator but how do I put the CustomPasswortValidator method into registration.validators? What even is registration.validators? Where can I find that? Thank you -
valid UUID is not a valid UUID
I have a really weird problem were I'm getting that a valid UUID is not a valid UUID, for example: 'fd31b6b5-325d-4b65-b496-d7e4d16c8a93' is not a valid UUID. File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value 2371. value = uuid.UUID(value) File "/usr/lib64/python3.4/uuid.py" in __init__ 134. hex = hex.replace('urn:', '').replace('uuid:', '') During handling of the above exception ('UUID' object has no attribute 'replace'), another exception occurred: My models have an UUIDField as a pk, the error seems to happen randomly and won't dissapear until I restart the server, when the server restarts it works ok, so I'm a little lost here, I use django 1.10.7, postgresql 9.6.3, python 3.4.3 in amazon aws. -
Does Angular Routes accept a complete url to templateUrl?
Consider the following scenario: I have an application Python (Django) running in a Heroku environment with a detached frontend using Angular 1.6.5. The frontend build process puts files in my Django application static directory. Due to Heroku rules, when DEBUG = True, no static files will be served, so all files in static directories in my Python application will be copied to Amazon S3 by python manage.py collectstatic --no-input. I tried to configure the templatesUrl with a full qualified url from Amazon S3 like https://myapplication.s3-sa-east-1.amazonaws.com/html/index.html but I have got no success. When I set DEBUG = True in settings.py, once the static files are with the application, it works. The actual router looks like in angular.module().config(): $routeProvider.when('/', { templateUrl: 'static/html/index.html' }) When I tired $routeProvider.when('/', { templateUrl: 'https://myapplication.s3-sa-east-1.amazonaws.com/html/index.html' }) did not work. I inspected console to get some errors. Sometimes I have got nothing... and had a blank screen... but sometimes I have got a $compile error due to full qualified url returned a 404. I tried the same url (extract from 404 error) and made a normal request in browser and it works. I considered S3 Bucket policy too... but got stucked. Any help? -
Serialize a many-to-many relationship with a single extra field with Django REST
I have a simple model where a User can take part in multiple Game and a Game can be played by multiple User. Each User has a score field in a Game. To achieve this, I have followed the example provided in the Django documentation to implement such a ManyToMany relationship with an additional field by creating a third model Membership to link the Game table and the User one. I have the following models.py: from django.contrib.auth.models import AbstractUser from django.db.models.signals import post_save from django.dispatch import receiver from rest_framework.authtoken.models import Token from django.db import models class User(AbstractUser): pass # triggered as soon as a new user is saved in the db @receiver(post_save, sender=User) def create_auth_token(sender, instance=None, created=False, **kwargs): if created: Token.objects.create(user=instance) class Game(models.Model): users = models.ManyToManyField(User, through='Membership') class Membership(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) game = models.ForeignKey(Game, on_delete=models.CASCADE) score = models.IntegerField() Then I have written serializers.py: from rest_framework import serializers from .models import User, Membership, Game class UserSerializer(serializers.ModelSerializer): class Meta: model = User # unique email model._meta.get_field('email').__dict__['_unique'] = True fields = ('id', 'username', 'password', 'email') def create(self, validated_data): user = super().create(validated_data) if 'password' in validated_data: user.set_password(validated_data['password']) user.save() return user class GameSerializer(serializers.ModelSerializer): users = UserSerializer(many=True) class Meta: model = Game fields = … -
Postgresql User not connecting to Database
For almost a month now I have been struggling with this issue. Whenever I try to access my Django Admin page on production I get the following error: OperationalError at /admin/login/ FATAL: password authentication failed for user "vpusr" FATAL: password authentication failed for user "vpusr" My production.py settings file is as follows: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'vpdb', 'USER': 'vpusr', 'PASSWORD': os.environ["VP_DB_PASS"], 'HOST': 'localhost', } } NOTE: the environment variable is working correctly. even if I put the normal password hard coded in there it doesn't work. Here is the list of databases with their owner: List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres vpdb | vpusr | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/vpusr + | | | | | vpusr=CTc/vpusr And here is the list of users: List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, …