Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django TemplateDoesNotExist at account/signup/
Can someone please help me? I created a sign up form under: blog/templates/regitsration/signup.html blog/views.py blog/urls.py blog/forms.py The site isn't working correctly. It says: TemplateDoesNotExist at accounts/signup/ registration/signup.html The files are at https://github.com/VictorLeRed1275/my-first-blog and https://victorlered.pythonanywhere.com -
Django REST Framework creating foreign key object inside serializer
Hello I'm new to REST Framework. I'm trying to make my serializer create new Town if it doesn't exist when i'm creating the new shop. class ShopSerializer(serializers.HyperlinkedModelSerializer): town = serializers.SlugRelatedField(slug_field='town', queryset=Town.objects.all()) # town_send = serializers.CharField(max_length=50, write_only='True') class Meta: model = Shop fields = ('name', 'address', 'opening_hour', 'closing_hour', 'owner', 'geo_point', 'town', 'url') read_only_fields = ('owner',) def create(self, validated_data): request = self.context['request'] owner = request.user town = validated_data.pop('town') town, exist = Town.objects.get_or_create(town=town) shop = Shop.objects.create(owner=owner, town=town, **validated_data) return shop Currently it updates if I send it existing town name, but when i'm giving it new town it shows: "Object with field town=x does not exist" -
A Django QuerySet that always returns iterator()
Is it possible to define a custom QuerySet, that will always return the output of .iterator() when it is iterated, while keeping all the other QuerySet functionality? Wrapping an existing QuerySet would work, too. Currently, the only solution I came up with is this awful wrapper class: class IteratorQuerySet: def __init__(self, qs): self._qs = qs def __iter__(self): return self._qs.iterator() def __getitem__(self, *args, **kwargs): return self._qs.__getitem__(*args, **kwargs) def __getattr__(self, attr): return getattr(self._qs, attr) The downside is, that it is not really a QuerySet, but simply forwards all attributes and getitem calls to the qs. What different angles could I try? -
I can not register my Django project on Heroku
I updated the python version from 3.6.4 to 3.6.5 today. This is because, in the process of distributing to Heroku, it recommends version 3.6.5. Therefore, the following power shell contents were confirmed. Writing objects: 100% (35/35), 11.68 KiB | 0 bytes/s, done. Total 35 (delta 3), reused 0 (delta 0) remote: Compressing source files... done. remote: -----> Python app detected remote: ! The latest version of Python 3 is python-3.6.5 (you are using ÿþpython-3.6.5, which is unsupported). remote: ! We recommend upgrading by specifying the latest version (python-3.6.5). remote: Learn More: https://devcenter.heroku.com/articles/python-runtimes remote: -----> Installing ÿþpython-3.6.5 remote: ! Requested runtime (ÿþpython-3.6.5) is not available for this stack (heroku-16). remote: ! Aborting. More info: https://devcenter.heroku.com/articles/python-support remote: ! Push rejected, failed to compile Python app. remote: remote: ! Push failed remote: remote: ! Push rejected to XXXXXXXX. remote: To https://git.heroku.com/XXXXXXXX.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/XXXXXXXX.git what should i do? -
Django Rest Frmework trouble with the creation of users
I made three different views.py variants and in all of them the user is successfully created but in the admin panel I see Invalid password format or unknown hashing algorithm. views1.py class UserCreteSerializer(ModelSerializer): class Meta: model=User fields = ['email', 'phone', 'password'] extra_kwargs = {"password": {"write_only": True} } def create(self, validated_data): email = validated_data['email'] phone = validated_data['phone'] password = validated_data['password'] user_obj = User( email = email, phone = phone ) user_obj.set_password(password) user_obj.save() return validated_data views2.py def create(self, validated_data): user = User(**validated_data) user.set_password(validated_data['password']) user.save() return user views3.py def create(self, validated_data): return User.objects.create(**validated_data) Why is it so? All these ways should work -
django urlpattern placement
In my django app there is a separate application for auth purposes. My main urls.py urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^auth/', include('auth.urls', namespace='auth'), name='auth'), etc. ] My auth urls.py urlpatterns = [ url(r'^$', views.log_in, name='auth'), ] Now I want to put logout view into my auth application and bind it with '/logout'. As far as there is only one url ('/auth') which leads to my auth app where and how should I put '/logout' urlpattern? -
How to take Dashboard user input in Django REST API and run external Python Script in Django and See External python script output in API?
I am new in Django and Python. I create small python script and Django REST API. Now I want to integrate External python script and Django REST API so I will get Output from External python script. My updated view.py file as follows from rest_framework import viewsets from . models import input_params from . serializers import input_paramsSerializer import sys from base_search.myscript import * import requests import subprocess class inputViewSet(viewsets.ModelViewSet): queryset = input_params.objects.all() serializer_class = input_paramsSerializer def addition(self,request): para = input_params(request.POST) if request.method == "POST": output = subprocess.Popen(['myscript']) return output my url.py file is follows from django.contrib import admin from django.conf.urls import url,include #from rest_framework.urlpatterns import format_suffix_patterns from base_search.views import inputViewSet , addition from rest_framework import routers router = routers.DefaultRouter() router.register(r'input',inputViewSet) #router.register(r'average',addition) urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^',include(router.urls)), #url(r'^', views.addition, name = 'average') ] my model.py is follows: from django.db import models # Create your models here. class input_params(models.Model): location = models.CharField(max_length = 10) min_hourly_rate = models.IntegerField() max_hourly_rate = models.IntegerField() def __str__ (self): return self.location myscript.py is from base_search.models import input_params def average(min_hourly_rate, max_hourly_rate): average = ((max_hourly_rate) + (min_hourly_rate))/2 print (average) API is running but I am not getting Output so I can not check API accepting USER input or not … -
Django Form with related data from two other tables displayed using a nested (optgroup) <select>
I'm trying to duplicate the functionality of this manually created <select> using a proper Django form. {% regroup roll_counts by get_type_display as roll_list %} <select name="film"> <option>Select a film</option> {% for type in roll_list %} <optgroup label="{{ type.grouper }}"> {% for film in type.list %} <option value="{{film.id}}">{{ film.manufacturer }} {{ film.name }} ({{ film.count }})</option> {% endfor %} </optgroup> {% endfor %} </select> The queryset for roll_counts looks like this: roll_counts = Film.objects\ .filter(roll__owner=owner, roll__status='storage')\ .filter(format=camera.format)\ .annotate(count=Count('name'))\ .order_by('type') I'm trying to recreate this using some sort of Django ModelForm to be able to actually validate data submitted to the form. The ultimate goal is to be able to "load" a camera with a roll of film. Which is to say: associate a Roll (with a foreign key to Film) with a particular Camera and mark said camera with a status of loaded. So the <select> displays data that isn't being updated by the form. What needs to be modified are the Roll and Camera models, not the Film model. But I assumed all that could be handled within the view that receives data from the form. I'm unsure how to get a ModelForm to display the aforementioned roll_count query (and … -
How to filter and access ManyToMany fields in a Django QuerySet?
Suppose I have the following models with a ManyToMany relationship: class City(models.Model): name = models.CharField(max_length=100) class Course(models.Model): name = models.CharField(max_length=100) cities = models.ManyToManyField(City) Now I want to filter for the courses with a given name in a given city: courses = Course.objects.filter(name='Course1', cities__name='City1') Is it possible to access the City fields through the courses QuerySet? In this example, would it be possible to get 'City1' from courses only? Thanks in advance. -
Django - How to make API calls at constant intervals
What I want to do: I have built a dashboard which shows Key Performance Indicators that I get from an API at Zendesk.com. It needs to update at regular intervals (I am trying to use time.sleep() to accomplish this) but it cannot be too often as I have a 700 request per minute limit. I used Django so I could create an interface to show the data. IT WILL NEVER BE USED AS A WEBSITE. The problem: I use jquery "get" calls to get data from my script. If I use "python manage.py runserver" it eventually creates what I am guessing are many many workers and makes calls to my script far far too often and blows past my 700 request limit with Zendesk. If I use "python manage.py runserver --nothreading" it works great HOWEVER when I click a button in the navbar or try to refresh the page, I have to wait for my script to run fully before it gets to the task. Relevant code of my program: As an example, in the html code I call data from json: <script type="text/javascript"> $(document).ready(function() { function pollSiteData () { $.get('/nb_new/', function(resp) { $('#nb_new').text(resp['nb_new']); }); window.setTimeout(pollSiteData,10000); } pollSiteData(); }); </script> … -
How to provide select language option in django
I Have an application and site developed in django which supports two langauges namely,English and Hebrew. in my settings.py LANGUAGE CODE is set to 'en-us' LANGUAGES = [ ('he', gettext('Hebrew')), ('en',gettext('English')), ] Whenever I am using English language, it does not throw error that all hebrew fields are necessary but when I do such in Hebrew language ,it gives error all English fields are required. How can I provide user selected language and select language code accordingly in django -
Serve Flask and Django applications with Gunicorn and Nginx
I have 3 restful APIs two of them build with Flask one with Django. I run all with gunicorn. I have a meteor project for UI which use these three APIs for the backend. And I use nginx as a reverse proxy. nginx configuration is: server { listen 80 default_server; listen [::]:80 default_server; client_max_body_size 100M; root /var/www/html; server_name <my_ip>; location / { proxy_pass http://127.0.0.1:3000/; } location /auth/ { proxy_pass http://127.0.0.1:5005/; } location /workflow/ { proxy_pass http://127.0.0.1:8000/workflow/; } location /bugbase/ { proxy_pass http://127.0.0.1:5000/; } } Works if I run all backend projects with python as follow: $ python manage.py But if I run them with gunicorn: $ gunicorn -b 127.0.0.0:<port> manage:app While I try to login(/auth/ proxy_pass) gunicorn says 'timeout', Its same for all other APIs. They don't work, but the run without any error. Environments python: 3.6 flask: 0.12 django: 1.11 ubuntu: 16.04 -
Error when indexing data using elasticsearch dsl
I have two models which are as follows: class PostUser(models.Model): user_id = models.CharField(max_length=1000,blank=True,null=True) reputation = models.CharField(max_length = 1000 , blank = True , null = True) def __unicode__(self): return self.user_id def indexing(self): obj = PostUserIndex( meta = {'id': self.id}, user_id = self.user_id, reputation = self.reputation, ) obj.save(index = 'post-user-index') return obj.to_dict(include_meta=True) class Posts(models.Model): user_post_id = models.CharField(max_length = 1000 , blank = True , null = True) score = models.CharField(max_length = 1000 , blank = True , null = True) owner_user_id = models.ForeignKey(PostUser,default="-100") def __unicode__(self): return self.user_post_id def indexing(self): obj = PostsIndex( meta = {'id': self.id}, user_post_id = self.user_post_id, score = self.score, owner_user_id = self.owner_user_id, ) obj.save(index = 'newuserposts-index') return obj.to_dict(include_meta=True) The way I am trying to index my data is as follows: class PostUserIndex(DocType): user_id = Text() reputation = Text() class PostsIndex(DocType): user_post_id = Text() score = Text() owner_user_id = Nested(PostUserIndex) Then i try to run the following method to index data: def posts_indexing(): PostsIndex.init(index='newuserposts-index') es = Elasticsearch() bulk(client=es, actions=(b.indexing() for b in models.Posts.objects.all().iterator())) I have tried different approaches by manually entering the nested properties and also changing from doctype to inner doc of PostUser but still I am getting the weird error. ERROR: AttributeError: 'StackOverFlowUsers' object has no attribute … -
What is the best practice for pushing variables to templates?
I'm writing a web application and was wondering what the best practice is for pushing variables to templates. Right now, I'm requesting the models in my views and then pushing these objects to templates. Example: I have a framework and controls attached to these frameworks. If I want to display all the controls for the specific frameworks I would do this in views: # views.py def show_frameworks(request): frameworks = Framework.objects.all() controls = Control.objects.all() data['frameworks'] = frameworks data['controls'] = controls return render(request, 'show_frameworks.html', data) And then in templates: # templates/show_frameworks.html <div> {% for framework in frameworks %} <p>{{framework.name}}</p> {% for control in controls %} {% if control.framework == framework %} <p>{{control.title}}</p> {% endif %} {% endfor %} {% endfor %} </div> Would this be efficient to do or would it be faster/more efficient to do something like this: # views.py def show_frameworks(request): controls = Control.objects.all().order_by('framework') data['controls'] = controls return render(request, 'show_frameworks.html', data) And then in templates: # templates/show_frameworks.html <div> {% for control in controls %} {% if forloop.first %} <p>{{control.framework.name}}</p> <p>{{control.title}}</p> {% with prevframework = control.framework %} {% else %} {% with curframework = control.framework %} {% if curframework != prevframework %} <p>{{control.framework.name}}</p> <p>{{control.title}}</p> {% else %} <p>{{control.title}}</p> {% endif %} … -
I can't deploy to heroku with django
I'd like to deploy to Heroku. But the error messages are displaied. $ git push heroku master remote: Error: could not determine PostgreSQL version from '10.3' remote: remote: ---------------------------------------- remote: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7sfvmk3a/psycopg2/ remote: ! Push rejected, failed to compile Python app. remote: remote: ! Push failed remote: Verifying deploy... remote: remote: ! Push rejected to ayblog. remote: To https://git.heroku.com/ayblog.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/ayblog.git' I think "Error: could not determine PostgreSQL version from '10.3'" is wrong. And I thing "Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7sfvmk3a/psycopg2/" is also wrong. If you know some solution, would you tell me how to do that? Thank you. -
How to implement sklearn classifiers in Django Form ?
I am using some of the sklearn classifiers ( SVM, KNN etc) for breast cancer prediction. But I need to implement it in Web Application using Django Framework. I don't know how to implement it in Django Form. Can anyone please explain/ show me the step wise process of implementing it ? -
Get data from API and display on tempate
I am trying to get data from the stackoverflow API and display them on an html table in my template. So far I have managed to get the data but cannot display them in the template. I end up getting the last one. My code so far: def get_questions(request): context = {} r = requests.get('https://api.stackexchange.com/2.2/questions?fromdate=1525737600&order=desc&sort=activity&tagged=python&site=stackoverflow').json() for item in r['items']: context['owner'] = item['owner']['display_name'] context['title'] = item['title'] #some other attrs here template = 'questions/questions_list.html' context['greeting'] = 'Hello' return render(request,template,context) -
Heroku not running django migrations
I have a Django application that is running on Heroku. I am using Postgres with Django and when I first committed the files and ran heroku run python manage.py migrate, the initial migrations worked fine. I then created a new app restaurant in which I created a new model. I ran the local makemigrations and migrate and it created a new file restaurant/migrations/0001_initial.py. The problem is that when I now run heroku run python manage.py migrate it says there are no migrations to apply. I tried heroku run python manage.py migrate restaurant but there's an error CommandError: App 'restaurant' does not have migrations. I have logged into the console and the migration files are there so I was wondering if I should be doing something like: heroku run python manage.py makemigrations heroku run python manage.py migrate I am using the starter project as the base and only modified the settings so that DATABASES = { 'default': dj_database_url.config() } -
Overridden create method not saving the instance field in Django Rest Framework
I am working with the nested serializers in my project. There is just one small problem that I am facing and unable to guess whats going wrong. I have two models:- Model 1:- class Answer_Options(models.Model): text = models.CharField(max_length=200) Model 2:- class Quiz_Question(models.Model): text = models.CharField(max_length=200) possible_answers = models.ManyToManyField(Answer_Options) correct = models.ForeignKey(Answer_Options, related_name="correct", default=None, on_delete=models.CASCADE, blank=True, null=True) I have created the following serializers for my above models as follows:- class Answer_OptionsSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Answer_Options fields = ('url', 'text') And for Quiz Question:- class Quiz_QuestionSerializer(serializers.HyperlinkedModelSerializer): possible_answers = Answer_OptionsSerializer(many=True) correct = Answer_OptionsSerializer() class Meta: model = Quiz_Question fields = ('url', 'text', 'possible_answers', 'correct') def create(self, validated_data): possible_answers_data = validated_data.pop('possible_answers') correct_answers_data = validated_data.pop('correct') quiz_question = Quiz_Question.objects.create(**validated_data) if possible_answers_data: for answer in possible_answers_data: answer, created = Answer_Options.objects.get_or_create(text=answer['text']) if (answer.text == correct_answers_data['text']): quiz_question.correct = answer //Not sure why this is not getting saved quiz_question.possible_answers.add(answer) return quiz_question What happens is that when I post data through Django Rest Framework the create method gets called and possible answers get saved but don't know why the correct answer is not getting save for that instance. I am not getting any error or exception. Also I can see the correct answer on the Django Rest Frameworks new object … -
Django Postgres JSONField query
I have a class with a json field in it class A(models.Model) brand = JSONField() If I post an array of JSON like [{'brand_id:1', 'name':'b1'}, {'brand_id:2', 'name':'b2'}] it is stored as an array of JSON. This works fine. How should I query so as to check if '1' is present in the brand_id of any dictionary in that array? -
Python Django 2.0 DecimalField "Ensure that there are no more than 3 digits before the decimal point"
My amount column attributes are set to max_digits = 13, decimal_places = 7 because you could technically have something like 10000.0000001 bitcoin. When I try to enter and submit just 0.1 Bitcoin on my form I get the error: Ensure that there are no more than 3 digits before the decimal point. This isn't working as expected: 0.1 is not more than 3 digits, and even if it was I should still be able to set more than 3 digits.. What is happening here? models.py class Transactions(models.Model): user = models.ForeignKey(User, on_delete = models.CASCADE) coin = models.CharField(max_length = 64) buysell = models.CharField(default = 'buy', max_length = 4) amount = models.DecimalField(max_digits = 13, decimal_places = 7) trade_price = models.DecimalField(max_digits = 5, decimal_places = 2) trade_date = models.DateTimeField(auto_now = True) forms.py class TransactionForm(forms.ModelForm): CHOICES = ((1, 'Buy'), (2, 'Sell'),) coin = forms.ModelChoiceField(queryset = Coin.objects.all()) buysell = forms.ChoiceField(choices = CHOICES) field_order = ['buysell', 'coin', 'amount', 'trade_price'] class Meta: model = Transactions fields = {'buysell', 'coin', 'amount', 'trade_price'} -
Python 3.5 CSV.reader returns no line
I try to read an excel csv file. with open('test.csv') as f: reader = csv.reader(f, dialect='excel') but csv.reader returns nothing. Inside the CSV file, the csv file is organized as follow: 04/30/2018,HFRX Absolute Return Index,HFRXAR,0.09%,1067.4 04/30/2018,HFRX ED: Distressed Restructuring Index,HFRXDS,0.44%,1051.13 04/30/2018,HFRX ED: Merger Arbitrage Index,HFRXMA,-0.80%,1816.23 04/30/2018,HFRX ED: Special Situations Index,HFRXSS,0.46%,1324.98 04/30/2018,HFRX EH: Equity Market Neutral Index,HFRXEMN,-0.07%,1011.05 04/30/2018,HFRX EH: Fundamental Growth Index,HFRXEHG,-1.89%,1766.22 04/30/2018,HFRX EH: Fundamental Value Index,HFRXEHV,0.11%,1233.52 04/30/2018,HFRX Emerging Markets Composite Index,HFRXEMC,-0.16%,1905.7 open function returns: and the csv.reader returns: Any idea why I am not able to read this file? Many thanks in advance for your help. -
Django Chart.js
I am trying to setup Chart.js to use in my project. This is example with i am trying to reproduce and i get this I am using PyCharm, so it was easy to download all libraries and i added chart.js in Installed aps My test.html: {% load staticfiles %} <html> <head> <title>django-chartjs line chart demo</title> <!--[if lte IE 8]> <script src="{% static 'js/excanvas.js' %}"></script> <![endif]--> </head> <body> <h1>Some Line Charts loaded in Ajax!</h1> <canvas id="myChart" width="500" height="400"></canvas> <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script> <script type="text/javascript" src="{% static 'js/Chart.min.js' %}"></script> <script type="text/javascript"> $.get('{% url "line_chart_json" %}', function(data) { var ctx = $("#myChart").get(0).getContext("2d"); new Chart(ctx, { type: 'line', data: data }); }); </script> </body> </html> My view: class LineChartJSONView(BaseLineChartView): def get_labels(self): """Return 7 labels for the x-axis.""" return ["January", "February", "March", "April", "May", "June", "July"] def get_providers(self): """Return names of datasets.""" return ["Central", "Eastside", "Westside"] def get_data(self): """Return 3 datasets to plot.""" return [[75, 44, 92, 11, 44, 95, 35], [41, 92, 18, 3, 73, 87, 92], [87, 21, 94, 3, 90, 13, 65]] line_chart = TemplateView.as_view(template_name='main/mark/test.html') line_chart_json = LineChartJSONView.as_view() -
504 gateway timeout for any requests to Nginx with lot of free resources
We have been maintaining a project internally which has both web and mobile application platform. The backend of the project is developed in Django 1.9 (Python 3.4) and deployed in AWS. The server stack consists of Nginx, Gunicorn, Django and PostgreSQL. We use Redis based cache server to serve resource intensive heavy queries. Our AWS resources include: t1.medium EC2 (2 core, 4 GB RAM) PostgreSQL RDS with one additional read-replica. Right now Gunicorn is set to create 5 workers (by following the 2*n+1 rule). Load wise, there are like 20-30 mobile users making requests in every minute and there are 5-10 users checking the web panel every hour. So I would say, not very much load. Now this setup works alright for 80% days. But when something goes wrong (for example, we detect a bug in the live system and we had to switch off the server for maintenance for few hours. In the mean time, the mobile apps have a queue of requests ready in their app. So when we make the backend live, a lot of users hit the system at the same time.), the server stops behaving normally and started responding with 504 gateway timeout error. Surprisingly … -
Django: help needed to understand the code with csrf_exempt
I am trying to create an endpoint in DJango where the output is in JSONformat and not as a template and Post data is sent using JSON string not as a form without csrf token. I have searched on net (https://github.com/WiserTogether/django-remote-forms) and got the following relevant code as mentioned in the end of this post. I understand in the below code csrf_exempt is mainly for not checking for csrf But later i found the following. I didnt undestand why and what for are they being used: csrf_middleware = CsrfViewMiddleware() # Process request for CSRF csrf_middleware.process_view(request, None, None, None) # Process response for CSRF csrf_middleware.process_response(request, response) the below is the complete code: @csrf_exempt def user_create(request): csrf_middleware = CsrfViewMiddleware() response_data = {} if request.method == 'GET': test = {'create user api by sending email and password'} elif request.method == 'POST': # Process request for CSRF csrf_middleware.process_view(request, None, None, None) form_data = json.loads(request.body) form = MyUserCreationForm(form_data) if form.is_valid(): user = form.save() payload = { 'id': user.id, 'email': user.email, } jwt_token = {'token': jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256')} response = HttpResponse( json.dumps(jwt_token, cls=DjangoJSONEncoder), content_type="application/json" ) return response response = HttpResponse( json.dumps(test), content_type="application/json" ) # Process response for CSRF csrf_middleware.process_response(request, response) return response if we are exempting …