Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Using custom Manager & QuerySet does not work with related objects properly
Given two simple models: class Employee(Model): user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True) class AttendanceLog(Model): from_datetime = models.DateTimeField(_('from')) to_datetime = models.DateTimeField(_('to')) employee = models.ForeignKey(Employee, on_delete=models.CASCADE, verbose_name=_('employee')) We can retrieve some_employee's attendance logs just fine: some_employee.attendancelog_set. We can also use custom queryset in attendance log (objects = AttendanceLogQuerySet.as_manager()), such as: class AttendanceLogQuerySet(models.QuerySet): def today(self): # returns logs that has to_datetime with today's date. return self.filter( to_datetime__date=date.today() ) And so we can retrieve employee's attendance for today by doing:some_employee.attendancelog_set.today() The problem is when we try to use both custom Manager and custom Queryset (objects = AttendanceLogManager()), such as: class AttendanceLogQuerySet(models.QuerySet): def today(self): # returns logs that has to_datetime with today's date. return self.filter( to_datetime__date=datetime.today() ) class AttendanceLogManager(models.Manager): def get_queryset(self): return AttendanceLogQuerySet(self.model, using=self._db) def some_special_create_method(self, some_args): pass We get an error: AttributeError: 'RelatedManager' object has no attribute 'today' This should work according to documentation on how to use both QuerySet and a Manager. -
How to serve static files in nginx without hitting my gunicorn/django server?
I've have an nginx/gunicorn/django setup by following parts one and two of this guide, and it works fine. Now what I've tried to do is include my own static files that I created by building a VueJS project. My static folder now contains an admin folder (automatically put there by following the steps in part two of the guide) and a login folder. My file structure looks like this: - myproject - myproject - __pycache__ - __init.py__ - settings.py - urls.py - wsgi.py - static - admin - css - fonts - img - js - login - index.html - static - css - img - js -db.sqlite3 - manage.py The most recent iteration of my nginx configuration file looks like this: server { listen 8000; server_name 0.0.0.0; rewrite_log on; error_log /home/vagrant/debug.log debug; root /sync/genetesis/github/cardioflux-webapp/myproject; location = /favicon.ico { access_log off; log_not_found off; } location /static { alias /sync/genetesis/github/cardioflux-webapp/myproject/static; autoindex on; } location / { include proxy_params; proxy_pass http://0.0.0.0:8030; } } These are my results for hitting various urls: - When I hit http://0.0.0.0:8000, I see the Django "Congratulations!" page with the rocket ship. - When I hit http://0.0.0.0:8000/admin, I see the admin page, complete with all stylings. - When … -
python mock: mock a classmethod
When creating a Python unittest for a class, I'm looking for a way to mock/override the result of a classmethod on that class. Say I have a Django model Foo such as this, with the bar() classmethod, which is called by instance method baz(). # myapp/models.py class Foo(models.Model): ... @classmethod def bar(cls, name): return "bar_{}".format(name) def baz(self): r = Foo.bar("NAME") return "baz_{}".format(r) Then, my test calls baz(), which in turn calls the classmethod bar(), and I want to mock bar(). # myapp/tests.py from unittest.mock import patch from myapp.models import Foo from django.test import TestCase class MyTests(TestCase): def setUp(self): self.foo = Foo() self.foo.save() @patch("myapp.models.Foo.bar") def test__foo(self, mock_bar): # SETUP mock_bar.return_value = "bar_MOCKED_RESULT" # TEST result = self.foo.baz() # RESULTS # Desired Test self.assertEqual(result, "baz_bar_MOCKED_RESULT") # However, this is the actual value # result is "baz_bar_NAME" However, I've tried a number of variations similar to the above, but the mock never works and bar() always returns the un-mocked value. How can I properly mock the bar() classmethod in this scenario? (Note: though this example doesn't show it for simplicity, the Foo model is used in a number of tests in this test module, and I only want to override bar() on a … -
Having issues with getting Chosen.js to display in my Django template
I'm learning development and I'm using a linting tool with Atom called linter-jscs. I have a simple javascript file defined as the following: linter-jscs invalid line break $(document).ready(function () { $('#id_facilitydimselect').chosen(); } ) ; The linting tool shows the following: Is this really an error that will cause my code not to work? I can't seem to get rid of it. I'm trying to use the following Django template to use the chosen javascript library, but it is not displaying correctly. So, it's either due to my error below or something else i'm not getting. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.js" type="text/javascript"></script> <script src= "{% static '/accounts/js/chosen.jquery.js' %}" type="text/javascript"></script> <script src= "{% static '/accounts/js/security_chosen.js' %}" type="text/javascript"></script> <link rel="stylesheet" href="{% static '/accounts/js/chosen.css' %}"> <div> <em>Facility: </em> <select facilitydimselect="Choose a Facility..." class="chosen-select" multiple tabindex="4"> {% for facility in facilitydim %} <option value="{{facility.coid_name}}">{{facility.coid_name}}</option> {% endfor %} </select> I know that Chosen is installed correctly using pip install django-chosen and I can see it in my GET statement in the console as shown below: 1679 [05/Jan/2018 13:54:23] "GET /static/accounts/js/security_chosen.js HTTP/1.1" 404 1703 [05/Jan/2018 13:54:23] "GET /static/accounts/js/chosen.jquery.js HTTP/1.1" 404 1697 [05/Jan/2018 13:54:23] "GET /static/accounts/js/security_chosen.js HTTP/1.1" 404 1703 What is really the issue? -
What's the best way to build a receive-only smtp server with python?
I want (actually I don't want but I think, I have to) to implement a lightweight SMTP server with Python to receive emails for my domain. Emails should only be received and not sent. I want to save the mails either on hard disk to continue working with them or manipulate them in-memory. Whats the best python library to implement this use case? I was also looking for non python email servers but these are always full blown servers with a lot of stuff and features, I actually don't need. -
Gunicorn Nginx host two websites - Gunicorn creates a sock file only for one
I have a Ubuntu server on which I'm trying to host two Django applications using Gunicorn and Nginx. When I have only one website hosted all works fine. Gunicorn creates a .sock file and nginx redirects to it. The problem I have is that I have a gunicorn config file for the second site and that doesn't create a .sock file therefore I get a bad gateway error. Here are my files: Gunicorn.conf description "David Bien com application" start on runlevel [2345] stop on runlevel [!2345] respawn setuid ubuntu setgid www-data chdir /home/ubuntu/davidbien exec venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/davidbien/davidbien.sock davidbiencom.wsgi:application The above file works fine. The second one: Gunicorn1.conf description "Projects" start on runlevel [2345] stop on runlevel [!2345] respawn setuid ubuntu setgid www-data chdir /home/user/dbprojects exec virtual/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/dbprojects/dbproject.sock dbproject.wsgi:application This one doesn't create the .sock file. I tried restarting gunicorn and even tried running the second file only by running sudo service gunicorn1 start but I get: start: Job failed to start In the logs there's nothing mentioning the second file running. What am I doing wrong? -
If user.is_authenticated don't work, if not user.is_anonymous works (Django)
I'm having troubles with the Django condition : {% if user.is_authenticated %} It return false if I'm logged in AND logged out. However, this condition : {% if not user.is_anonymous %} is perfectly fine and working as intended. Why ? -
Syntax Error When Trying to Open Downloaded Django Application
I am trying to work on a Django application that I downloaded from bitbucket which uses Python2-based code. Previously, Python 3.6 and Django 2.0 was on my system, so I downgraded to Python 2.7 and Django 1.11 to try to get it to work. When I go to the project directory and type in "python manage.py runserver," I get a syntax error (this is a shortened version of it, hence the dots): Unhandled exception in thread started by <function wrapper at 0x0000000006A0A358> Traceback (most recent call last): File "C:\Users\M\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper fn(*args, **kwargs) . . . File "C:\Users\M\Anaconda3\lib\site-packages\django\contrib\admindocs\urls.py", line 2, in <module> from django.contrib.admindocs import views File "C:\Users\M\Anaconda3\lib\site-packages\django\contrib\admindocs\views.py", line 9, in <module> from django.contrib.admindocs import utils File "C:\Users\M\Anaconda3\lib\site-packages\django\contrib\admindocs\utils.py", line 12, in <module> import docutils.core File "C:\Users\M\Anaconda3\lib\site-packages\docutils\core.py", line 246 print('\n::: Runtime settings:', file=self._stderr) ^ SyntaxError: invalid syntax If you have a solution to this problem, please let me know. -
Is there any way to use neo4django with django 2.0.1?
I have installed django 2.0.1, but when I install neo4django, it deinstalls that version and installs django 1.5.12. is there any way to prevent so? -
Extending custom router to default router across apps in Django Rest Framework
I have come across a problem regarding having the API apps seperate, while still being able to use the browsable API for navigation. I have previously used a seperate routers.py file in my main application containing the following extension of the DefaultRouter. class DefaultRouter(routers.DefaultRouter): def extend(self, router): self.registry.extend(router.registry) Followed by adding the other application routers like this: from . routers import DefaultRouter from app1.urls import router as app1_router # Default Router mainAppRouter = DefaultRouter() mainAppRouter.extend(app1_router) where the app1_router is a new SimpleRouter object. Now the problem occurs when I want to modify the SimpleRouter and create my own App1Router, such as this class App1Router(SimpleRouter): routes = [ Route( url = r'^{prefix}{trailing_slash}$', mapping = { 'get': 'retrieve', 'post': 'create', 'patch': 'partial_update', }, name = '{basename}-user', initkwargs = {} ), ] This will not handle my extension correctly. As an example, GET and PATCH are not recognized as allowed methods whenever I extend the router, but when I dont extend, but only use the custom router, everything works fine. My question is therefor, how can I handle extending custom routers across seperate applications, but still maintain a good browsable API? -
can't pickle dictionary in django
I have a simple dictionary that i am trying to save to cache and looks like it django is trying to pickle: items_list = [] item_obj['title'] = title item_obj['url'] = s2 item_obj['created_at'] = created_at item_obj['duration'] = duration items_list.append(item_obj) This has a very simple format that outputs: [{'title': "Podcast1", 'url': 'https://example.com\\n', 'created_at': 'Thu, 28 Dec 2017', 'duration': '00:30:34'}] I am running this from a custom management command like this: python3 manage.py podcast_job I attempt to save to cache: podcasts = get_podcasts() print(podcasts) cache.set('podcasts', podcasts) I get the error: File "podcast_job.py", line 13, in handle cache.set('podcasts', podcasts) File "python3.6/site-packages/django_redis/cache.py", line 33, in _decorator return method(self, *args, **kwargs) File "python3.6/site-packages/django_redis/cache.py", line 68, in set return self.client.set(*args, **kwargs) File "python3.6/site-packages/django_redis/client/default.py", line 109, in set nvalue = self.encode(value) File "python3.6/site-packages/django_redis/client/default.py", line 329, in encode value = self._serializer.dumps(value) File "python3.6/site-packages/django_redis/serializers/pickle.py", line 33, in dumps return pickle.dumps(value, self._pickle_version) RecursionError: maximum recursion depth exceeded while calling a Python object If I try to save with a string I get no error and it saves fine: cache.set('podcasts', str(podcasts)) How can I save the list of dictionaries and not get the error above? -
how to change index_start fo django-views
start_index() it jumps in the fourth page 21 fo 46. how to modify the start_index(). models.py class Articls(models.Model): articl = models.TextField() def __str__(self): return self.articl class Page(models.Model): numb_articls = models.IntegerField(default=11) ifo_page = models.CharField(max_length=100) def __str__(self): return str(self.numb_articls) views.py def articl(request): articl_list = Articls.objects.all() page = request.GET.get('page') les_page = Page.objects.get(pk=page) paginator = Paginator(articl_list, les_page.numb_articls) print(paginator.page(page).start_index()) print(paginator.page(page).end_index()) try: articls = paginator.page(page) except PageNotAnInteger: articls = paginator.page(1) except EmptyPage: articls = paginator.page(paginator.num_pages) context = { 'articls': articls, 'les_page': les_page, 'paginate': True } return render(request, '.../articls.html', context) on the terminal 1 7 [05/Jan/2018 19:52:24] "GET /?page=1 HTTP/1.1" 200 7087 8 14 [05/Jan/2018 19:52:28] "GET /?page=2 HTTP/1.1" 200 7729 15 21 [05/Jan/2018 19:52:45] "GET /?page=3 HTTP/1.1" 200 7891 46 60 [05/Jan/2018 19:52:49] "GET /?page=4 HTTP/1.1" 200 9767 for numb_articls = [ 7, 7, 7, 11] -
django-rosetta error: You can't use the CacheRosettaStorage
I'm using django-rosetta app, it works on development without a CACHES setting, but on prod I've this setting as follow: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', } } The problem is that under prod it raises me django.core.exceptions.ImproperlyConfigured: You can't use the CacheRosettaStorage if your cache isn't correctly set up, please double check your Django DATABASES setting and that the cache server is responding The database setting is simple as DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } -
Strange behaviour of request.POST in Django
I'm sending a POST request to a Django server through Postman. This is what the body of my request looks like POST /update/ HTTP/1.1 Host: 127.0.0.1:8000 Content-Type: multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW Cache-Control: no-cache Postman-Token: 0764e56c-0fd8-fcce-5248-34f7d05f2748 ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="UploadDownloadSettings" dssssss ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="settings"; filename="settings.zip" Content-Type: application/zip When I try to access request.POST['UploadDownloadSettings'], the program says the key is not valid. When I loop through the keys in request.POST I get a bunch of keys from the zip file and the key name. According to the docs, this should parse out form-data. This seems like standard form data as far as I understand. https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.HttpRequest.POST Am I misunderstanding the way the django post request works? -
Preview files with Python / Django
Is there a way to preview files (like .doc, .docx, .png, .jpg, .pdf) in the browser without using Google or microsoft URLs? Searching on google I can not find a solution that fits in this case. I can not use cloud solutions. -
How do I emit the HTML of an external website in a Django template?
I want to get the contents of one of my pages on an external site, the HTML and all contents. If were in .NET I could use WebClient and retrieve the page, save it to a variable, and emit it in Razor. PHP can use cURL. How can I do that in a Django template? Do I need to create a Plugin that uses urllib? If it matters, I am using DjangoCMS. I searched and tried the Django http module. I have looked at the helper Django module for http and didn't see anything. I wasn't sure where to put urllib or requests, as in I do not know if what I am trying to do requires me to build a plugin or a custom template tag. I was hoping to know how to do it with either Python and Django's template language or just the template language, so I really do not want to do this in JavaScript, iframe, object, etc. -
Using multiselect widget for a ForeignKey field Django
Good day i seem to be lost in how i want to implement something. so here is how it goes ive got a carowner model class carowner(models.Model): ..... carowner = models.CharField(primary_key=True) and a cars model class car(models.Model): .... carowner_id = models.ForeignKey(carowner) it has a foreignkey cause each car can only have one carowner but a carowner can have many cars but i run into a situation where ive got a modelform class assigncarownertocar(forms.ModelForm): car= forms.ModelChoiceField(widget=MultipleSelect()) class Meta: model=carowner fields = [# all fields that are needed ] what i want to happen is when im creating a new carowner asigns him multiple cars in the same form from a queryset i provided when the modelform is initialized. so everything loads fine in the form. i see the list of cars but once i select one or more i get this 'Select a valid choice. 539421 is not one of the available choices. what i am asking is should i add the modelmultiplechoicefield to the model that holds the foreign key? cause right now i have it attached to the carowner model. i dont see documentation stating that this is wrong -
storing python list of dictionaries in redis receiving recursion error
I have the following list of dictionaries: podcasts = [{'title': "Podcast1", 'url': 'https://example.com\\n', 'created_at': 'Thu, 28 Dec 2017', 'duration': '00:30:34'}] I attempt to save this with django redis with the following: from django.core.cache import cache cache.set('podcasts', podcasts) I get the error: File "jobs.py", line 13, in handle cache.set('podcasts', podcasts) File "python3.6/site-packages/django_redis/cache.py", line 33, in _decorator return method(self, *args, **kwargs) File "python3.6/site-packages/django_redis/cache.py", line 68, in set return self.client.set(*args, **kwargs) File "python3.6/site-packages/django_redis/client/default.py", line 109, in set nvalue = self.encode(value) File "python3.6/site-packages/django_redis/client/default.py", line 329, in encode value = self._serializer.dumps(value) File "python3.6/site-packages/django_redis/serializers/pickle.py", line 33, in dumps return pickle.dumps(value, self._pickle_version) ** RecursionError: maximum recursion depth exceeded while calling a Python object ** -
Django ModelForm foreign key filtering
I'm trying to filter foreign key field selections in my model form, but form isn't working. My scripts: forms.py from django import forms from .models import Album, Song class SongCreateForm(forms.ModelForm): class Meta: model = Song fields = [ 'album', 'name', 'is_favorite' ] widgets = { 'album': forms.Select(attrs={'class': 'form-control'}), 'name': forms.TextInput(attrs={'class': 'form-control'}), 'is_favorite': forms.CheckboxInput(attrs={'class': 'form-check-input'}), } def __init__(self, user, *args, **kwargs): super(SongCreateForm, self).__init__(*args, **kwargs) self.fields['album'].queryset = Album.objects.filter(owner=user) views.py from django.views.generic import CreateView class SongCreateView(CreateView): template_name = 'music/song_create.html' success_url = '/songs/' def get_form(self, form_class=None): form_class = SongCreateForm(user=self.request.user) return form_class I've tried to identify the problem in console and here is the result: from music.forms import SongCreateForm from music.models import Album from django.contrib.auth import get_user_model UserModel = get_user_model() user = UserModel.objects.get(pk=22) album = Album.objects.get(pk=1) song_form = SongCreateForm(user=user, album=album, name='something', is_favorite=True) Traceback (most recent call last): File "/home/elgin/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-8-e0507c59917a>", line 1, in <module> song_form = SongCreateForm(user=user, album=album, name='something', is_favorite=True) File "/home/elgin/Desktop/python/pycharm/django/muplay/music/forms.py", line 40, in __init__ super(SongCreateForm, self).__init__(*args, **kwargs) TypeError: __init__() got an unexpected keyword argument 'album' What am I doing wrong? -
With django-ckeditor how would I dynamically set CKEDITOR_FILENAME_GENERATOR based on app name?
I'd like to be able to have a custom CKEDITOR_FILENAME_GENERATOR for one particular app while also having a default for all other apps. My first though was to dynamically set by app name somehow. Would something like the following be possible: #settings.py ... def filename_generator(): fng = 'myproject.custom_filename' if app_name = "blog" #<<<--- how would I achieve something like this? fng = 'blog.custom_filename' return fng CKEDITOR_FILENAME_GENERATOR = filename_generator() ... -
object has no attribute ***_set
I'm writing an app to record and what clothes a user wears each day. A user logs in, a Checkin object is created, and then the user is sent to a page where they can click buttons to add what clothes they wore that day. When a user selects Jeans, it creates a Jeans object that references the User and the current Checkin. I want the Jeans button to disappear once it is selected. In the template I want to reference {% if not checkin.jeans_set.all %} Jeans Link {% endif %} checkin.jeans_set.all always evaluates to False when run in the template. I'm getting the error: 'Checkin' object has no attribute 'jeans_set' I can reference {{ request.user.checkin_set.all }} without issue in the template. class Checkin(models.Model): user = models.ForeignKey('auth.User') date = models.DateField(auto_now_add=True) description = models.CharField(max_length=40) class ClothesBase(models.Model): checkin = models.ForeignKey(Checkin, on_delete=models.CASCADE) user = models.ForeignKey('auth.User') class Meta: unique_together = ('user', 'checkin') class Jeans(ClothesBase): def __str__(self): return 'Jeans' class Shorts(ClothesBase): def __str__(self): return 'Shorts' -
Iterate through Django many-to-many set in the order model instances are added
I currently have a Django model that has a many-to-many-relationship: class Pool(models.Model): event = models.OneToOneField(Event) participants = models.ManyToManyField(Player, null=True) Participants are added in a specific order, and then at various times participants will be added and removed. For example, we add participants 1 through 7 in that order, and then remove participants 1 and 4, and then add participant 8. So the participant order by time they were added to Pool is: 2, 3, 5, 6, 7, 8 I want to be able to iterate through the participants in that same order. will pool.participant_set.all() return them in that order? If not, is there any in-build way to order by when participants were added to the Pool? -
Django email not working in production
I have normal forms not modelform where a user fills up the details and submits it. then the data from the form is cleaned and emailed, as the POST request. I have configured a gmail account and it worked fine in development stage however I'm not receiving any emails in production. 1.there is no error 2.seems like its working cos the redirect page is appearing after submission of the form.(if it fails then I should see a different redirected page) I have received no email whatsoever neither from my website nor google. EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'abcd@gmail.com' EMAIL_HOST_PASSWORD =********** EMAIL_PORT = 587 EMAIL_USE_TLS = True that's the setup in settings.py views.py subject = 'General Inquiry' from_email = settings.EMAIL_HOST_USER to_email = [email] contact_message = """ Name: {} Email: {} Phone: {} Region: {} Customer's Message: {}""" .format(name, email, phone,region, message) send_mail(subject, contact_message, from_email, to_email, fail_silently=True) return redirect('upload:contact') Am I missing out on something??? -
How to store env variables other than inside gunicorn.service for a Django project?
I tried putting all my environment variables for my Django project inside ~/.bash_profile and then use it in gunicorn.service with EnvironmentFile=/home/user/.bas... but with no luck. Eventually I had to put everything in ExecStart with -e in front of every variable, also with SECRET_KEY and everything in plain text. Is it safe, is it possible to access this externally or how else could I do this? -
Python 3 / Django 2, Passing a dictionary value to form field before saving
I'm trying to get my app to use a serial number, entered on a form, to compare to a dictionary and update a product field on a form before saving. Here's the details: Dictionary is two columns, 'Product Code', and 'Product' The first three digits of the Serial number entered on the form will match to the Product Code in the dictionary. Once the user submits the form, I want it to evaluate the serial number's first three digits, compare it to the dictionary keys, then change the 'product' form field to the dict['Product'] before saving the form. Here is my view: def create(request): #Page with input form to submit new records if request.method == 'POST': form = RecordForm(request.POST) if form.is_valid(): sn = request.POST.get('serial') with open('prodlist.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: if sn[0:2] in row['Product Code']: form.fields['product'].update = row['Product'] else: pass form.save() return HttpResponseRedirect('/tracker/all/') else: form = RecordForm() return render(request, 'tracker/form2.html', {'form': form}, RequestContext(request)) With this, I'm geting a key error for the 'product' piece. Is there a way to pass a value to a form field after it is submitted?