Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Data integrity and recovery with Django and Nginx
Django can be run either in Nginx (or some other server, but we are currently going to use Nginx) or with manage.py runserver Django own's server. In both cases I need data integrity and recovery. For data integrity I need not to terminate (some of) Django requests until they finish. They should not terminate because they should finish started data modification in the DB to preserve data integrity (and no, using SQL transactions is not a solution). They should not terminate as soon as Nginx receives service nginx stop (on Debian Linux) or some other similar command (on other OSes), but finish processing before terminating. How to do this? For data recovery I want: create an empty file when the server starts, remove it when the server stops (how to do it both with Nginx and with manage.py runserver?) When the server starts, if the file is found (indicating a crash of our software), before server starting we need to run my "data recovery" script. How to do this? -
My celery task never returns
I am just starting to learn about Django and have just discovered celery to run async background tasks. I have a dummy project which I pilfered off the internet with a sample task as follows: from djcelery import celery from time import sleep @celery.task def sleeptask(i): sleep(i) return i Now in my view, I have the following: def test_celery(request): result = tasks.sleeptask.delay(10) return HttpResponse(result.task_id) This runs fine and when I point the browser to it, I get some random string like 93463e9e-d8f5-46b2-8544-8d4b70108b0d which I am guessing is the task id. However, when I do this: def test_celery(request): result = tasks.sleeptask.delay(10) return HttpResponse(result.get()) The web browser goes in a loop with the message "Connecting..." and never returns. I was under the impression, this will block till the task is run and give the result but that does not seem to be the case. What am I doing wrong? Another question is the way I am doing it, is it going to run it asynchronously i.e. not block while the task is running? -
How to try-except get an object in one line?
This is a wet snippet that tries to get an object and not make an error if it doesn't exist, how can I dry it? try: return json_encoder.encode(ContactType.objects.get(pk).values()) except ContactType.DoesNotExist: return Ideally, I'd like to be able to use something like this: return try_get(json_encoder.encode(ContactType.objects.get(pk).values())) But I don't know what try_get would look like, I don't think it's possible to pass that line into a function as a parameter. How can I accomplish this? -
Django can't find one css file
Ok so this is really strange, I added userena for django 1.9, and everything works fine but i am trying to customize the login page. So i made a templates folder, userena, signin_form.html and it works, but when i try to load css with {%static 'css/loginstyle.css'%} (also loaded staticfiled) it says it can't find the file. It's certainly there, no typo or anything. Other css's in the same folder i can find perfectly fine with static_url, like bootstrap for example. Any idea why it doesn't work? It's in the folder for sure and it's not a typo -
What is the frontend language/platform of api.ai and wit.ai?
I am trying to develop a web application like wit.ai or api.ai. I am not sure what would be the best frontend language/platform/technology for such a web applications. In api.ai and wit.ai you create chat robots. For each robot the user define a bunch of keywords as entity. For example Entity: Pizza -> {Italian, French, Greek} Then the user define templates for the input sentences and assign the keywords of the input sentence to the corresponding entities. For example: I would like to order a Italian with two topping. In the sentence above, the user assign the "Italian" keyword to the "Pizza" entity. Then they just assign a output response for such a input. The frontend should let the user performs these tasks such as highlighting a keyword, assigning to entities, showing lists of entities to the user. Since the algorithms that I am developing are in Python, I was thinking about using Django. Do you think it is the best platform for the such a task? I really appreciate your help. Amir -
Paypal Transactions: How to receive and send funds with PayPal API and Django?
i've been wondering to make payment system for my Django website, but with PayPal it seems to be easier for users, i've heard there is special API for python, so it can be used with Django. So for example, There is user account and my account, i make a receiver function, which listens to the payment gateway and whenever user sends funds to the email, it does a specific commands. Now i also make a sender function, so i can send funds to users, Now if API get's users email whenever they see HTTP Response of Paypal paying window, i want to make a function to send it to specific email. Also another example with code: import random from process import multiprocessing Users = [ "..." ] # PayPal: import SomePayPalApi def Receiver(): SomePayPalApi.receive_to = SomePayPalApi.current_user.email while True: if SomePayPalApi.recieved: print SomePaypalApi.recieved.amount print SomePaypalApi.received.email print SomePaypalApi.Balance received = True break def Sender(): SomePayPalApi.sendto = random.choice(Users) SomePayPalApi.send() if __name__ == "__main__": p1 = Process(target=Receiver) p2 = Process(target=Sender) p1.start() p2.start() I know it's not as easy as example, but i am looking for two general functions of PayPal, Paying and Receiving, How can i achieve this API on Django? Also if possible, … -
ModelSelect2MultipleField not defined
Does anybody know where ModelSelect2MultipleField is imported from? I've been trying to import it into a file that I need, and I thought that I could do it with from django_select2.fields import ModelSelect2MultipleField but I keep getting errors. Before when I had it from django_select2 import * It said that ModelSelect2MultipleField, which I use in the py file, was not defined. Now it says that there is no module named fields. The .py file I'm talking about can be found here (https://bitbucket.org/cbplusd/wibo/src/efc144e1bdc1eb52f76591c60ff0a1d40464b0ed/reports/forms.py?at=master&fileviewer=file-view-default). The Django-Select2 I have installed is version 5.8.9, so is there another way to import ModelSelect2MultipleField other than this way? Thanks in advance! -
filter model and show result without page refreshing without AJAX
Is there a way to filter rendered model and show the result without page refreshing using only Django features(without ajax etc)? -
import python packages from another directory into a Django project
I have a Django project where a user uploads some images and I have some image processing routines which I write and they reside in a completely different folder in my hard drive (on the same machine). Now, I plan to use something like celery to process these images. So, the idea would be that as soon as the files get uploaded, I can start some celery task which would process these images. Now, I was wondering how I can change my Django settings so that these image processing routines are available from within my Django project? So, my image processing project has the following structure: ip --- calibration --- io --- report --- utils So from my django project, I hope to be able to do something like: from io import * and be able to use the classes defined there. -
Checking that an object exists in DB (Django)
Consider this Django code: class User(models.Model): name = models.CharField(null=True, blank=False, verbose_name=_("Name"), help_text='User Name', max_length=256) class UsersGroup(models.Model): name = models.CharField(null=False, blank=False, verbose_name=_("Name"), help_text='Users Group Name', max_length=256) users = models.ManyToManyField(User) subgroups = models.ManyToManyField(UsersGroup, symmetrical=False, related_name='supergroups') # ... with transaction.atomic(): group.users.add(user) What if the user was deleted from the DB before the transaction starts? It would add an nonexistent user to group.users. This is an error. What to do in this situation to preserve DB integrity? -
Resize image in post_save then read the resized image in the same signal
I resize an image once its uploaded in the post_save. Right after that I then want to create a base64 string from the image, and save it again. But it sais the newly resized image does not exists when I try an open the file? @receiver(post_save, sender=Guard) def update_image(sender, instance, created, **kwargs): post_save.disconnect(update_image, sender=Guard) THUMBNAIL_SIZE = (200, 200) image = Image.open(os.path.join(settings.MEDIA_ROOT, instance.mugshot.name)) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) # Save the thumbnail temp_handle = StringIO() image.save(temp_handle, 'png') # image stored to stringIO temp_handle.seek(0) # sets position of file to 0 # Save to the thumbnail field suf = SimpleUploadedFile(os.path.split(instance.mugshot.name)[-1], temp_handle.read(), content_type='image/png') # reads in the file to save it instance.mugshot.save(''.join(suf.name.split('.')[:-1])+'.png', suf) instance.save() with open(instance.mugshot.url, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) instance.base64 = encoded_string instance.save() post_save.connect(update_image, sender=Guard) -
How do you display fields from a related model in admin.py?
I have the following models: class Property(models.Model): user = models.ForeignKey(User) id = models.CharField(max_length=20, null=True) class Property_Value(models.Model): id = models.ForeignKey(Property) amount = models.DecimalField(max_digits = 10, decimal_places How do I access the Property_Value.amount via Property on the admin page? I have this so far... class PropertyAdmin(admin.ModelAdmin): list_display = ('id', 'user', 'property_value') def property_value(self, obj): return obj.id.amount class Meta: model = Property admin.site.register(Property, PropertyAdmin) -
how to use get_object_or_404 with order_by('?) to get random image
I want to get random object from model but if there are no data in database I want to return 404 page. This like of code works for me well: dummy_image=DummyImage.objects.order_by('?').first().image_url.url but I want to use this python shortcut to get obect or 404 so I tried this: dummy_image = get_object_or_404(DummyImage).order('?').first().image_url.url but It dosent't work and cause issues. It says that it returned more than two object(?) How do I solve the problem? -
How to solve this error Nginx and uwsgi 502 bad gateway
I faced 502 BadGateway error. How to solve this? I catched nginx access log and error log access log 120.142.184.164 - - [27/Sep/2016:16:15:48 +0000] "GET / HTTP/1.1" 502 583 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" error log 2016/09/27 16:15:48 [crit] 3585#0: *1 connect() to unix:/tmp/PROJECT_NAME.sock failed (13: Permission denied) while connecting to upstream, client: 120.142.184.164, server: 52.78.105.246, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/PROJECT_NAME.sock:", host: "52.78.105.246:8000" nginx config file server { listen 8000; server_name 52.78.105.246; access_log /var/log/nginx/example_access.log; error_log /var/log/nginx/example_error.log; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/ghdalsrn/PROJECT_NAME; } location / { include uwsgi_params; uwsgi_pass unix:/tmp/PROJECT_NAME.sock; } } uwsgi config file [uwsgi] vhost=true project = PROJECT_NAME base = /home/ghdalsrn plugin = python chdir = %(base)/%(project) home = %(base)/.virtualenvs/ENV_NAME module = %(project).wsgi:application enable-threads = true master = true processes = 5 socket = /tmp/%(project).sock chmod-socket=666 vacuum = true I using django, and nginx's configtest says OK, not fail. I'm even as root user, this error occurred always. nginx/1.4.6// Ubuntu 14// and pip3 freeze to version chardet==2.2.1 colorama==0.2.5 command-not-found==0.3 html5lib==0.999 language-selector==0.1 pbr==1.10.0 pycurl==7.19.3 pygobject==3.12.0 python-apt==0.9.3.5ubuntu2 requests==2.2.1 six==1.10.0 stevedore==1.17.1 uWSGI==2.0.13.1 ufw==0.34-rc-0ubuntu2 unattended-upgrades==0.1 urllib3==1.7.1 virtualenv==15.0.3 virtualenv-clone==0.2.6 virtualenvwrapper==4.7.2 wheel==0.24.0 How can i solve it … -
How to deploy Django using virtual node of apache and virtual python environment?
I have to deploy my Django application on a server which I have limited access to its apache web server. Each student have access to a folder which is a Virtual Host Node with this structure: student/ cgi-bin/ html/ data/ Folder data is for files which are not reachable by http. I've created my virtualenv "env" here. "html" folder is the root document, reachable using http request/responses. For example there's html/index.html which loads when I write my url in the browser. Also there's a wsgi file by default here with following directive: WSGIScriptAlias /wsgi /home/web/student/html/wsgi.wsgi And finally I've created the django project here. and I don't have permission to write to /etc/apache2/sites-enabled. So the final structure of student/ is as follow: student/ cgi-bin/ html/ index.html wsgi.wsgi my_project/ my_project/ ... wsgi.py data/ env/ So I have to modify html/wsgi.wsgi to point to wsgi.py of my django project and also use my virtualenv as python path. I've modified it as follow: WSGIScriptAlias / /home/web/student/html/my_project/my_project/wsgi.py process-group=my_project WSGIDaemonProcess my_project python-path=/home/web/student/html/my_project:/home/web/student/data/env/li/python2.7/site-packages WSGIProcessGroup my_project <Directory /home/web/student/html/my_project/my_project> <Files wsgi.py> Require all granted </Files> </Directory> But it still shows that index.html as first page, instead of "it worked" page of django. -
Creating time sensitive URL links using Itsdangerous module
I'm using Itsdangerous module in django to create a single-use URL. It also should be time sensitve. Reading the documentation, I see that there are two different classes: TimestampSigner and URLSafeSerializer. The first one creates a signature with expiration deadline. The second one works as a URL serializer, and it should be used on "environments where you only have a limited set of characters available". There's also another class, TimedJSONWebSignatureSerializer, that generates a signature which includes a timestamp (See this link). The token it generates is not static and I can set an expiration date. However, it may be too long for the usage as an URL link. The deal is, I'm worried about the size of the generated token, but it is a requirement that it should be unique and also expire after 1 day. How can I do this? -
Can't upload media files from elastic beanstalk/ec2 server
Hey I've been stuck on a problem for a while now where I can upload media files from my localhost to AWS S3, but when I deploy to EB/EC2 I'm getting a 504 gateway timeout when uploading media files. On my development server the uploads work fine and are stored in s3 without any problem. I'm currently using django-storages but I have tried django-storages-redux and django-s3-storages and I have the same problem where it works on my localhost but on my deployed server I get the 504 Gateway Timeout and the file is not uploaded to s3. I have my settings files broken out into dev and prod. In my prod settings I have the following: # S3 FILE SETTINGS AWS_STORAGE_BUCKET_NAME = 'buttercast-assets' AWS_S3_CUSTOM_DOMAIN = 'cdn.buttercast.com' ASSETS_PREFIX = 'app' # prefix in the bucket MEDIA_PREFIX = ASSETS_PREFIX + '/uploads/' DEFAULT_FILE_STORAGE = 'core.storages.MediaStorage' STATICFILES_STORAGE = 'core.storages.StaticStorage' To test on development I simply move these settings to the bottom of my dev settings. I have these custom prefixes to store it in a specific directory on s3. # storages.py from django.conf import settings from storages.backends.s3boto import S3BotoStorage class StaticStorage(S3BotoStorage): def __init__(self, *args, **kwargs): kwargs['location'] = settings.ASSETS_PREFIX return super(StaticStorage, self).__init__(*args, **kwargs) class MediaStorage(S3BotoStorage): … -
using django rest framework to return info by name
I am using Django rest framework and I create this class to return all the name of project class cpuProjectsViewSet(viewsets.ViewSet): serializer_class = serializers.cpuProjectsSerializer def list(self, request): all_rows = connect_database() name_project = [] all_projects = [] for item_row in all_rows: name_project.append(item_row['project']) name_project = list(sorted(set(name_project))) for i in range(0, len(name_project)): all_projects.append({'project' : str(name_project[i])}) serializer = serializers.cpuProjectsSerializer(instance=all_projects, many=True) return Response(serializer.data) my URL is like that http://127.0.0.1:8000/cpuProjects/ this return all the projects, buy If now I want a particular project, Have I to create a new class?? if I want to use the same URL ... for example http://127.0.0.1:8000/cpuProjects/ => return all project http://127.0.0.1:8000/cpuProjects/nameProject => return a particular project. Is it possible to do in the same class? I try to use retrieve method but the need an ID of the project, no the name right? thanks in advance! -
Why does Django [Rest Framework] pass the data through OrderedDict and back, isn't it just a waste of compute?
After reading this blog post, I looked into using .values() and skip DRF serialization altogether. So now I'm curious, the natural representation is in a dictionary that looks so close to proper JSON, why bother with DRF at all? This is the natural representation from .values(): [ {'type': 'Zone', 'many': False, 'id': 1, 'rank': 0}, ] And this is after going all the way through the Django ORM and a serializer: [ OrderedDict( [ ('id', 1), ('type', 'Zone'), ('many', False), ('rank', 0) ] ), ] And this is the final valid JSON output (changed F to f, should've just used 0 and 1): [ {'type': 'Zone', 'many': false, 'id': 1, 'rank': 0}, ] Obviously this is for gets, I will use the serializers to validate posts. What is the point of doing things the DRF way with the APIView, Serializer, and Response? I don't use throttling, and permissions/authentication can be easily implemented without DRF with far better readibility. So right now it seems useless to use DRF for gets, why not just use a normal django view with json.dumps or JSONEncoder? -
ManyToMany Relationships. Returning fields in def __str__ method
I have two models: AffectedSegment model class AffectedSegment(models.Model): SEGMENTO_ESCAPULA = 'Escápula' SEGMENTO_HOMBRO = 'Hombro' SEGMENTO_CODO = 'Codo' SEGMENTO_ANTEBRAZO = 'Antebrazo' SEGMENTO_CARPO_MUNECA = 'Carpo/Muñeca' SEGMENTO_MANO = 'Mano' SEGMENT_CHOICES = ( (SEGMENTO_ESCAPULA, u'Escápula'), (SEGMENTO_HOMBRO, u'Hombro'), (SEGMENTO_CODO, u'Codo'), (SEGMENTO_ANTEBRAZO, u'Antebrazo'), (SEGMENTO_CARPO_MUNECA, u'Carpo/Muñeca'), (SEGMENTO_MANO, u'Mano'), ) affected_segment = models.CharField( max_length=12, choices=SEGMENT_CHOICES, blank=False, verbose_name='Segmento afectado', help_text='Ingrese uno o mas segmentos corporal de miembros superiores' ) class Meta: verbose_name_plural = 'Segmentos corporales' def __str__(self): return "%s" % self.affected_segment And Movement model. Please review the __str__ method of this model at the end: class Movement(models.Model): type = models.CharField( max_length=255, verbose_name='Tipo de movimiento' ) corporal_segment_associated = models.ManyToManyField( AffectedSegment, blank=True, verbose_name='Segmento corporal asociado') class Meta: verbose_name = 'Movimiento' def __str__(self): return "{},{}".format(self.type, self.corporal_segment_associated) I have another model named RehabilitationSession in which I want allow the possibility of choose multiple affected_segments and multiple movements such as follow: class RehabilitationSession(models.Model): affected_segment = models.ManyToManyField( AffectedSegment, verbose_name='Segmento afectado', #related_name='affectedsegment' ) movement = models.ManyToManyField( Movement, # Modelo encadenado verbose_name='Movimiento', #related_name = 'movement' ) In my Django Admin when I go to the RehabilitationSession model I see the following in the movement field: I understand that in the ManyToMany relationships, a intermediate table is created with the ID's fields of the two tables which make … -
Use admin login form then redirect back to page
Right now I have an app here: htttp://localhost/main Which requires a login for django's authentication. I want to use the admin login form http://localhost/admin/login. The problem is that by default the admin login redirects back to itself, but I want it to redirect back to http://localhost/main. Is there a way I can get this done? This is my view currently @login_required(login_url='/admin', redirect_field_name='main' def main_view(request): return render(request, 'main_site.html') By doing this, it just redirects me to the following URL: http://localhost/admin/login/?next=/admin/%3Fmain%3D/main/ But I need it to be http://localhost/admin/login/?next=/main/ -
Documentation: both data and initial for formset
Django 1.10. https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#can-order https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#can-delete The examples at both of the abovementioned links are as follows: >>> data = { ... 'form-TOTAL_FORMS': '3', ... 'form-INITIAL_FORMS': '2', ... 'form-MAX_NUM_FORMS': '', ... 'form-0-title': 'Article #1', ... 'form-0-pub_date': '2008-05-10', ... 'form-0-DELETE': 'on', ... 'form-1-title': 'Article #2', ... 'form-1-pub_date': '2008-05-11', ... 'form-1-DELETE': '', ... 'form-2-title': '', ... 'form-2-pub_date': '', ... 'form-2-DELETE': '', ... } >>> formset = ArticleFormSet(data, initial=[ ... {'title': 'Article #1', 'pub_date': datetime.date(2008, 5, 10)}, ... {'title': 'Article #2', 'pub_date': datetime.date(2008, 5, 11)}, ... ]) What troubles me: why should we need initial data here? It overburdens the documentation as we don't use it initial data in the example. The only case when we need both data and initial is when we use has_changed(). I'm a newbie. Maybe I don't understand that deep. How do you think, maybe I should draw the attention of the community at Djangoproject to this problem? -
Access vales of objects via index in Django
is there a way to access a value of an objectvia indexing without the use of dict()? This is my model: class state(models.Model): statecode = models.CharField(max_length=2, default='XX') statename = models.CharField(max_length=32, default='XXXXXXXXXXXXX') When I start the shell and create a new object... >>> st=state(statecode="BO",statename="BOSTON") ...my object looks like this: >>> st <state: BOSTON, BO> What I now want is to access the statename via an index, so instead of >>> st.statename 'BOSTON' I want to access it with something like this: >>> st[1] but currently I'm getting the follow error: TypeError: 'state' object does not support indexing Any ideas? -
Reusing Django Cursor for raw queries
I have a little application which is supposed to work as a querying service layer to many SQL databases. Based on a parameter in the request body the application decides which DB the query should be executed on. Hence, it obtains a cursor to the specific DB. After executing the query, it closes the cursor. Here is the code def get_cursor(request_body): cursor = None if request_body['DB'] >= 'xyz': cursor = connections["hotDB"].cursor() else: cursor = connections["coldDB"].cursor() return cursor There is no model in the application. All the queries are raw queries. Obtaining the cursor and closing it are expensive operation and makes the request take long time. Is there a way to cache the cursor and reuse it for later requests. -
How to import GeoIP2 successfully into a Django project
I'm trying to use GeoIP2 to determine lat/lng of all of our users based on their IP address. In my device/views.py I've got: from django.contrib.gis.geoip2 import GeoIP2 and then at the bottom of base.py: GEOIP_PATH = os.path.join(BASE_DIR, 'geoip') GEOIP_COUNTRY = 'GeoLite2-Country.mmdb' GEOIP_CITY = 'GeoLite2-City.mmdb' And here's a screen shot of the errors I'm getting: Anyone have any brilliant ideas?