Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to use Celery to do asynchronous tasks AWS EC2?
We set up an asynchronous task using Django, celery and rabbitMQ. Here our goal is to perform tasks like video transcoding, Thumbnail generation, Image compression and Image scaling altogether We followed the below tutorial to implement it and the asynchronous task is working properly in Localhost. For the production compatibility, we used supervisord to start celery workers and to restart in case of system reboot or crash. http://michal.karzynski.pl/blog/2014/05/18/setting-up-an-asynchronous-task-queue-for-django-using-celery-redis/ When we tried to implement same in AWS EC2, we are unable to install and configure Supervisord in AWS EC2.so we have to start celery manually every time. Can anyone please help us how to install and how to handle this procedure on AWS EC2 The code we are using task.py from __future__ import absolute_import from test_celery.celery import app import time @app.task def longtime_add(x, y): print 'long time task begins' # sleep 5 seconds time.sleep(5) print 'long time task finished' return x + y celery.py from __future__ import absolute_import, unicode_literals import os from celery import Celery import afnity.settings as settings from django.conf import settings os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') app = Celery('taskapp') app.config_from_object('django.conf:settings') if __name__ == '__main__': app.start() settings.py BROKER_URL = 'amqp://guest:guest@localhost//' CELERY_RESULT_BACKEND = 'redis://%s:%d/%d' % ('localhost', 6379, 0) -
django orm latest item group by each foreign key
We have following models: class Publisher(models.Model): name = models.CharField(max_length=32) local_name = models.CharField(max_length=32) url_pattern = models.CharField(max_length=128) enabled = models.BooleanField(default=True) home_page = models.BooleanField(default=False) category = models.ForeignKey(NewspaperCategory, null=True) class Newspaper(models.Model): class Meta: unique_together = ("publisher", "date") ordering = ("-date",) publisher = models.ForeignKey(Publisher) image = models.ImageField(upload_to=newspaper_upload_to) thumbnail = models.ImageField(upload_to=newspaper_thumbnail_upload_to) date = models.DateField() I have a APIView (Django rest framework) and there are several different query parameters which filter the output of API , I'd like to have a latest query parameter which list only the latest version of each publisher, also I need to be able to do further filtering and slicing on that queryset before evaluating it . but the result of query should be Newspaper instances not dict so I can feed them to my serializer . -
ImportError: No module named 'env.db' while running django server
When I only created django project and ran server everything worked, but after I started app and ran server again this error occured: Unhandled exception in thread started by <function check_errors. <locals>.wrapper at 0x7f09bff60730> Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 115, in load_backend return import_module('%s.base' % backend_name) File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked ImportError: No module named 'env.db' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 227, in wrapper fn(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/runserver.py", line 117, in inner_run autoreload.raise_last_exception() File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", … -
python - Directly Send Files to S3 From Django on Heroku
So I'm trying to uploaded larger files to my site, and due to Herokus limitations with the sizes, I'm trying to upload them directly to S3. Heroku provides great documentation on how to do so, seen here. I'm following along with the guide, and adjusting my views.py based on this Git. The problem is my signing request doesn't work when trying to post to S3. I get the error The request signature we calculated does not match the signature you provided. Check your key and signing method. So i am unsure of how my signature is wrong or if there is something wrong in my javascript. Any help would be appreciated. My views.py def sign_s3(request): """ https://devcenter.heroku.com/articles/s3-upload-python """ if request.user.is_authenticated(): user = request.user.id AWS_ACCESS_KEY = AWS_ACCESS_KEY_ID AWS_SECRET_KEY = AWS_SECRET_ACCESS_KEY S3_BUCKET = AWS_STORAGE_BUCKET_NAME object_name = urllib.parse.quote_plus(request.GET['file_name']) mime_type = request.GET['file_type'] secondsPerDay = 24*60*60 expires = int(time.time()+secondsPerDay) amz_headers = "x-amz-acl:public-read" string_to_sign = "PUT\n\n%s\n%d\n%s\n/%s/%s" % (mime_type, expires, amz_headers, S3_BUCKET, object_name) encodedSecretKey = AWS_SECRET_KEY.encode() encodedString = string_to_sign.encode() h = hmac.new(encodedSecretKey, encodedString, sha1) hDigest = h.digest() signature = base64.encodebytes(hDigest).strip() print(signature) signature = urllib.parse.quote_plus(signature) print(signature) url = 'https://%s.s3.amazonaws.com/media/user_%s/%s/' % (S3_BUCKET, user, object_name) return JsonResponse({ 'data': '%s?AWSAccessKeyId=%s&Expires=%s&Signature=%s' % (url, AWS_ACCESS_KEY, expires, signature), 'url': url, }) My javascript: function … -
Efficient way to serve Django REST APIs
APIs built on Django Rest Framework. Few points before my question. I have a Employee model which is linked to seven models with different relationships among themselves. I have a seperate API for employee model I have separate APIs for each of those seven models which uses HyperLinkedModelSerializer. Now for an app calling my APIs has to make 8 requests to get all the information regarding a employee. Is this the REST compliant way to do it? or should I send all the data in a single API using ModelSerializer? or suggest me if there is another way to make the APIs more efficient. -
How to dedect image upload through api in input feild of htm?
I have a project where users have their profile and values are coming from API.I am using Django as a language. So when I add my profile photo it is working fine with the current code.And when I go to edit page of the same user it display my current image and when I again hit the save button it detect no image. my html with api value in src:- <div class="main-img-preview margin_bottom10"> <img class="thumbnails img-preview" src="{% if result_data_for_editing.profileImage.mediaPath != None %}{{ result_data_for_editing.profileImage.mediaPath }} {% else %}{{ '../assets/images/default-user-image.png' }}{% endif %}" title="Preview Logo" accept="image/*" > </div> <div class="input-group"> <div class="input-group-btn"> <div class="fileUpload btn btn_teal btn_raised text-uppercase fake-shadow"> <span>Browse</span> <input id="logo-id" name="media" type="file" class="attachment_upload" required=""> </div> </div> </div> </div> my script is this:- $(document).ready(function () { var brand = document.getElementById('logo-id'); brand.className = 'attachment_upload'; brand.onchange = function () { document.getElementById('fakeUploadLogo').value = this.value.substring(); }; function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('.img-preview').attr('src', e.target.result); }; reader.readAsDataURL(input.files[0]); } } $("#logo-id").change(function () { readURL(this); }); }); how can I dedect the image from API into my input feild.PLease ignore gramatical mistakes if their are any. THanks in advance -
How to pass all the data in dataframe that extract from excel sheet to highchart?
I have a raw data and right now I need to used the raw data to plot a highchart and pass to Django, any one can share me the basic how to plot a highchart in order to shown in HTML page? I'm very new to python, Django and Highchart, I have read through all the related material on Highchart but I still not understand and not able to start it. From this https://www.highcharts.com/demo/line-basic I able to contract a chart, but in my case I need extract all the data and plot a chart. I try apply {% block content%} and the title all are base on my raw data name in a dataframe but I still not able to build a chart -
Save data with current user logged-in Django Admin
So I have this model named Report. It has a column that is linked to User model. And I have set up the authentication of users to be able to add his own report. Obviously, the authentication I've made was to only allow users to add his own report. Since the Report model is linked to User model, he can also select what user to be linked into the report he will add. Is it possible to customize this User field that it will automatically get the current logged user after creating a report? What I've done so far: I've created a dashboard where a user can log in. Created a template for the Report App to be able the user to add their own report. Set the proper authentication for users. What I don't really like about this is I can't find a way to re-use the pop-up add & edit. I found this link but haven't tried it since it seems it's already outdated. -
Authentication failed with django and mongodb
I need a little bit of help. I'm a student and I got an assignment with NoSQL databases, we need to use MongoDB (we need to do a LinkedIn sort of web app). I decided to used django since I'm trying to learn it and I've been finding some issues trying to link MongoDB to my django app. OS: Windows 8.1 Django version: 1.10 MongoDB version: 3.0.4 (since I have a 32-bits laptop) I've followed different tutorials and can't seem to get this running. I'm also using PyMongo 2.8 and Mongoengine 0.9. This is how I have my settings.py file: """ Django settings for mysite project. Generated by 'django-admin startproject' using Django 1.10.6. """ import os from mongoengine import * import pymongo # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '85!^dm*y!!wrinm@ihd_342#^3m%yab)pj3(fo99j&1iv6^my!' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] # Attempt to connecto to MongoDB MONGOADMIN_OVERRIDE_ADMIN = True MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] … -
Django Alert box
I want make alertbox(javascript alert()) in django template. When login error(wrong password or username), run the alertbox and refresh... or when register complete, run the alertbox(register success) and redirect. [% if messages %} <div class="javascript:alert"> {{ somemessage }} </div> {% endif %} like this. Thanks. -
Django, how display thing from others views in template?
I have views game, edit_rate, etc. def game(request, slug, game_id): ... return render(request, 'game.html', args) def edit_rate(request, slug, game_id, rate_id): .... return render(request, "edit_rate.html", args) I must use rate_id in game.html because I need it in: <a href="/games/{{game.id}}/{{game.slug}}/add_rate/edit/{{?rate_id?}}" </a> How I can do it? By the comment I used {% for x in game.comment_set.all %} {{x}} {% endfor %} but I don't want loop. I need one thing, I tried do this with _set, but didn't work, and I didn't find other examples with _set Rate model: game = models.ForeignKey(Games) user = models.ForeignKey(User) rate = models.IntegerField() So, how I can use rate_id in my game.html? -
Pass Django Model Object into Template Tag
I created a custom template tag to query a list of objects, but each object has a tag associated with it. I would like to pass an object as a filter into my template tag to display only certain tagged objects in my template. Template Tag @register.inclusion_tag( 'tags/_documents_snippets.html', takes_context=True ) def document_snippets(context): Document = get_document_model() documents = Document.objects.all() return { 'documents': documents, 'request': context['request'], } Template <div class="col-md-12"> <ul class="c-content-list-1 c-separator-dot c-square"> {% for doc in documents %} <li><a href="{{ doc.url }}">{{ doc.title }}</a></li> {% endfor %} </ul> </div> Tag {% document_snippets %} Can I do something like {% document_snippets|tags="AO Now" %} -
Unknown view function
I have a form that should redirect to another view function called content to predict. I receive an error saying that the form doesn't exist when it does as shown in my code: def content_to_predict(request): if request.method == "POST": form = InputForm(request.POST) if form.is_valid(): return redirect('content_to_predict') else: form = InputForm() return render(request, 'prediction/content_input.html', {'form': form}) def show_prediction_result(request): return HttpResponse('hello') What's the problem? -
Two endpoints for the same resource in django rest framework
I want to create two endpoints /comments/ and /comments/requests/ or something to that effect. The first shows your comments, and the second shows your pending comments (Comments that people sent you that you need to approve). They both work with a comments model. How could I achieve this in Django Rest Framework? Right now, my view is class CommentsListview(APIView): serializer_class = CommentSerializer def get(self, request, format=None): comments, _, _, = Comments.get_comment_users(request.user) comments_serializer = CommentSerializer(comments, many=True) return Response({'comments': comments_serializer.data}) def requests(sel,f request, format=None): _, requests, _ = Comments.get_comment_users(request.user) requests_serializer = CommentSerializer(requests, many=True) return Response({'requests': requests_serializer.data}) I'd like to allow a user to go to localhost:8000/comments/ to view their comments and localhost:8000/comments/requests/ to view their pending comment requests. Since I haven't been able to figure this out, the only other sollution would be to require the user to switch the behavior of the endpoint using a parameter as a flag /comments/?requests=True but that just seems sloppy. -
python code for pinging servers in the network and marking unresponsive servers as down
can anyone suggest me a python code for the below scenario Each server in the network regularly sends a message to each of the other servers. The recipient must acknowledge the reception of the message. If a recipient fails to do this multiple times in a row (for example, 3 times), the server is marked as down -
How do you return result after entering text in form in django?
As an exercise I'd like to write a web app that returns keyword prediction after user enters content in the form. So, if a user enters some "text" in form, then below the form in, the keyword should appear. I'm thinking that one approach could be using redirect to another view function that produces the prediction model function in view then the returned result would be a revised template. But, I'm not too sure about the following: Could multiple functions be placed in the view under the same url patterns? Is there an if condition that can be applied on the same template holding the form such that if the user submits, then the template shows results? View: def content_to_predict(request): if request.method == "POST": form = InputForm(request.POST) if form.is_valid(): return redirect('show_prediction_result') else: form = InputForm() return render(request, 'prediction/content_input.html', {'form': form}) Template: <h1> Input Content: </h1> <form method="POST" class="post-form">{% csrf_token %} {{ form.as_p }} <button type="submit" class="save btn btn-default">Save</button> </form> -
Should default Debian time zone: 'Etc/UTC be changed?
Should the default Debian time zone: 'Etc/UTC be changed fo all practical purposes including administration or development using Django or other frameworks? -
Django, how do it in template?
{% if request.user??? %} <h2><a href="/games/{{game.id}}/{{game.slug}}/add_rate">Add rate</a></h2> {% else %} <h2><a href="/games/{{game.id}}/{{game.slug}}/add_rate/edit/{{rate.id}}/">Edit rate</a></h2> {% endif %} I want to this : if user have rate for this game display "Edit rate" else " Add rate" How do this? -
py2neo connection refused while docker-compose up
I am trying to run a Django app which connects to neo4j database using py2neo library. Its running fine on my my local machine. But when I am trying to get it docerized using docker-compose I am getting bellow error again and again. This is my docker-compose.yml file version: '2' services: db: image: postgres neo4j: image: neo4j ports: - "7474:7474" - "7687:7687" volumes: - ./db/dbms:/data/dbms web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db - neo4j links: - neo4j Dockerfile: FROM python:3 FROM neo4j:3.1 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/ ADD startneo.sh /startneo.sh CMD ["/start.sh"] views.py import os import json # Create your views here. from py2neo import Graph, authenticate from bottle import get,run,request,response,static_file graph = Graph(password='neo4j') @get("/") def get_index(): return static_file("index.html", root="static") @get("/graph") def get_graph(self): print("i was here" ) print("graph start") results = graph.run( "MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) " "RETURN m.title as movie, collect(a.name) as cast " "LIMIT {limit}", {"limit": 10}) print("graph run the run") nodes = [] rels = [] i = 0 for movie, cast in results: #print("i am here") nodes.append({"title": movie, "label": "movie"}) target = i i … -
What are the possible ways of extracting text from a news article?
I'm developing a Web app for my school project by implementing Django and NewsAPI so far. The NewsAPI returns me JSON response including the heading, the source URL and a few lines of description for the news while I need full text of article as next step for my detailed view page. So, after a research I found ArticleAPI very useful where I can extract full text of article by just giving the source URL I got from NewsAPI. Yet, since ArticleAPI is neither open-source nor free-to-use, I need another solution. Do you have any suggestions? Thanks in advance. -
django heroku makemigrations ignoring changes in models
I deleted two models from models.py, and when I run makemigrations and migrate locally, everything is fine. When I run makemigrations on Heroku, I get the following message, where Building and BuildingInstance are the models I deleted: Migrations for 'hello': 0002_building_buildinginstance.py: - Create model Building - Create model BuildingInstance When I run migrate, I get: Running migrations: No migrations to apply. Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them. I followed the steps here and also tried squashing the migrations locally and on Heroku as suggested here. How can I fix this problem? -
What is the pythonic way of building full urls for links?
I'm looking for a way to build urls in python3 without having to do string concatenation. I get that I can import requests url_endpoint = 'https://www.duckduckgo.com' mydict = {'q': 'whee! Stanford!!!', 'something': 'else'} resp = requests.get(url_endpoint, params=mydict) print(resp.url) # THIS IS EXACTLY WHAT I WANT or from requests import Request, Session s = Session() req = Request('GET', url, params={'q': 'blah'}) print(req.url) # I didn't get this to work, but from the docs # it should build the url without making the call or url = baseurl + "?" + urllib.urlencode(params) I like that the request library intelligently decides to drop ? if it isn't needed, but that code actually makes a full GET request so instead of just building a full text url (which I plan to dump to an html tag). I am using django, but I didn't see anything to help with that in the core library. -
Django deployment best practices
Safaribooksonline.com has a video [1] from Jacob Kaplan-Moss about how to deploy a Django app. This video is from 2010. It refers to a site [2] that has a list of many relevant aspects. Now the workshop mentions things like virtual machines, vagrant (as deployment environments) or Fabric and other tools for deployment automation. I was wondering how much has changed since then. I can think of Docker replacing Vagrant. Or Heroku or AWS instead of renting a dedicated physical server for deployment (or virtual machines). Or using Ansible or Chef / Puppet instead of Capistrano or Fabric. But what else has changed? What is still relevant? What is done differently? What is the state of the art in 2017 for deploying a production ready Django app. Can anybody point me to good blogs / books / tutorials? [1] "Django deployment workshop", https://www.safaribooksonline.com/library/view/django-deployment-workshop/9781449396442/ [2] "infrastructure of modern websites", https://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites -
How do you nest url mapping in webapp2.WSGIApplication
I'm writing a webapp2 application and am trying to figure out out to nest url mappings. The application is broken up into several packages, and I'd like each package to be able to specify it's own url mappings similar to the way Django does with it's include() directive. Copying from the Django documentation this would look like: urlpatterns = [ # ... snip ... url(r'^community/', include('django_website.aggregator.urls')), url(r'^contact/', include('django_website.contact.urls')), # ... snip ... ] Would this need to be specified in app.yaml, or is there a way to specify the inclusion in webapp2.WSGIApplication([]) -
ValueError: Empty module name when running Python Django Project Locally
I took over this Django project and am trying to get it to run locally. Problem is that whenever I run it locally (python manage.py runserver), I get this error: ValueError: Empty module name Here is the full traceback: /Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/environ/environ.py:579: UserWarning: not reading /Users/pauljurczyk/BandyApp/BandyBack/Bandy-django-new-master/.env - it doesn't exist. warnings.warn("not reading %s - it doesn't exist." % env_file) /Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/environ/environ.py:579: UserWarning: not reading /Users/pauljurczyk/BandyApp/BandyBack/Bandy-django-new-master/.env - it doesn't exist. warnings.warn("not reading %s - it doesn't exist." % env_file) Unhandled exception in thread started by Traceback (most recent call last): File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper fn(*args, **kwargs) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run autoreload.raise_last_exception() File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/django/utils/autoreload.py", line 252, in raise_last_exception six.reraise(*_exception) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper fn(*args, **kwargs) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/django/init.py", line 18, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate app_config = AppConfig.create(entry) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/django/apps/config.py", line 86, in create module = import_module(entry) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py", line 37, in import_module import(name) File "bandy/apps/taskapp/init.py", line 1, in from .celery import app as celery_app File "bandy/apps/taskapp/celery.py", line 15, in app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, force=True) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/celery/app/base.py", line 322, in autodiscover_tasks return self._autodiscover_tasks(packages, related_name) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/celery/app/base.py", line 330, in _autodiscover_tasks self.loader.autodiscover_tasks(packages, related_name) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/celery/loaders/base.py", line 252, in autodiscover_tasks related_name) if mod) File "/Users/pauljurczyk/BandyApp/BandyBack/eb-virt/lib/python2.7/site-packages/celery/loaders/base.py", line 273, in autodiscover_tasks return …