Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
django returning url kwarg string used for filtering as an int, trying to use as pk
Observe the url below http://127.0.0.1:8000/api/neighborhood-list/chicago/ this url is tied to a resource that will return a list of all the neighborhoods in a city. However I am currently getting this error: invalid literal for int() with base 10: 'chicago' Exception Location: /home/rickus/211hospitality/suitsandtables/backend/venv/local/lib/python2.7/site-packages/django/db/models/fields/init.py in get_prep_value, line 966 line 966: return int(value) Now I have already asked this question here: invalid literal for int() with base ten in listAPI view django rest framework and have implemented the suggestions but I am still fighting the django framework it seems. It looks like the Django framework is defaulting to trying to use my key word url agrument as a primary key here is my url, view, serializer, and model code url: url(r'^neighborhood-list/(?P<city>[a-zA-Z]+)/$', VenueFilterOptionsView.as_view(), name='neighborhood-list', ) view: class NeighborhoodListView(generics.ListAPIView): queryset = Neighborhood.objects.all() serializer_class = NeighborhoodSerializer(queryset, many=True) def get_queryset(self): return self.queryset.filter(city=self.kwargs.get('city')) serializer: class NeighborhoodSerializer(serializers.ModelSerializer): class Meta: model = Neighborhood fields = 'neighborhood' model: class Neighborhood(models.Model): city = models.ForeignKey(City, null=True) neighborhood = models.CharField(max_length=150, null=False) and if you so wish here is the full print out of the traceback which I think is going to be needed: Environment: Request Method: GET Request URL: http://127.0.0.1:8000/api/neighborhood-list/chicago/ Django Version: 1.11.2 Python Version: 2.7.12 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', … -
NoReverseMatch during render
I'm receiving this error: NoReverseMatch at /comments_page/1/post_comment/ Reverse for 'post_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comments_page/(?P[0-9]+)/post_comment/$'] My views.py def post_comment(request, product_id): host_product = Product.objects.get(pk=product_id) comment = Comment() comment.product = host_product comment.author = request.POST["author"] comment.comment_text = request.POST["comment"] comment.save() return render(request, 'comments_page/detail.html', {"host_product": host_product}) My comments_page\urls.py from django.conf.urls import url from . import views app_name = "comments_page" urlpatterns = [ # /comments_page/ url(r'^$', views.index, name="index"), # /comments_page/1/ url(r'^(?P<product_id>[0-9]+)/$', views.detail, name="detail"), # /comments_page/1/post_comment/ url(r'^(?P<product_id>[0-9]+)/post_comment/$', views.post_comment, name='post_comment'),] My detail.html <form action="{% url 'comments_page:post_comment' product.id %}" method="post"> {% csrf_token %} Name: <input type="text" id="author" name="author"> Comment: <textarea id="comment" name="comment"></textarea><br> <input type="submit" value="Post Comment"> I think I've identified the problem as being in the product.id here {% url 'comments_page:post_comment' product.id %} in the html page. I've tried formatting this a couple of different ways, but I haven't had any luck. Do note, the comment is going through and the form and it works as far as updating the database and loading the entry on the page goes, but the page is not being redirected. I have to reload it manually. Any help would be appreciated. -
How to run python script in a django class-based view?
My Django app models.py has the following class: class Project(models.Model): name = models.CharField(max_length=100) ... I am using class-based views so my views.py file has the following class: from django.views import generic from django.views.generic.edit import CreateView class ProjectCreate(CreateView): model = Project fields = ['name'] The HTTP form works perfectly and creates a new element in the database, but I need to call a function from an external python file upon the creation of a new instance of the class Project, the code I'm trying to run is: import script script.foo(self.object.name) I'm trying to run the function foo inside the class ProjectCreate but I'm clueless, I tried using get and dispatch methods but it didn't work, I have read the documentation of CreateView but I couldn't find my answer. Should I use function-based views? or is there a solution for class-based views? Thank you very much. -
exec: celery: not found, when running Celery through Supervisord
I've written a simple celery.conf file in /etc/supervisor/conf.d The .conf file: [program:celeryd] command=/home/user1/celery.sh stdout_logfile = /home/user1/logs/celery/celery.log redirect_stderr = true The celery.sh file: #!/bin/bash cd /home/user1/project/Django_Project exec celery --app=Django_Project.celery:app worker --loglevel=INFO The ISSUE is: I'm not able to run this celery.sh file through supervisor. Please NOTE: The script when invoked from the any directory works well and always runs the celery workers. For example /home/user1/celery.sh gets the celery working. Also, I get the following in my log file, /home/user1/logs/celery/celery.log /home/shivam/celery.sh: line 3: exec: celery: not found /home/shivam/celery.sh: line 3: exec: celery: not found /home/shivam/celery.sh: line 3: exec: celery: not found /home/shivam/celery.sh: line 3: exec: celery: not found To add another thing, I tried running another proceess from this same celery.conf file and it works as well.(It was the development server of Django) I'm not using virtualenv. The script when invoked manually works. But doesn't seem to find celery when invoked from supervisor. Any help is highly appreciated. Thanks in advance. -
Django project with core code stored in an app with the same name
When creating a Django project (e.g. myapp), the framework also creates a subdirectory/python package with the same name as the project to hold settings.py, urls.py and wsgi.py: ─ myapp └─ myapp ├─ __init__.py ├─ settings.py ├─ urls.py └─ wsgi.py Although I then add a number of apps, I almost always have some core functionality that belongs to the project itself. Is there any reason why I should not add the core functionality to the myapp package alongside settings.py and wsgi.py and include this in settings.INSTALLED_APPS? E.g.: ─ myapp ├─ app1 ├─ app2 └─ myapp ├─ __init__.py ├─ models.py ├─ settings.py ├─ templates | └─ homepage.html ├─ urls.py ├─ views.py └─ wsgi.py I've seen other people create a core app instead to hold the 'core' code that is specific to the project, is this necessary? -
Writing over 1 milion records to csv in python
Hi guys am extracting some data to a csv file using python, the data is over 1 million records.Definitely their seems to be memory issues with my script because after a painstaking 5 hours and roughly over 190k records written the scripts running process gets killed. here is my terminal (.venv)[cv1@mdecv01 maidea]$ python common_scripts/script_tests/ben-test-extract.py BEN Generating CSV file. Please wait ... Preparing to write file: BEN-data-20170731.csv Killed (.venv)[cv1@mdecv01 maidea]$ is their a way i can extractthis data with proper memory management, kindly any ideas. here is my script http://www.codesend.com/view/0d97aa0efd61ffa9bc235bf0bc73f2ea/ Any ideas gentlemen -
Dockerizing an already existing app and database
I am trying to Dockerize an app that is already created (database included). I've got the proper files in place: docker-compose.yml dockerfile requirements.txt I'm having trouble with the database part - How do I configure the docker-compose.yml file to point to the database that is already created? Here's why I ask - my understanding of Docker - is you create your base app - then "Dockerize" it or package it into an image that you can distribute. I'm a beginner at this - so that may be why I'm not understanding. Here is my current docker-compose.yml: version: '2' services: db: image: postgres:9.6 environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=qwerty1234 - POSTGRES_DB=epic_ar_db ports: - "5433:5433" web: build: . command: python2.7 manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db and dockerfile: ############################################################ # Dockerfile to run a Django-based web application # Based on an Ubuntu Image ############################################################ # Set the base image to use to Ubuntu FROM debian:8.8 # Set the file maintainer (your name - the file's author) MAINTAINER HeatherJ # Update the default application repository sources list RUN apt-get update && apt-get -y upgrade RUN apt-get install -y python python-pip libpq-dev python-dev #install git RUN apt-get update && … -
Generate PDF from http request in Django
I need to generate pdf for reporting purpose. Front end will be sending me json list of list (dynamic, may have only one item, or N items) in the format of [title, file type, data] in http request. [ ["Confusion Matrix", "img", "#image data in 64 bits encode"] , ["Graph1", "img", "#image data in 64 bits encode"] ] In my view, after accepting the request, how should I convert them into http format one by one and append them into one big http template to generate the pdf? I were trying with django template but I wonder if there is any more elegant solution out there. -
Format Django Table Column into Currency and % format
I was wondering how I could format the numbers in a django data table into currency and % format? 12345 => $12,345 0.01 =>1% -
No module named django.template in django 1.0.4
I have old project in django 1.0.4 and viertualenv with python 2.7. When I try one of my script i recived an error in this place: from django.template import Context, loader error: Traceback (most recent call last): File "path to file.py", line 6, in from django.template import Context, loader ImportError: No module named django.template Anybody had the same error like this? -
Django IntegrityError When Submitting Form (pre-populating in views)
I haven't be able to find a solid answer for my problems. I am trying to pre-populate form in views. So when user works with form, some data is already been taken care for. What am I trying to do is to automatically get user username and his group and feed that into database. But when I try to do that I get IntegrityError NOT NULL constraint failed. I am new to this and I'm learning from MDN web docs. My views.py: class AddNewView(generic.View): formClass = AddNewForm template_name = 'myapp/subscription_form.html' def get(self, request): form = self.formClass(None) return render(request, self.template_name, {'form': form}) def post(self, request): form = self.formClass(request.POST) if form.is_valid(): subscription = form.save(commit=False) organization = request.user.groups.values_list('id', flat=True).first() input_user = self.request.user stuff = form.cleaned_data['stuff'] description = form.cleaned_data['description'] subscription.save() return render(request, self.template_name, {'form': form}) forms.py: class AddNewForm(forms.ModelForm): def __init__(self, *args,**kwargs): super (AddNewView, self ).__init__(*args,**kwargs) class Meta: model = Subscription fields = [ 'stuff', 'description', ] models.py: class Subscription(models.Model): organization = models.ForeignKey(Group, help_text="which organization") input_user = models.CharField(max_length=150) input_date = models.DateTimeField(auto_now_add=True) description = models.CharField(max_length=1000, null=True, blank=True, help_text='Description') html: {% extends "base_generic.html" %} {% block content %} <form action="" method="post"> {% csrf_token %} <table> {{ form.as_table }} </table> <input type="submit" value="Submit" /> </form> {% endblock %} … -
Django create with calculated field
Say I have such Django model: class Item(model): category = CharField(...) weight = IntegerField(...) and I want to create new Item with weight=Item.objects.filter(categoty=SOME_CATEGOTY).aggregate(Max('weight'))['weight__max'] The problem is that above query is executed to load data into python, not as insert into items (category, weight) select SOME_CATEGORY, max(weight) from items where category = SOME_CATEGORY So, concurrent creations may create multiple items of same categoty with the same weight, whereas sequential creation always produces distinct weight values. Do I need serializable isolation level to achieve that in two steps: select max then create, or django has built-in mechanism to generate insert with select? -
Update default value according to the input
I have form screen as below. What I want to do is to add function to change default value for other forms according to the input for first form Project querying the database. Let's say, if project 'a' is already registered with version:0/program:residential/Location:Japan in database and fill in project 'a' again, default value for other form will be updated to version:1/program:residential/Location:Japan. Thank you in advance. -
Displaying a line chart in Django from MySQL
Is there a simple way to display the information from an MySQL Database in Django as an line graph? -
Filtering DateTime field with Date Field using Filter Set
How do I go about filtering a date time field with just a Date field. With the model and filter below http://localhost:8020/applications/?created=19-07-2017 returns an empty queryset even with records which have date created=19-07-2017 (The created date of the record in datetime IN MY MODELS.PY Class Application(models.Model): created = models.DateTimeField(auto_now=False, auto_now_add=True) IN MY FILTERS.PY import django_filters class ApplicationFilter(django_filters.FilterSet) created = django_filters.DateTimeFilter( label='Created') class Meta: model = Application fields = ['created'] -
Django class based view with GET parameters
Using django rest framework I'm trying to create a view that allows to retrieve and update a single user but I don't understand how to do it. At the moment I don't care about permissions and authentication. views.py class UserDetailsView(RetrieveUpdateAPIView): def get_object(self, user_id): user = get_user_model().objects.get(pk=user_id) return user urls.py urlpatterns = [ #rest of code url(r'^user/(?P<user_id>[0-9]+)/$', views.UserDetailsView.as_view(), name="profile"), ] if I try to access localhost:8000/user/1 I get: TypeError at /user/1/ get_object() missing 1 required positional argument: 'user_id' Request Method: GET Request URL: http://localhost:8000/user/1/ Django Version: 1.11.1 Exception Type: TypeError Exception Value: get_object() missing 1 required positional argument: 'user_id' I am missing something because with function based views everything works just fine. For example: views.py def game(request, id_game): # rest of code urls.py url(r'^games/(?P<id_game>[0-9]+)/$', views.game, name="game"), -
I got this "ValueError: invalid literal for int() with base 10" in my Django project
model in my application: from __future__ import unicode_literals from django.db import models class Category(models.Model): name = models.CharField(max_length = 128, unique = True) def __unicode__(self): return self.name class ModelName(models.Model): name = models.CharField(max_length = 128, unique = True) def __unicode__(self): return self.name class CarStorage(models.Model): category = models.ForeignKey(Category) CarID = models.CharField(max_length = 10, unique = True, null = False) model = models.ForeignKey(ModelName) storage = models.IntegerField(default = 0) perchasedYear = models.IntegerField(default = 0) def __unicode__(self): return self.CarID Population code: import os os.environ.setdefault('DJANGO_SETTINGS_MODULE','TKTrading.settings') import django django.setup() from APP.models import Category, ModelName, CarStorage def populate(): BASE_DIR = '/Users/apple/WebWorkshop/TKTrading/' DATA_DIR = os.path.join(BASE_DIR, 'APP/Data') CATEGORY = open(os.path.join(DATA_DIR, 'CATEGORY.txt'),'r') global CategoryList CategoryList = [] for line in CATEGORY: newC = Add_Category(line) CategoryList.append(newC) CATEGORY.close MODELNAME = open(os.path.join(DATA_DIR, 'Model.txt'), 'r') global ModelList ModelList = [] for line2 in MODELNAME: newM = Add_ModelName(line2) ModelList.append(newM) MODELNAME.close CARSTORAGE = open(os.path.join(DATA_DIR, 'CARSTORAGE.txt'), 'r') for line3 in CARSTORAGE: print(line3) c = line3.split(',') ID = str(c[0]) category = str(c[1]) model = str(c[2]) storage = int(c[3]) year = int(c[4]) Add_CarStorage(ID, category, model, storage, year) CARSTORAGE.close def Add_Category(name): c = Category.objects.get_or_create(name = name)[0] c.save() return c def Add_ModelName(name): m = ModelName.objects.get_or_create(name = name)[0] m.save() return m def Add_CarStorage(ID, category, model, storage, year): for CATEGORY in CategoryList: if CATEGORY.name == … -
Django auto path arrangement for static files when prepared template adding
When adding a prepared template such as bootstrap template into a django project. I change all src path which is linking local statics files such as javascript, css and images.I want to automate this operations .Is there any way for doing this ? In my opinion,using regex it will be possible.Howeever I can not until now I put a few patterns down to be the example <img src="images/slide-dark.jpg" alt="" > <link href="css/animate.css" rel="stylesheet"> <script src="js/bootstrap.min.js"></script> will be like this <img src="{% static 'images/slide-dark.jpg' %}"> <link href="{% static 'css/animate.css' %}" rel="stylesheet"> <script src="{% static 'js/bootstrap.min.js' %}"></script> -
Django admin Ordering
I am a beginner in Django and I am trying to order Comments using Array.. I have this class inside models.py class Comment(models.Model): line_combination = models.ForeignKey(LineCombination, null=False, blank=False, default=1, on_delete=models.CASCADE) name = models.CharField(max_length=100 , null=True , blank=True) email = models.CharField(max_length=250,null=True , blank=True) comment = models.TextField() class Meta: ordering = ['line_combination__line__name'] for Line Combination I have this class: class LineCombination(models.Model): line = models.ForeignKey(Line, null=False, blank=False, on_delete=models.CASCADE) combinations = models.ManyToManyField(GenoType, blank=False) class Meta: # Ordering the names of line Alphabetically ordering = ['line__name'] for the GenoType I have this class: class GenoType(models.Model): name = models.CharField(max_length=250, null=False, blank=False) I want to make ordering in the class Comment By line_combination.combinations.name Like this: ordering = ['line_combination__line__name' , 'line_combination__combinations__name'] but line_combination.combinations.name is Array So i can't put it as value .. so how can I do it? Thanks in advance.. -
django how to filter through a ManyToManyField?
I've got the models: class DamageKind(models.Model): # the possible regions for this kind of damage regions = models.ManyToManyField(Region) [...] class Damage(models.Model): region = models.ForeignKey(Region) kind = forms.models.ModelChoiceField(queryset=???) [...] how I can get the queryset that only contains the damagekinds that are possible for the selectet region? Thanks in advance -
get a better protection of my email password in Django
I have a form on my website and once it is submitted, it sends an email to mail own mailbox. My problem is that, to do so, I hardcoded my email and my password in my settings.py like this: EMAIL_HOST_USER = 'myemail@gmail.com' EMAIL_HOST_PASSWORD = 'mypassword' but I feel that it is not secured at all, so I try to find a way to make it not hardcoded in my settings.py. Based on how I store my secret key, I tried the following thing: with open('/Users/myapp/email.txt') as f: EMAIL_HOST_USER = f.read().strip() with open('/Users/myapp/email_password.txt') as f: EMAIL_HOST_PASSWORD = f.read().strip() But it did not work. It raises an SMTP error: "Username and Password not accepted." Any idea on how to protect my password better ? -
I'm getting this error "Missing needed parameter state" when hitting the /auth/complete/ url, what am I doing wrong?
I'm using the Python social auth with a custom backend. Here is my code so far: from requests import HTTPError from social.backends.oauth import BaseOAuth2 from social.exceptions import AuthFailed class MyOAuth2(BaseOAuth2): #testing OAuth authentication backend name = 'testing' AUTHORIZATION_URL = 'https://<my_drupal_oauth2_server>/oauth2/authorize' ACCESS_TOKEN_URL = 'https://<my_drupal_oauth2_server>/oauth2/token' SCOPE_SEPARATOR = ',' STATE_PARAMETER = 'False' REDIRECT_STATE = 'False' EXTRA_DATA = [ ('id', 'id'), ('expires', 'expires'), ] def get_user_details(self, response): #Return user details from test account return {'username': response.get('login'), 'email': response.get('email') or '', 'first_name': response.get('name')} #def user_data(self, access_token, *args, **kwargs): # #Loads user data from service This is my first attempt to implement an OAuth2 setup using a custom back end -- first attempt to write one. The OAuth2 server works with Google's OAuth2 playground. My django app (which is Open edX) however is throwing the error. If I manually add '?state=xyz' to the url, I get another error: "Session value state missing" I've searched for good information on both of these errors and I'm coming up short on what a root cause might be, and a solution. Please help! -
How to do bulk instance deletion in Django Rest Framework?
In DRF's DefaultRouter url router, it requires a {lookup} parameter to route DELETE requests to the destroy method of a ModelViewSet (so, you'd make your request to delete an object instance to the endpoint {prefix}/{lookup}/). This is fine for deleting a single instance, but I'd like to extend that functionality to deleting multiple instances on a single request. Let's say the lookup parameter is called uuid and the model is called Product. Here's an extended version of destroy: def destroy(self, request, uuid=None): """ Overridden method allows either url parameter of single UUID (to delete a single instance), or multiple query parameters `uuids` to delete multiple instances. """ if not uuid: uuids = request.query_params.get('uuids', None) if not uuids: return Response(status=status.HTTP_404_NOT_FOUND) if len(uuids) != Product.objects.filter(uuid__in=uuids).count(): return Response(status=status.HTTP_404_NOT_FOUND) Product.objects.filter(uuid__in=uuids).delete() else: instance = self.get_object(uuid) if not instance: return Response(status=status.HTTP_404_NOT_FOUND) instance.delete() return Response(status=status.HTTP_204_NO_CONTENT) So this version takes a DELETE request and multiple uuids[] query parameters in the url. Now I just need to route it in urls.py: from rest_framework.routers import DefaultRouter, Route class BulkDeleteRouter(DefaultRouter): """ a custom URL router for the Product API that correctly routes DELETE requests with multiple query parameters. """ def __init__(self, *args, **kwargs): super(BulkDeleteRouter, self).__init__(*args, **kwargs) self.routes += [ Route( url=r'^{prefix}$', … -
Django, Mail not sending. Sendgrid
I am tring to send mail to the admin from contact form from the website via send grid. The Email is not getting delivered for some reason Here is the view.py def contact(request): title = 'Contact' form = contactForm(request.POST or None) confirm_message = None if form.is_valid(): sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] subject = 'Message from **' content = '%s %s' %(comment, name) from_email = form.cleaned_data['email'] to_email = Email("***") try: mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) except: title="Sorry!" confirm_message = "Error sending message, Please try after sometime. Thank you!" title="Thanks" confirm_message = "Thanks for the message, We will get right back to you." form = None context = {'title': title, 'form':form, 'confirm_message': confirm_message,} template = "contact.html" return render(request,template,context) settings.py EMAIL_HOST = 'smtp.sendgrid.net' EMAIL_HOST_USER = '**' EMAIL_HOST_PASSWORD = '***' EMAIL_PORT = 587 EMAIL_USE_TLS = True EMAIL_BACKEND = "sgbackend.SendGridBackend" The email is set up in zoho.. and i am not able to get receive any emails -
validating number of characters allowed in django python
i'd like to ask how to validate my forms. This is my code. class SMSTemplateForm(forms.ModelForm): class Meta: model = SMSTemplate def clean(self): template_format = self.cleaned_data.get('template_form') if len(template_format) > 160: raise forms.ValidationError('SMS content too long.') return self.cleaned_data