Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Angular 5 deployment in Django app with hashing
I have a Django app that has an Angular 5 frontend connected by Django REST. Currently deployment only works with hashing disabled (--output-hashing none), because in uls.py I can only access a copy of index.html ("ang_home.html") in the template folder. ng build --prod --output-path .../backend/src/static/ --watch --output-hashing none --aot=false --env=prod I would now like to change my urls.py so that the newly created index. html in the django static folder is accessed directly. url(r'^.*', TemplateView.as_view(template_name="ang_home.html"), name='home'), The previous current solution is annoying because I would have to copy the index.html code into ang_home.html manually with every change. -
Django Cassandra engine timeout when creating data using models
I have a Django app running in a docker swarm. I'm trying to connect it with cassandra using django-cassandra-engine. Following the guides, I've configured installed apps and connection settings, and running the first step works great (manage.py sync_cassandra). It created the keyspace and my models. However, whenever I try to create data using the models or a raw query, there's simply a timeout while connecting without any other errors. Swarm is running on AWS with a custom VPC. Docker flow proxy is used as reverse proxy for the setup (not that it should affect the connection in any way) I've tried deploying cassandra both as a standalone and as a docker image. I am also able to ping the servers both ways. I am even able to manually connect to the django app container, install cassandra and connect to the cassandra cluster using cqlsh I've been banging my head off for the past few days around this... Has anyone encountered something similar? Any ideas as to where I can start digging Feel free to ask for any information you think may be relevant. -
Django do not validate select multiple
I am trying to disable validation on one manually created form field as per the below. The reason being I am using jquery to move a select option from one select multiple list to another. When the option is moved from available to assigned list and I submit the form I get an error stating that the id of the moved option is not a valid. which I can understand why. however I need for this to be ignored. I've searched and believed I could use clean_XX() but it doesn't seem to work. I still get the validation errors. error: 267 is the value of one of the subnets I moved across from the available list This is my form: class DeviceForm(forms.ModelForm): class Meta: model = DeviceData fields = ['site_data', 'switches', 'hostname', 'template', 'model', 'install_date','ospf_area','snmp_data'] def __init__(self, *args, **kwargs): site_id = kwargs.pop('site_id', None) device_id = kwargs.pop('device_id', None) self.is_add = kwargs.pop("is_add", False) super(DeviceForm, self).__init__(*args, **kwargs) def clean_assigned_subnets(self): assigned_subnets = self.cleaned_data['assigned_subnets'] return assigned_subnets devicesubnet = Subnets.objects.filter(devicesubnets__device_id=device_id) sitesubnet = Subnets.objects.filter(sitesubnets__site_id=site_id) common_subnets = list(set(devicesubnet) & set(sitesubnet)) subnet_id_list = [] for s in common_subnets: subnet_id_list.append(s.id) available_subnet_data = sitesubnet.exclude(id__in=subnet_id_list) assigned_choices = [] devicesubnet_data = DeviceSubnets.objects.filter(device_id=device_id) for choice in devicesubnet_data: assigned_choices.append((choice.subnet.id,choice.subnet.subnet)) self.fields['available_subnets'] = forms.ModelMultipleChoiceField( label='Available Subnets', queryset=available_subnet_data, … -
Annotating a Django queryset through two relations
I have models that represent People and Books, which are joined via a through-model that can indicate their role (e.g. 'Author' or 'Editor'): from django.db import models class Person(models.Model): name = models.CharField() class Book(models.Model): title = models.ChartField() people = models.ManyToManyField('Person', through='BookRole', related_name='books') class BookRole(models.Model): person = models.ForeignKey('Person', related_name='book_roles') book = models.ForeignKey('Book', related_name='roles') role_name = models.CharField() And I have a Reading model that represents an occasion a Book was read: class Reading(models.Model): book = models.ForeignKey('Book') start_date = models.DateField() end_date = models.DateField() I know how to get a list of Persons ordered by the quantity Books associated with them: from django.db.models import Count from .models import Person Person.objects.annotate(num_books=Count('books')) \ .order_by('-num_books') But how can I get a list of Persons ordered by the number of times their Books have been read (i.e. the number of Readings related to their Books)? -
Django channels 2, accessing db in tests
I recently updated my project to Django 2 and channels 2. Right now I am trying to rewrite my tests for chat app. I am facing a problem with tests that depend on django db mark from pytest-django. I tried to create objects in fixtures, setup methods, in test function itself, using async_to_sync on WebsocketCommunicator. However, none of those worked. If I create a user in a fixture and save it correctly gets an id. However, in my consumer Django does not see that User in the database. And treat it like an anonymous user. I have a temporary token which I use to authenticate a user on websocket.connect. @pytest.fixture def room(): room = generate_room() room.save() return room @pytest.fixture def room_with_user(room, normal_user): room.users.add(normal_user) yield room room.users.remove(normal_user) @pytest.fixture def normal_user(): user = generate_user() user.save() return user @pytest.mark.django_db class TestConnect: @pytest.mark.asyncio async def test_get_connected_client(self, path, room_with_user, temp_token): assert get_path(room_with_user.id) == path communicator = QSWebsocketCommunicator(application, path, query_string=get_query_string(temp_token)) connected, subprotocol = await communicator.connect() assert connected await communicator.disconnect() Consumer: class ChatConsumer(JsonWebsocketConsumer): def connect(self): # Called on connection. Either call self.user = self.scope['user'] self.room_id = self.scope['url_route']['kwargs']['room_id'] group = f'room_{self.room_id}' users = list(User.objects.all()) # no users here self.group_name = group if not (self.user is not None and … -
heroku django timestamps not converting to UTC
I've got a Django app (running on Heroku) with a simple form that contains a DateTimeField(): class Response(models.Model): ''' Model serves to record a users response to a given question on a given questionnaire. ''' user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.PROTECT, default=None, db_index=True, ) date = models.DateTimeField( blank=False, auto_now_add=True, db_index=True, help_text=( "Date on which question was answered." ) ) From what I understand, my date field should get populated automatically with the current datetime For DateTimeField: default=timezone.now - from django.utils.timezone.now() -- ref Furthermore, it should be a UTC timestamp If USE_TZ is True, this will be an aware datetime representing the current time in UTC. -- ref My settings.py has TIME_ZONE = 'UTC' USE_TZ = True And I've got some middleware that sets current time zone to CST: from django.utils import timezone import pytz class TimezoneMiddleware(object): def process_request(self, request): if request.user.is_authenticated(): timezone.activate(pytz.timezone('America/Chicago')) else: timezone.deactivate() I have some debugging print statements used right before the form submit [print(now(), localtime(now()), get_current_timezone())] to validate that now() is actually UTC. When I submit my form I'm seeing: 2018-03-06T16:56:23.569311+00:00 app[web.1]: 2018-03-06 16:56:23.569131+00:00 2018-03-06 10:56:23.569145-06:00 America/Chicago however the database stores 2018-03-06 10:56:23 How can I ensure that my submitted timestamps properly get converted to UTC? -
Double nested inputs in django request
So there's this question and it's answered but for PHP. Is it possible to make double nested inputs in html and parse it with Django? I managed to make it work only with one level and request.POST.getlist works fine if you just supply similar names. But if I follow that answer I get empty list. -
Django: Images inside conditional template tags
I'm trying to display an image based on certain conditions in a Django template, but Django doesn't seem to like use of the static tag within a condition. Code as follows: <td> {% if result.threshold == "normal" %} <img src="{% static "face-green-small.jpg" %}" alt="Green"/> {% endif %} {% if result.threshold == "high" or result.threshold == "low" %} <img src="{% static "face-amber-small.jpg" %}" alt="Amber"/> {% endif %} {% if result.thresdholf == "vhigh" or result.threshold == "vlow" %} <img src="{% static "face-red-small.jpg" %}" alt="Red"/> {% endif %}"> </td> The error I'm getting is: Invalid block tag on line 32: 'static', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag? I'm sure static is registered, because it's being used earlier in the template. Any guidance on what I'm doing wrong would be appreciated. -
Creating docker secrets with user variables within django
I have django application that uses docker to create sessions using some user inputs. The spec for the services look something like this: { "Name": "dummy_service", "TaskTemplate": { "ContainerSpec": { "Image": REGISTRY_HOST + '/' + DEFAULT_IMAGE + ':latest', "Command": ["start-notebook.sh"], "Env": [LIST OF ENV VARS], "User": "root" }, "RestartPolicy": { "Condition": "on-failure", "Delay": 10000000000, "MaxAttempts": 10 }, }, "Mode": { "Replicated": { "Replicas": 1 } }, "EndpointSpec": { "Ports": [ { "Protocol": "tcp", "TargetPort": 8888 } ] } } I would like to create a secret for any variables that the user wants so that if somebody gets hold of my docker credentials they cannot view a users secrets by simply looking at the environment variables of their session. The user would give these to us through a POST request using a password form input. Their secret would be in a variable contained in something like this requests.POST['some_private_name'] in the back end of the application. Is this possible? -
Editing fields in Django admin when "Save as New"
I'm trying to blank some fields when a user in the Django admin clicks "Save as New", but it doesn't seem to be working. Ar first I was just calling obj.save(), but it complained their was no request object, so I passed in the request object. Now the new item saves as expected, but if I don't click "Save as New", and just click "Save", I get an IntegrityError: "duplicate key value violates unique constraint". Leading me to believe it's trying to save the existing object with the same pk? Here is the relevant code that lives in my admin.py: def save_model(self, request, obj, form, change): if '_saveasnew' in request.POST: original_pk = resolve(request.path).args[0] original_obj = obj._meta.concrete_model.objects.get(id=original_pk) for prop, value in vars(original_obj).items(): print(prop) if prop == 'submitter_email_address': setattr(obj, prop, None) if prop == 'submitted_by_id': setattr(obj, prop, None) if isinstance(getattr(original_obj, prop), FieldFile): setattr(obj, prop, getattr(original_obj, prop)) obj.save(request) I got the relevant code from this question and edited it to what I need: Django "Save as new" and keep Image fields -
django-s3-storage: Use protected and public files in one project (AWS_S3_BUCKET_AUTH)
I`m using django-s3-storage. I want to use files with and without authentication in one project. So I created two storage classes and set AWS_S3_BUCKET_AUTH to True or false after initial setup from django_s3_storage.storage import S3Storage class S3StoragePublic(S3Storage): def _setup(self): super()._setup() self.settings.AWS_S3_BUCKET_AUTH = False class S3StorageProtected(S3Storage): def _setup(self): super()._setup() self.settings.AWS_S3_BUCKET_AUTH = True Is there a better way of doing this? Or is this how it should be done? -
Django: Make user email required
For my application the email field of a User should be required. That's not the case for the default User model. So I thought it would make sense to create a custom user model as described in the docs: If you’re starting a new project, it’s highly recommended to set up a custom user model, even if the default User model is sufficient for you. This model behaves identically to the default user model, but you’ll be able to customize it in the future if the need arises: from django.contrib.auth.models import AbstractUser class User(AbstractUser): email = models.EmailField(_('email address'), blank=False) However, I am not sure if that's the correct way to achieve this. The reason is that here the Django docs say: Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class. It is an error to have fields in the abstract base class with … -
Django: Automatically selecting related UseRProfile when retrieving User model
When showing {{ user }} in a Django template, the default behavior is to show the username, i.e. user.username. I'm changing this to show the user's initials instead, which are stored in a separate (OneToOneField) UserProfile model. So in customsignup/models.py I've overridden the __unicode__ function successfully, with the desired result: # __unicode__-function overridden. def show_userprofile_initials(self): return self.userprofile.initials User.__unicode__ = show_userprofile_initials But of course, the database is hit again because it needs to independently select the UserProfile model every time a user object is asked to show itself as a string. So even though this works, it escalates the number of database hits quite a bit. What I'm trying to do is modify the manager of the User model after it has been defined by the Django library. So I'm in no control over the User model definition itself. I'm attempting to override an existing model manager. So I've tried overriding the objects member of the User model in the same way that I overrode the __unicode__ function, like so: # A model manager for automatically selecting the related userprofile-table # when selecting from user-table. class UserManager(models.Manager): def get_queryset(self): # Testing indicates that code here will NOT run. return super(UserManager, self).get_queryset().select_related('userprofile') … -
Add Properties To Class At Runtime
Is it possible to add a property to an instance of a class at runtime? This is for Django and it'd model objects but it'd be a great tool to have if this would work on any python class outside of Django. e.g. class person(models.Model) firstname=models.CharField(max_length=100) lastname=models.CharField(max_length=100) def main(): p = person() p.fisrtsname="Firsty" p.lastname="Lasty" p.addnewproperty(newtemporaryproperty) p.newtemporaryproperty="This is a new temporary property" when i use person.newtemporaryproperty=property("") i get a "Can't set attribute error" when trying to add a value to it. I may be using it wrong. -
DRF check if an object already exist in DB when receiving a request
I have products defined by their Name and Location. Each product has a unique pair of Name/Location. I'm writing a view to be able to create a product and I would like to first check if it exists in DB. If yes then keep the ID somewhere to return it to my front app. If no then create it and get the ID also. From my research, overriding the perform_create method should be the solution but I can't figure out how to. Any help would be appreciated. urls.py from django.conf.urls import url from main.views import product_view urlpatterns = [ url(r'^products/$', product_view.ProductCreate.as_view()), url(r'^products/(?P<pk>[0-9]+)/$', product_view.ProductDetail.as_view()), ] product_view.py from rest_framework import generics from rest_framework import permissions from main.models import Product from main.serializers import ProductSerializer class ProductCreate(generics.CreateAPIView): """ Create a new product. """ permission_classes = (permissions.IsAuthenticated,) serializer_class = ProductSerializer queryset = Product.objects.all() def perform_create(self, serializer): product_name = serializer.validated_data['product_name'] product_location = serializer.validated_data['product_location'] if product_name != '': product_list = Product.objects.filter( product_name=product_name, product_location=product_location) if not product_list: product = <my function to create the product> else: product = product_list[0] # I have no idea how to handle the following # serializer.save(...) # else: # Raise an error class ProductDetail(generics.RetrieveUpdateAPIView): """ Retrieve, update or delete a product. """ permission_classes … -
How to do price filter in django internetshop?
I downloaded free template for internetshop and there is a price slider: <div class="price-range"><!--price-range--> <h2>Price Range</h2> <div class="well text-center"> <input type="text" class="span2" value="" data-slider-min="0" data-slider-max="600" data-slider-step="5" data-slider-value="[250,450]" id="sl2" ><br /> <b class="pull-left">$ 0</b> <b class="pull-right">$ 600</b> </div> </div><!--/price-range--> How can i re write it with django to filter items with price ? -
Django: Change a model's attribute field with a form
I have rendered a list of objects in my page. Each one has an edit button next to it that you can click on, that brings up a modal window, to change the name of the model field. The model: class SensorParameter(models.Model): sensor = models.ForeignKey(Sensor) parameter = models.ForeignKey(Parameter) parameter_name_userdef = models.CharField(max_length=100, blank=False, default="Parameter") live_value = models.FloatField() Live value comes from somewhere else. Sensor and Parameter are keys to build a unique table of parameters that belong to sensors. The field I need the user to be able to edit is parameter_name_userdef where the end user can change a machine generated string to whatever they want (from "MhzV13.65Hp" to "FOO", for example). I have tried with a form, but I can't quite get it to work. I think the form is not grabbing the particular instance of the object, but instead it's instantiating a new object from the model. The form: class CustomParameterForm(ModelForm): class Meta: model = SensorParameter fields = ['parameter_name_userdef'] I only provide the value they can change. The objects are built into a table, that looks like this in the template: <tbody> {% for parameter in sensor_parameters %} <tr class="text-left"> <td class="text-center"><span>{{parameter.parameter.parameter_name}}</span></td> <td><i data-parameter-id="{{parameter.id}}" data-toggle="modal" data-target="#editCustomParam" class="far fa-edit" title="Edit … -
Tests failed on ForeignKey constraints
I'm creating a custom "upload from csv" function in my Django application, using a custom model.Manager method that receives the result of applying csv.DictReader on the csv data. My tests on normal models are okay, but they are failing on models with ForeignKey constraints. The relevant source codes are as follows models.py class PledgeManager(models.Manager): """Custom manager for model Pledge.""" def from_upload(self, data): """Process data from Pledge upload.""" for obj in data: try: # get the prospect & fund by natural key obj['prospect'] = Prospect.objects.get_by_natural_key( obj['prospect']) obj['pledge_fund'] = Fund.objects.get_by_natural_key( obj['pledge_fund']) obj['pledge_date'] = datetime.strptime( obj['pledge_date'], r'%d/%m/%Y') try: with transaction.atomic(): self.create(**obj) ccall_log.debug('Created Pledge object: %s', obj) except IntegrityError: ccall_log.error('Cannot create Pledge object: %s', obj) except Prospect.DoesNotExist: # no prospect ccall_log.error( 'Cannot create Pledge object, no Prospect: %s', obj) except Fund.DoesNotExist: # no fund ccall_log.error( 'Cannot create Pledge object, no Fund: %s', obj) except BaseException as exc_: ccall_log.exception(exc_) ccall_log.error( 'Exception encountered during processing: %s', obj) class Pledge(models.Model): """Model for a Pledge.""" objects = PledgeManager() pledge_amount = models.DecimalField( verbose_name='Pledge amount', decimal_places=2, max_digits=12, validators=[MinValueValidator(0)]) pledge_fund = models.ForeignKey(Fund, on_delete=models.CASCADE) pledge_date = models.DateField(verbose_name='Pledge date') prospect = models.ForeignKey(Prospect, on_delete=models.CASCADE) test_models.py class TestPledge(TestCase): """Test cases for Pledge.""" @classmethod def setUpTestData(cls): prospect_obj = { 'natural_key': 'Test Prospect', ... } Prospect.objects.create(**prospect_obj) Fund.objects.create(natural_key='Test … -
Django |safe template filter
Is there a way to specify characters that shouldn't be escaped is Django templates. I have the following Copyright® I don't want Django to escape the &reg ; is there a filter to do that? The workaround I've used to prevent XSS attacks is: {{object.title|safe|striptags}} Is there a better solution than this? -
Django forms getlist and valid_form
I am trying to retrieve the list of options in a select multiple (not the values selected but all the items in the choices. I have others have used: request.POST.getlist('assigned_subnets[]') However I am using a updateview and believe I need to get the data in the form_valid function. I have tried a few options to try and get the list (one in the sample below) but none have worked thus far. form valid: def form_valid(self, form): form.instance.site_data = self.site assigned_subnets = form.cleaned_data.getlist('assigned_subnets') print(assigned_subnets) return super(EditDevice, self).form_valid(form) Full form if needed: class EditDevice(UpdateView): model = DeviceData form_class = DeviceForm template_name = "config/device_form.html" @method_decorator(user_passes_test(lambda u: u.has_perm('config.edit_device'))) def dispatch(self, *args, **kwargs): self.site_id = self.kwargs['site_id'] self.site = get_object_or_404(SiteData, pk=self.site_id) return super(EditDevice, self).dispatch(*args, **kwargs) def get_success_url(self, **kwargs): return reverse_lazy("config:device_details", args=(self.site_id,)) def form_valid(self, form): form.instance.site_data = self.site assigned_subnets = form.cleaned_data.getlist('assigned_subnets') print(assigned_subnets) return super(EditDevice, self).form_valid(form) def get_form_kwargs(self, *args, **kwargs): kwargs = super().get_form_kwargs() kwargs['site_id'] = self.site_id kwargs['device_id'] = self.object.pk return kwargs def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['SiteID']=self.site_id context['SiteName']=self.site.location context['FormType']='Edit' context['active_devices']='class="active"' context['used_subnets']='class="active"' return context -
change-over system - Rails to Django: how should I treat with `encrypted_password` column in django?
I've decided to move my rails app to django app. What I'd like to know is how to treat with encrypted_password column in django app. In rails, password column is automatically saved as encrypted_password, and there is no password column. I've created User model and in database users table exists. by runnning syncdb method, I could create models.py from existing mysql database. I've set AUTH_USER_MODEL = 'myapp.Users' but mysql_exceptions.OperationalError: (1054, "Unknown column 'users.password' in 'field list'") occurs. -
Django - Generate a pdf with html block text
I'm working on a project with Django (1.12) and python(2.7). I have a function which returns me in a string the content of an html page. I need to create the html page with this string, and after to transform it to pdf (already have the function), but WITHOUT leaving my actual page. I search since this morning on the internet but i don't find something like my case. Does anybody knows if it's possible ? and if yes, how? Thanks, @Nikotine -
Django Template Nested for with If Statement
I'm trying to create a selection box in a HTML table where the given name should be selected in the combo box. In order to do this, I need to create a nested loop. The outer loop creates the rows, whereas the inner loop fills the grid cell with options for "Resources". For this example I'm only showing the code creating the Resource cell. The nested loop creates the items, and contains an if statement comparing the action.owner value with the resource.name value. If the items match, the option value should be selected. The server runs with the code, and I cannot find any hidden problems. However I'm not getting the expected results. No item is being selected. Any idea what I'm doing wrong? {% for action in actions %} <tr> <td> <SELECT class="custom-select" value = "{{action.owner}}"> {% for resource in resources %} <option value = "{{resource.name}}" {% if action.owner == resource.name %}selected="selected"{% endif %}>{{resource.name}}</option> {% endfor %} </SELECT> </td> </tr> {% endfor %} -
Returning information from database through django to UI
Can anyone tell me why information is not being displayed in my html? views.py @login_required(login_url="http://127.0.0.1:8000/accounts/login/") def userprofile(request): data = UserProfile.objects.all() return TemplateResponse(request, 'personalInfo/details.html', {"data": data}) urls.py urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^details/$', views.userprofile, name='patient'), ] models.py class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) fullName = models.CharField(max_length = 250, default='', blank=True) address = models.CharField(max_length = 250, default='', blank=True) gender = models.CharField(max_length = 250, default='', blank=True) dob = models.DateField(blank=True) homePhone = models.CharField(max_length = 250, default='', blank=True) personalPhone = models.CharField(max_length = 250, default='', blank=True) emergencyContact = models.CharField(max_length = 250, default='', blank=True) email = models.CharField(max_length = 250, default='', blank=True) ppsn = models.CharField(max_length = 250, default='', blank=True) maritalStatus = models.CharField(max_length = 250, default='', blank=True) employment = models.BooleanField(blank=True) personalPic = models.CharField(max_length = 250, default='', blank=True) def __str__(self): return self.fullName + ' - ' + self.ppsn I am using {{ userprofile.fullname }} to return the name of the person. I am also using profiles with a one to one link to each user. What I want to happen is when a user is logged in that his own information is displayed of course like a profile. Any help would be appreciated. Thanks. -
How to subtract two django dates in template
I want to get the number of days between today and a model field end_date. I defined today in my view as shown below. I will then use the number of days to check if its less than or equal to 50 which will add a class to the element MY TEMPLATE {% for instance in queryset %} <tr> <td>{{forloop.counter}}</td> <td>{{instance.empnumber}}</td> <td>{{instance.name}}</td> <td>{{instance.be}}</td> <td>{{instance.status}}</td> {% if (today - instance.end_date) <= 50 %} <td class="highlight">{{instance.start_date}}</td> <td class="highlight">{{instance.end_date}} : {{ instance.end_date|timeuntil:today }}</td> {% else %} <td>{{instance.status_start_date}}</td> <td>{{instance.status_end_date}}</td> {% endif %} </tr> {% endfor %} MY VIEW if request.user.is_authenticated(): queryset = Employee.objects.all().order_by('-last_updated') today = date.today() context = { "queryset": queryset, "today": today, }