Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django REST Framework on Heroku not saving created objects
I am running a Django app on Heroku that uses Django REST Framework to store data from scanners, which are devices which send a JSON POST request to the framework whenever it detects a new scan. The request is serialized and creates a new scan object. The models are as follows: class Location(models.Model): address = models.CharField(max_length=300) city = models.CharField(max_length=50) state = models.CharField(max_length=100) zipcode = models.IntegerField(null=True, blank=True) latitude = models.FloatField(null=True, blank=True) longitude = models.FloatField() _current_occupancy = models.IntegerField(default=0, db_column='current_occupancy') def __str__(self): return self.address @property def current_occupancy(self): scanner_objs = Scanner.objects.filter(location=self) num_scans = 0 for s in scanner_objs: today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min) today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max) scan_objs = Scan.objects.filter(scanner=s, datetime__range=(today_min,today_max)) num_scans += len(scan_objs) return num_scans class Scanner(models.Model): location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True,related_name='scanner') description = models.CharField(max_length=300, null=True, blank=True) def __str__(self): return self.location.address + ": " + self.description + " (id=" + str(self.pk) + ")" class Scan(models.Model): scanner = models.ForeignKey(Scanner, on_delete=models.CASCADE, null=True, blank=True,related_name='scan') datetime = models.DateTimeField(null=True, blank=True, default=datetime.datetime.now) def __str__(self): if self.scanner != None: return self.scanner.description + ": " + str(self.datetime) else: return "None: " + str(self.datetime) class Meta: ordering = ('datetime',) I'm writing code locally and pushing to Heroku with Git. When I create any object locally and push it, the object saves correctly, … -
MySQL's query is stuck and cpu is 100% on live but not on local
I have a problem with certain pages on Django Admin that make MySQL's query stuck (cpu is 100%). A couple of weeks ago, I had to convert all tables from MyISAM to InnoDB. This operation worked fine. But on certain pages, the request was frozen. After some investigations, I found out that it was MySQL's query. I noticed that foreign keys were missing. So I added the missing foreign keys and the query worked. It works on my machine (localhost) but not on live. Every time I load the page on live, I have to restart the MySQL server. I ran the query via MySQL workbench: on local: OK on live: stuck I'm using AWS (EC2 with RDS) with Django 1.10.5 and MySQL 5.7.x I am stuck on finding a solution and I don't know what to do now or to look for. Please advise. -
Django rest - Serialize nested objects
I am using Django 1.10.5 and djangorestframework 3.5.3. I have 2 models that are related with one to many relation: class Minisymposium(models.Model): STATUS_CHOICES = ( ('pending', 'Pending'), ('approved', 'Approved'), ('denied', 'Denied')) id = models.AutoField(primary_key=True) number = models.IntegerField(default=0, null=False) title = models.CharField(max_length=100, null=False) description = models.TextField(max_length=9000, null=False) status = models.CharField(max_length=100, choices=STATUS_CHOICES, null=False, default='pending') user = models.ForeignKey(User, null=False, related_name='user') corresponding_organizer = models.ForeignKey(User, null=False, related_name='corresponding_organizer') anticipated_abstracts = models.IntegerField(null=False) anticipated_attendees = models.IntegerField(null=False) date = models.DateTimeField(auto_now_add=True) def __str__(self): return '{0}-{1}'.format(self.number, self.title) class UnregisteredOrganizer(models.Model): id = models.AutoField(primary_key=True) first_name = models.CharField(max_length=1000, blank=False) last_name = models.CharField(max_length=1000, blank=False) email = models.EmailField(max_length=254, blank=True) affiliation = models.CharField(max_length=254, help_text='(institution only)', blank=True) phone_number = PhoneNumberField(blank=True) minisymposium = models.ForeignKey(Minisymposium, related_name='other_organizers') Each model have a serializer. But the problem is with Minisymposium`s serializer. Because I want to send an UnregisteredOrganizer`s ID on creating one, and get the whole object as serialized on getting a Minisymposium. And as I see in ModelSerializer it is not possible: class MinisymposiumSerializer(serializers.ModelSerializer): other_organizers = UnregisteredOrganizerSerializer(many=True) class Meta: model = Minisymposium fields = ('url', 'id', 'number', 'title', 'description', 'status', 'user', 'corresponding_organizer', 'anticipated_abstracts', 'anticipated_attendees', 'other_organizers', 'date') def create(self, validated_data): other_organizers = [] if 'other_organizers' in validated_data: other_organizers = validated_data.pop('other_organizers') minisymposium = Minisymposium.objects.create(**validated_data) minisymposium.save() for organizer in other_organizers: UnregisteredOrganizer.objects.create(minisymposium=minisymposium, **organizer).save() return minisymposium How … -
Disable django allauth confirm email during social login
I've setup django-allauth and for so far linkedin, facebook and twitter after oauth2 confirms and redirects back it brings a form asking you to confirm email. I've tried ACCOUNT_EMAIL_VERIFICATION = False doesn't remove it. -
Reverse for 'view_item' with arguments '('',)' and keyword arguments '{}' not found
I am making a shopping website using django 1.4, I am displaying my products as a list view, when I try to pass the id of a product to a url pattern, I keep getting the below error. so my list of products does not load. Reverse for 'view_item' with arguments '('',)' and keyword arguments '{}' not found. list_products.html <td class="vertical-center"><a href="{% url view_item item_id %}">{{ item_id }}</a></td> urls.py url(r'^view_item/(?P<item_id>\w+)', 'view_item', name="view_item"), -
Python / Django / Apache2/ WSGI / UserDir / Conda: Django
I want to run 2 django applications on a linux server (Debian). These two project run well on my pc and on the server using the django command runserver. Here is what I did so far: I activated userdir: sudo a2enmod userdir I created two users: sudo useradd -g www-data -m user1 sudo useradd -g www-data -m user2su Then I created my two conda environments, imported from a file, with the same name as my users. I created two directories where I put my django projects /var/www/users/user1 and /var/www/users/user2. Then I create a conf file for each user: sudo nano /etc/apache2/conf-available/user1.conf with the following content WSGIDaemonProcess user1 user=user1 home=/var/www/users/user1 processes=1 threads=3 display-name=%{GROUP} python-path=/var/www/users/user1 python-home=/home/developer/anaconda3/envs/user1/lib/python3.4/site-packages WSGIScriptAlias /~user1 /var/www/users/user1/user1/user1/wsgi.py process-group=user1 WSGISocketPrefix /var/run/wsgi <Directory /var/www/users/user1> WSGIProcessGroup user1 </Directory> developer is the user that is used for development on the server. I also add these lines to my apache2.conf file: UserDir disabled UserDir enabled user1 user2 UserDir /var/www/users <Directory /var/www/users> AllowOverride FileInfo AuthConfig Limit Options +MultiViews +SymLinksIfOwnerMatch +ExecCGI -Includes -Indexes AddHandler wsgi-script wsgi Order allow,deny Allow from all </Directory> I tried to install wsgi with the package manager or directly from source in the corresponding conda environment. I always get the following error: from … -
Django returning incorrect attribute
I'm attempting to get Django to display a list of links to all records currently in the development database on the home/index page. my views.py code looks like: from django.http import HttpResponse from .models import GvData def index(request): all_geneva_data = GvData.objects.all() html = '' for data in all_geneva_data: url = '/geneva/' +str(GvData.id) + '/' html += '<a href="' + url + '">title</a><br>' return HttpResponse(html) I have a few sample records already in my database and I've confirmed by using the shell that each record does have an id and can be located with model.objects.filter(id='#'). This code succeeds in creating links but the link returns a page not found error. Instead of placing the integer value of the record id what's place in the url is: %3Cdjango.db.models.query_utils.DeferredAttribute%20object%20at%200x0452BF10%3E Why am I getting this instead of the int value of id cast as a string in the url pattern? Thanks for your time. -
Django says - No module named 'blog'
I am getting "ModuleNotFoundError: No module named 'blog'" error when i add my blog app to the INSTALLED_APPS section of settings.py. I have determined that it has something to do with the way i have added the "blog" app under INSTALLED_APPS. When i remove the 'blog' reference from INSTASLLED_APPS error goes away. It looks like that Django is unable to find directory for my blog app? I have done one thing differently and that is use: python manage.py startapp blog /myproject Difference here is specifying the /myproject directory and not using: python manage.py startapp blog Which will place it under root directory myproject. I wanted to avoid adding app directory in the root folder so i stay more organized. But it looks like Django does not like this or i am not referencing this correctly in the INSTALLED_APPS section? My project directory is as fallowing: myproject/ ├── myproject │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── settings.cpython-36.pyc │ │ ├── urls.cpython-36.pyc │ │ └── wsgi.cpython-36.pyc │ ├── blog │ │ ├── __init__.py │ │ ├── admin.py │ │ ├── apps.py │ │ ├── migrations │ │ │ └── __init__.py │ │ ├── models.py │ │ … -
Convert CSV to excel using xlsxwriter and save it to django Model
Now I don't have any problems about converting this csv or downloading it I have a problem saving it to django model at filefield The minmized sample code: def download_convert_reports_s3_temp(): def get_report_url(): bucket_name = 'temp_bucket' conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.get_bucket(bucket_name) key = bucket.get_key('TEMP_2017-01-10.csv') return key.generate_url(expires_in=600) def get_doc(): return Doc.objects.get(owner=User.objects.first()) def get_file(file): file_temp = NamedTemporaryFile(delete=True) file_temp.write(file.content) file_temp.flush() return File(file_temp) def convert_csv_to_xlsx(): request = requests.get(get_report_url()) csvfile = get_file(request) from django.conf import settings excelFile = xlsxwriter.Workbook('report.xlsx', { 'strings_to_numbers': True, 'default_date_format': 'yy/mm/dd', 'tmpdir': settings.MEDIA_ROOT + '/documents/%s'.format(file.name.rsplit('.')[0] + '.xlsx') } excelFile = get_doc().file worksheet = excelFile.add_worksheet() worksheet.write('A1', 'data') worksheet.write('B1', 'data') worksheet.write('C1', 'data') worksheet.write('D1', 'data') worksheet.write('E1', 'data') # Start from the first cell. Rows and columns are zero indexed. row = 1 col = 0 with open(csvfile, 'rb') as f: content = csv.reader(f) # Iterate over the data and write it out row by row. for row_data in content: for data in row_data: worksheet.write(row, col, data) col += 1 row += 1 col = 0 f.close() excelFile.close() return convert_csv_to_xlsx() Now the problem that I really don't know to to save this excel file to the doc.file, I tried many ways, Any suggestion Thanks -
Django import-export generate username before user csv upload
I need to bulk generate users for my app from a csv file. The csv I have contains the following fields: last_name, first_name, email To generate the username I have a function which I was hoping to use in a 'dehydrate' method like: def dehydrate_username(self, user): # username = generate_username_function here return username It turns out that dehydrate works only for exports, not for imports. How do I inject my username field in the csv import with django import-export? -
Django allauth get full linkedin details
Using django-allauth with linkedin however can't get full profile details from profile of the user. in the settings file SOCIALACCOUNT_PROVIDERS = { 'stackexchange': { 'SITE': 'stackoverflow' }, 'linkedin':{'SCOPE': ['r_fullprofile', 'r_emailaddress']} } and on the account I check extra_data and get {u'firstName': u'Samuel', u'lastName': u'Muiruri', u'pictureUrls': {u'_total': 1, u'values': [u'https://media.licdn.com/mpr/mprx/0_C1AR7HY-6mvzKOYWkCPzody-62WvzgOW5_7nWd8yaZHnlyuFFiKcIigGyxn']}, u'pictureUrl': u'https://media.licdn.com/mpr/mprx/0_dCvykIHSymsQZ-MUdbc_kWwSpuuw4-4UI5TikeHriWZsFnfR5k5KoHyO-G2vUzJBHTBCwfVnu3gN', u'emailAddress': u'muiruri.samuel@gmail.com', u'publicProfileUrl': u'https://www.linkedin.com/in/samuel-muiruri-a5235532', u'id': u'_xLBtzYkuK'} on linkedin dev oauth page I've selected email and r_basicprofile -
combination django linux pam
i understand howto configure linux pam as backend https://github.com/ftao/django-pam http://django-pam.readthedocs.io/en/latest/modules.html it works fine, and users already created in linux can by authentificated... but i want more.. i want django to create user in linux if it is created in in django... do anyone knows what libray use ? Or howto extend django-pam? -
Different Virtual Environment for multiple projects
I'm new to environmental variable issues. I work on django. I saw some projects source code getting data from environmental variable. I successfully setup the variables. But problem is the variables setup globally. When I call the variable from another project same variables value. How can I setup the variable for virtual environment only? -
Django - putting HTML tags inside blocks of text subject to translation
I am translating my web site from English into Russian (using Django 1.9) and have screened the doc several times, and I still can't solve the issue I have the following snippet in my template: {% blocktrans %} Your activation key is: <strong>{{activation_key}}</strong> {% endblocktrans %} After running makemessages, my .po file looks like this: msgid "Your activation key is: <strong>%(activation_key)s</strong>" msgstr "Ключ для активации Вашего аккаунта: %(activation_key)s." The problem is that after running the compilemessages command, the phrase stays in English in Russian version of the web site (in other words, the phrase is not translated into Russian). Please note that the problem lies in <strong> tag. If I remove it from the template, all works just fine. I've also tried to remove <strong> from the .po automatically-generated msgid so that the .po file would like this: msgid "Your activation key is: %(activation_key)s" msgstr "Ключ для активации Вашего аккаунта: %(activation_key)s." ...but this does not help either. Does Django provide a way to include HTML tags in translation phrases? And if not, what is the cleanest work-around ? -
Custom comments for viewset in api documentation
I have django rest framework api application and generate documentation for it. I use api view with viewset and I have custom method. In urls I use routers. For generate documentation I use DRF Docs. My view: class UserViewSet(viewsets.ModelViewSet): """View for user object. """ ... @detail_route(methods=["post"]) def set_password(self, request, pk=None): """View to set new password """ ... urls: from django.conf.urls import url, include from rest_framework.routers import DefaultRouter from accounts.views import UserViewSet __all__ = ["accounts_urlpatterns"] accounts_router = DefaultRouter() accounts_router.register(r'users', UserViewSet) accounts_urlpatterns = [ url(r'^accounts/', include(accounts_router.urls)) ] My api documentation result: I want custom comments for set_password method and display different fields because I use for this method other serializer. How can I do that? -
Django-easy-pdf, header on every page of the document
I am trying to export some data in a PDF file. I am using: Django 1.9.12 django-easy-pdf 0.1.0 Python 2.7 The export works fine and all (no problem with my view) but I am struggling with adding a page header into every page of the document. At this point I can only render it on the first page. I have no such problem with the footer, it renders properly on every page. My template is as follows: {% extends "easy_pdf/base.html" %} {% block extra_style %} <style type="text/css"> @page { size: landscape; margin-left: 1cm; margin-right: 1cm; margin-top: 1.5cm; margin-bottom: 2cm; @frame footer { -pdf-frame-content: page-footer; bottom: 0cm; margin-left: 1cm; margin-right: 1cm; height: 2cm; } } </style> {% endblock %} {% block page_header %} {% include "document_head.html" %} {% endblock %} {% block content %} {% include "main_table.html" %} {% endblock %} {% block page_foot %} {% include "document_foot.html" %} {% endblock %} Doing the blocks without include makes no difference. I have rewritten some base styling from base.html according to my needs (page and footer). I am not sure if I understand the functionality of the header properly but in my opinion it should be rendered on every page (similar to … -
django smart_select app organization
I have three mysql tables without pk’s assigned, table A contains unique names, B contains distinct names and value 1 (longer than table 1), and table C is distinct values of names, value 1 and value 2 (longer than table B). I am struggling to define models and form for these relationships to create a form (Django form or html select form) to do what django smart_selects promises to do. https://github.com/digi604/django-smart-selects, I have followed the documentation and many answers on the net. But my app is still not working. here is my code: there may be typo's or spacing issues on this post, but those are not the cause of errors in my app. I am using python 2.7, django 1, 10, 0 smart_selects is added to settings, project url (not app url), etc. models.py class Names(models.Model): name = models.CharField(max_length=150, unique=True) class Meta: db_table = 'A' class Foo(models.Model): Name = models.ForeignKey(Names, to_field = 'name', db_column='name')# need to assign db_column or get error Name_id in fieldlist, it is possible that “to_field” is not needed here. But I am not sure Val1 = models.BigIntegerField(blank=True, null=True) class Meta: db_table = 'B' class Foo2(models.Model): name = models.ForeignKey(Names, db_column='name', to_field="name", related_name = 'name_') VAL1 = … -
Putting an init method on an InLineFormSet
I have used an InLineFormSet to display all of the records that relate to a particular timesheet. The user can add many new forms on the page, I've used ajax and html to append an empty existing_form on the bottom with new IDs. However saving this is a problem.... because the newly added forms do not have timesheet_id assigned yet, they don't save as part of existing_forms. I am trying to put an init method together to assign this when the form is created, hopefully solving this problem. (?) 1) Do I have to save the record before it will become part of existing_formset? 2) I have excluded the timesheet_id from the TimeForm.... this means I assume form.timesheet_id = var will not work... and I would have to use obj.timesheet_id... but can I do this in the init of a form?? very confused. 3) Is it easier to use a new formset for newly added forms and use modelformset_factory..... View: class CustomInlineFormSet(BaseInlineFormSet): def clean(self): super(CustomInlineFormSet, self).clean() timesheet = TimeSheet.objects.get(pk=timesheet_id) for form in self.forms: form.empty_permitted = True def __init__(self, request, *args, **kwargs): super(CustomInlineFormSet, self).__init__(*args,**kwargs) # the following won't work because it's excluded from the form? # self.fields['timesheet_id'] = forms....... oh no … -
django-allauth: Only allow users stored in a database
I was able to log in user using django-allauth, but my specific requirements is that, I create user manually in my application, and I want only those users to be able to login using django-allauth. Is there a way to override callback view? or something else can be done? -
Media Url returns error 500 django
have the following problem, I have in my settings.py configured in the following way: RUTA_PROYECTO = os.path.dirname(os.path.realpath(__file__)) MEDIA_ROOT = os.path.join(RUTA_PROYECTO,'fotos') MEDIA_URL = '/media/' In my urls file I have: from django.conf.urls.static import static Urlpatterns = patterns ('', .... ) + static (settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) I have in my models def upload_name(instance, filename): return '{}/{}'.format(instance.persona.nro_doc, filename) class FotosPersona(models.Model): persona = models.ForeignKey(Personas,related_name='fotos_persona') tipo_foto = models.CharField(max_length=25) foto = models.ImageField(upload_to=upload_name) fecha = models.DateField(auto_now=True) class Meta: db_table = 'fotos_persona' When I save the data, the image is stored in a folder within the following structure: Project | | |----Project |---- settings.py |---- media |---- urls.py ..... But when I want to display the image in a template, it is not displayed. When in the browser I entered the http://localhost:8000/media/other_folder/image_file.jpg url it returns me an HTTP 500 error. -
django change content in a tab with data from a model
i'm trying to show content in a tab from a model in a template, the problem is I do not know how to show the content depending on the option to which you click, as I'm using a for in the template it only me Shows the contents of the last record in the table, someone can help me or suggest what I can do my model class Beneficio(models.Model): autor = models.ForeignKey('auth.User') OP_DEP = ( ('Afiliacion', 'Afiliacion'), ('Bienestar y Seguridad Social', 'Bienestar y Seguridad Social'), ('Beneficios del Trabajador', 'Beneficios del Trabajador'), ('Créditos', 'Créditos'), ('SISA', 'SISA'), ('Inversora', 'Inversora'), ) dependencia = models.CharField( max_length=100, choices=OP_DEP, default='AA' ) beneficio = models.CharField(max_length=100, unique=True) texto = HTMLField('Texto') fecha_creacion = models.DateTimeField(default=timezone.now) fecha_publicacion = models.DateTimeField(blank=True, null=True) def __str__(self): return self.dependencia my view def bafiliacion(request): afi = Beneficio.objects.filter( fecha_publicacion__lte=timezone.now()) return render(request, 'noticias/beneafiliacion.html', {'afi': afi}) my template <div class="Beneficios"> <div class="container-fluid"> <div class="col-md-4"> <ul class="nav nav-tabs tabs-left"> {% for item in afi %} <li><a href="#{{item.beneficio}}" data-toggle="tab"> {{item.beneficio}}</a></li> {% endfor %} </ul> </div> <div class="col-md-8"> <div class="tab-content"> {% for item in afi %} <div class="tab-pane" id="{{item.beneficio}}">{{item.texto|safe}}</div> {% endfor %} </div> </div> -
Django, Getting a value from a POST
I have in my template: This is passed by {{form}} <form action="" method="POST"> Inicio: <input type="text" id="start"> <input type="submit" value="Sned" > {% csrf_token %} </form> Then in the views.py def test(request): if request.method != 'POST': context = {'form': 'by GET'} return render(request, 'test.html', context) else: if 'start' in request.POST: start = request.POST['start'] else: start = False context = {'form': start} return render(request, 'test.html', context) It seems that always return False If I dont check the existance of the key I have this error: MultiValueDictKeyError And the erropage says : "'start'" (single plus double quotes) -
ImportError: No module named post_home
I was trying to get my HttpResponse from my views.py from django.http import HttpResponse from django.shortcuts import render # Create your views here. def post_home(request): return HttpResponse("<h1>GWorld</h1>") So i tried to import it from my folder app to the urls.py which is located in the other folder from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^posts/$', include("posts.views.post_home")), ] But then i received an error File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) ImportError: No module named post_home I'm not really sure what happen because i'm pretty sure that i wrote the function in the views.py file. i tried using url(r'^posts/', include("posts.views.post_home")), But i received the same error. -
Postgres: More verbose error message: I am missing the table name
I see this message: IntegrityError: null value in column "date" violates not-null constraint DETAIL: Failing row contains (10005, null, f, TEST, MAIL). Is there a way to get a more verbose error message from PostgreSQL? I am missing the table name. I am using the database via django orm. -
Re-use the same template for a similar job
I ask to a client to provide us a list of documents, e.g., ['doc1', 'doc2', 'doc3', 'doc4', 'doc5']. From this time, each 24 hours for 5 days, we send an email to the client informing him of missing documents : Hello 'Mr or Ms', We inform you that the following documents are missing to complete the request: 'list a missing documents' I was thinking that I could use a filter on that list and apply a cron job to reapply that filter every 24 hours for 5 days. For instance, if the client has provided all the documents from the first day, then we send him a message telling him that all the documents have been delivered successfully and that he can proceed to the next step. As I program since just some days, I need your help to resolve my problem. I'm thinking that It is not the better solution. At the beginning I thought to create five different template with slug send-remaining-documents-day-one, send-remaining-documents-day-two, ..., but it is not suggested I guess. Could anyone be able to suggest a better solution than that? Thanks in advance!