Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Problem in running django command in crontab
I'm using docker container and I want to do periodic crawling & updating Django models. So I decide to make django command and run it periodically by Linux crontab. Cronjob list * * * * * . /root/.profile; /usr/local/bin/python3 /code/manage.py crawl_snu >> /var/log/cron.log 2>&1 # crawl_snu is a command name But error occurs Traceback (most recent call last): File "/code/manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 204, in fetch_command settings.INSTALLED_APPS File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 56, in __getattr__ self._setup(name) File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 43, in _setup self._wrapped = Settings(settings_module) File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 125, in __init__ raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") SECRET_KEY is in environment variable, so I use it like below # settings.py # ... SECRET_KEY = os.environ.get('SECRET_KEY') # ... I tried to pass the environment variable by adding . /root/.profile/ before executing a command, but it doesn't work. There are many environment variables besides SECRET_KEY, and they are passed by host via docker run --env .env imagename. It may be a problem related to Docker crontab. Are there any better solutions for periodically executing django command with docker? -
Django Allauth Social account Google Signup Customize username
I have a django restfulframework with login option via google its working cool. I have to override to customise username DefaultAccountAdapter save_user in my middleware. I want to username be like, abraham instead of 001-abraham this is my code in middleware. def save_user(self, request, sociallogin, form=None): us = sociallogin.user organization = Organization.objects.filter(Q(name__icontains=org_name)).last() u = sociallogin.user u.set_unusable_password() if form: self.custom_save_user(request, u, form, organization.code) else: self.populate_username(request, u, organization.code) sociallogin.save(request) return u def populate_username(self, request, user, code=""): from allauth.account.utils import user_username, user_email, user_field first_name = user_field(user, 'first_name') last_name = user_field(user, 'last_name') email = user_email(user) username = user_username(user) if code: username = str(code) + '-' + username username = user_username(user, username or '') if app_settings.USER_MODEL_USERNAME_FIELD: user_username( user, username or get_account_adapter().generate_unique_username([ first_name, last_name, email, username, Thankyou so much for helping me! -
Django connect to IBM Cloud database
I'm new in Django. I'm trying to connect Django app to IBM Cloud db, I loaded my data IBM cloud and created credionals. Then installed ibm_db_django $ easy_install ibm_db_django Then in setting.py did import import ibm_db_django Then I added credionals in settings.py DATABASES = { 'default': { 'ENGINE' : 'ibm_db_django', 'NAME' : 'BLUDB', 'USER' : 'USER', 'PASSWORD' : 'PASSWORD', 'HOST' : 'dashdb-txn-sbox-yp-lon02-02.services.eu-gb.bluemix.net', 'PORT' : '50000', 'PROTOCOL' : 'TCPIP', 'PCONNECT' : True, }, } Is this enough to connect to ibm cloud db? Now how can I test is this connected to database, and how can I migrate all tables from that database? -
Gunicorn memory usage and threads keep growing
I'm running a Django app with Gunicorn in a Kubernetes environment. Since a few weeks the memory usage of the pods keeps growing. After looking into the process list I noticed that there are many gunicorn processes which seem dead but are still using memory. The command I'm starting gunicorn is: gunicorn app.wsgi -w 3 -b 0.0.0.0:8000 --env DJANGO_SETTINGS_MODULE=app.settings.prod --reload I don't know what is causing the growth of the gunicorn processes and how to avoid this behaviour. Here is a screenshot of the process list: pod memory usage htop -
Multi threading in django
I need to create an api which calculate sum of numbers but I have no idea on how to do this by multi-threading I am able to create the model for storing numbers but how to will my view look if i need to achieve this by multi-threading? -
how to get field value from model
I have a model for registering with some fields. All fields are models I fill in through the form like this. def get_name(request): if request.method == 'POST': user_code = generate_code(8) subject = 'code' message = user_code phone=request.POST['phone'] form = NameForm(request.POST) if form.is_valid(): date_use = form.cleaned_data.get("date_visit") time_use = form.cleaned_data.get("time_visit") purpose_use = form.cleaned_data.get("purpose") if Registration.objects.filter(date_visit=date_use,time_visit=time_use,purpose=purpose_use).count()==0: Registration.objects.create(fio=request.POST['fio'],phone=request.POST['phone'],date_visit=request.POST['date_visit'],time_visit=request.POST['time_visit'], number_car=request.POST['number_car'],purpose=request.POST['purpose'], tso=request.POST['tso']) request.session["phone"] = phone request.session["code"] = user_code return HttpResponseRedirect('endreg') else: form = NameForm() return render(request, 'registers/detail.html', {'form': form}) The model also has a field date_register = models.DateTimeField(verbose_name = 'date register', auto_now_add=True) how can i write date_register value in request.session["date"] ? -
Force TemporaryFileUploadHandler on specific request
I need to process an uploaded file with GeoDjango. According to the documentation, I should use Datasource() constructor from GDAL. Problem is that size of uploaded shapefiles can be less than 2.5 MB, so MemoryFileUploadHandler is used by default and thus I can't access to a filepath required by Datasource(). I decided to override request.upload_handlers for my specific view, with only "django.core.files.uploadhandler.TemporaryFileUploadHandler" because I don't need to create a subclass (for now), but I get the following error : You cannot set the upload handlers after the upload has been processed. Here is my piece of code: def home(request): request.upload_handlers = ["django.core.files.uploadhandler.TemporaryFileUploadHandler"] if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) else: form = UploadFileForm() return render( request, 'app/index.html', { 'title':'HOME', 'form': form, } ) What am I doing wrong ? Also, should I create a subclass for a custom handler anyway ? -
Update database currency rates in djago docker
class Currency(models.Model): base = models.CharField(max_length=10,default='USD') AED = models.DecimalField(max_digits=100,decimal_places=5) GBP = models.DecimalField(max_digits=100,decimal_places=5) Now i am getting currency rates from api. I need to save new rates to my database in every x minutes . I used schedule package it worked but server was blocked. there is any way not use celery. it was so overkill. We are using Docker container. -
download multiple csv files and save in one csv file
I would like to download a csv file playlist.csv, and re-use the file in the views, like; views.py def export_csv(request): result = Advert data = open(os.path.join(settings.PROJECT_PATH, 'csv/playlist.csv'), 'r').read() response = django.http.HttpResponse(data, content_type='application/x-download') response['Content-Disposition'] = 'attachment;filename=playlist.csv' writer = csv.writer(response) adverts = Advert.objects.values_list('file') for advert in adverts: writer.writerow(advert) return response def my_real_export(request): csvFile = 'playlist.csv' xmlFile = 'xml/mmyXMLfile.xml' csvData = csv.reader(open(csvFile)) xmlData = open(xmlFile, 'w') xmlData.write('<m_config path="C:\\Users\\user\\Desktop\\playlist.xml">' + "\n") # there must be only one top-level tag rowNum = 10 for row in csvData: if rowNum == 10: tags = row # replace spaces w/ underscores in tag names for i in range(0, 1): xmlData.write(' ' + '<file in="0.0", path ="C:\\Users\\user\\Desktop\\new\\media\\' + row[ i] + '">' + '</file>' + "\n") rowNum = rowNum + 1 xmlData.write('</m_config>' + "\n") xmlData.close() return render(request, 'sch/home.html') My problem is that, each time i click on the download button, it downloads as playlist, the next download playlist(1), playlist(2) till it reaches playlist(100) and even more Now, my question is that I'm looking for a way to overwrite in such a way that when I click on the download button, it replaces the content of the already existing playlist.csv Any ideas will be helpful please, thanks in advance! -
How can I change the selection of a MultiSelectField of my django model using python code?
I have a django model with a MultiSelectField like this: from django.db import models from multiselectfield import MultiSelectField CHOICES = (('0', "Example choice"), ('1', "Another choice")) class MyModel(models.Model): name = models.CharField(max_length=50, unique=True) my_field = MultiSelectField(choices=CHOICES, blank=True) def save(self, *args, **kwargs): self.my_method() super().save(*args, **kwargs) def my_method(self): # do something, imagine that the result is that "Another choice" has to be selected now # select "Another choice" here - but how? As indicated by the comments and the question title I want to change what choices are selected in the MultiSelectField my_field using a method in the model. When I access self.my_field I get a list of the selected choices and I can also append choices to this list (self.my_field.append('1')). But this will not get saved in the database: the next time my_method is called "Another choice" is not selected anymore. (Removing a choice with self.my_field.remove('0') also doesn't get saved.) The name attribute of my model can be changed in my_method in the most trivial way (self.name = "A different name") and this will get saved in the database so it seems to be a problem specific to MultiSelectField. I also tried to inherit from MultiSelectField and add a method which I … -
How get a generated PIL image from Django REST framework backend to frontend?
I was planning to make a web app where I can make a POST request to generate an image with PIL on the backend and link it to the frontend through the REST framework by GET where I can download it. I don't want to save the generated image in the database. Just wanna generate it one time only for download. Any idea on how I can pull it off? -
How to optimize count of queries in Django admin for custom fields from another model?
I have the problem with SQL queries optimization for custom list_display field "balance". For getting account balance query executed for each row. What is the way to avoid it and to make count queryes fixed. I trying to user prefetch_related but this is doesnt have any effects. class UserAdmin(admin.ModelAdmin): form = UserAdminForm fields = [ 'username', 'password', 'last_name', 'first_name', 'middle_name', 'groups' ] list_display = [ 'username_', 'person_name', 'balance', def balance(self, obj): balance = obj.person.customer.customeraccount_set.first().account.balance return balance balance.allow_tags = True balance.short_description = 'Balance' def get_queryset(self, request): qs = super().get_queryset(request).prefetch_related( 'person', 'person__customer', ).filter(groups__name='driver') ordering = self.get_ordering(request) if ordering: qs = qs.order_by(*ordering) return qs Screen from debug tool bar: -
printing lists in django table
I have lists of list like this [['402.32 ', ' #1 ', ' Fri 10 Jan 2020 ', ' 0 ', 'DELIVERY ', ' Requested by HEMANT'], ['102.32 ', ' #2 ', ' Fri 14 Feb 2020 ', ' 8 ', ' ADDED ', '4.53.22']] I want to loop through So that I can print the list in Django table format 402.32 #1 Fri 10 Jan 2020 0 DELIVERY Requested by HEMANT 102.32 #2 Fri 14 Jan 2020 8 ADDED 4.53.22 Please help how to proceed further I have tried like this but only one row is coming i=0 j=0 while i <= 2: while j <=6: print(list1[i][j]) j=j+1 i=i+1 but my doubt is how to print in table format with tr and td html tags with this while loop -
delete button issue 'str' object is not a mapping
I have an Imageloader app that each time upload an image it will show on the html page so i added download button to the images and i want to add delete button also ,I am trying to add delete button to image each time I press the button I got error 'str' object is not a mapping, even I don't understand what this error mean I tried a lot of things but it didn't work below is my code, any help appreciated. urls.py -main urlpatterns = [ path('admin/', admin.site.urls), path('', home, name='home'), path('image/', include('imageloader.urls'), 'imageloader'), ] urls.py -app app_name = 'imageloader' urlpatterns = [ path('<int:pk>/', views.delete_pic, name='delete_pic'), ] models.py class Imageloader(models.Model): def validate_file_size(value): filesize = value.size if filesize > 3000000: raise ValidationError( "The maximum file size that can be uploaded is 3MB") else: return value image = models.ImageField( help_text='max size only 3mb ', upload_to="imageloader_file/", validators=[validate_file_size], max_length=120, blank=False, unique=True, ) def __str__(self): return str(self.image) def delete(self, *args, **kwargs): self.image.delete() return super().delete(*args, **kwargs) view.py def home(request): """this function to view the home page """ template_home = 'index.html' img = Imageloader.objects.all() if request.method == "POST": form = Imageloaderform(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get("image") form.save() messages.success(request, f'Image uploaded successfully') return redirect('home') else: … -
How to group all the related data and return as a values dict in django?
I have 3 models: class City(models.Model): name = models.CharField(max_length=150) class Person(models.Model): name = models.CharField(max_length=150) city = models.ForeignKey( City, related_name="persons", on_delete=models.PROTECT) class Fruit(models.Model): quantity = models.IntegerField() price = models.IntegerField() person = models.ForeignKey(Person, related_name="fruits") city = models.ForeignKey(City, related_name="fruits") I want to group all the data based on city and get total price of fruits city wise and total quantity citywise as well as person wise. I want my result something like: {'city': 'XYZ', 'total_fruits': 20, 'total_price': 123, 'persons': [{'name': 'foo', 'total_fruits': 5}, {'name': 'bar', 'total_orders': 6}]} I have tried something like: queryset = City.objects.annotate(total_price = Sum('persons__fruits__price'), total_fruits= Count('persons__fruits')) city_list = [] for city in queryset: abc = {'name': city.name, 'total_fruits':company.total_fruits, 'total_price':company.total_price} persons = city.persons.annotate(total_fruits= Count('fruits')).values('name','total_fruits') abc.update({'persons':persons }) city_list.append(abc) Which is not much efficient. I want to remove this loop and minimize the queries. Thanks in advance. Let me know where I am wrong. -
can anybody help me. I am stuck at this step where Django is not able to show requested page
I am doing this Django project and I am stuck at this. I have created a new url path which will return the value which will be given after movies/__ in url. here is my code: note: movie is one the app in my application movies/urls.py from django.urls import path from . import views urlpatterns = [ path("", views.index, name="movie_index"), path("< movie_id >", views.detail, name="movie_detail") ] movies/views.py from django.http import HttpResponse from django.shortcuts import render from .models import movie def index(request): movies = movie.objects.all() return render(request, "movies/index.html", {"movies": movies}) def detail(request, movie_id): return HttpResponse(movie_id) views.py in root folder urlpatterns = [ path('admin/', admin.site.urls), path('movies/', include('movies.urls')) ] my error : enter image description here I am using Django 2.1 and python 3.8 thanks in advance....... -
Is there any way to remove edges and node when using DjangoFilterConnectionField?
i started to use graphene with django and for right now i don't need all that overhead of edges and node, i know it's for pagination but right now i only need the fields of my model. To be clear, i still want to be able to use filterset i just dont know how to remove the edges and node overhead. i've tried to use graphene.List but i couldn't add filterset to it. so instead of doing this {users(nameIcontains:"a") { edges{ node{ name } } } i would like to do this {users(nameIcontains:"a") { name } -
How do i get the values from input text fields from my custom form through the request: Django 3.0?
So, I've been trying hard, getting values from input fields in my custom form. I have a url that corresponds to the form and that form redirects it to the same form again. The view of the form url checks whether the request method is Post. If it is, then I declare a vairiable equal to request.POST, I then assigned those values to my model- item_description(). Here is the code of the views.py: def addItem(request): if request.method == "POST": data = request.POST print(data.__dict__) item_description(item_name=data.item_name, item_number=data.item_number, item_quantity=data.item_quantity) else: HttpResponse("Something went wrong!") return render(request, 'ims/addItemForm.html') HTML form: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>IMS| Add Item</title> </head> <body> <form action="{% url 'Item-addition' %}" method="post"> {% csrf_token %} <input type="text" name="item_name" placeholder="Enter Item Name"> <input type="text" name="item_number" placeholder="Enter Item Number"> <input type="text" name="item_quantity" placeholder="Enter Item Quantity"> <button type="submit" name="add" value="add">Add Item</button> </form> <a href="{% url 'Items-list' %}">See items</a> </body> </html> urls.py: from django.urls import path from . import views urlpatterns = [ path('add_item', views.addItem, name='Item-addition'), path('items', views.itemsList, name='Items-list'), ] models.py: from django.db import models class item_description(models.Model): item_name = models.CharField(max_length=200) item_number = models.CharField(max_length=200) item_quantity = models.CharField(max_length=200) def __str__(self): return self.item_name Also, i printed out the request which is a dictionary, but it was … -
Disable components of get_initial in django
I am initializing some of the fields in my django forms and they are working fine but they are still editable, How can I disable them?? Views.py def get_initial(self): initial = super().get_initial() initial['sales_order'] = MaterialRequest.objects.get(pk=self.kwargs['pk']) #disable this initial['parent_company'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('owner', flat=True)[0] initial['product1'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product1', flat=True)[0] initial['product2'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product2', flat=True)[0] initial['product3'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product3', flat=True)[0] initial['product4'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product4', flat=True)[0] initial['product5'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product5', flat=True)[0] initial['product6'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product6', flat=True)[0] initial['product7'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product7', flat=True)[0] initial['product8'] = MaterialRequest.objects.filter(pk=self.kwargs['pk']).values_list('product8', flat=True)[0] Also, Is there a cleaner method to re-write this ? -
Django: How to import CSV from Django admin?
hi im currently try to import a csv file into a django project with using the django admin. the code ive written seems to work when in python run but im unsure about how to build the html template as i cant seem to find any examples. Is anyone able to either post an example or point me in the right direction -
The response received in AJAX call is shown in another HTML page
I have ajax request call which sends an ID to the server, then the server sends a JSON response. I want to update the innerHTML of the pre tag using the value in that JSON Response. for some reason, the below code is not printing the console log as well. <script> $("#AssociateForm").submit(function(e) { e.preventDefault(); var form = $(this); var url = form.attr('action'); var local_id = $('input[name=J_ID]').attr('id'); var formData = { 'J_ID' : $('input[name=J_ID]').val() }; console.log(formData) $.ajax({ url: url, data: formData, dataType: 'json', success: function (datas) { var data = JSON.parse(datas); if(datas.status){ alert(datas); //$('#Failure_'+local_id).innerHTML = data.category + ' issue: '+data.j_id +' ('+data.j_status+')' } }, error: function(jqXHR, textStatus){ alert("In error") } }) .done(function(data){ alert(data) }); }); </script> But, When the response comes, the success section is not triggered. Instead, the complete JSON string is printed on a different page. {"category": "known", "j_id": "AU298", "j_status": "Confirmed"} below is from View-Page-source <html> <head></head> <body data-gr-c-s-loaded="true"> <pre style="word-wrap: break-word; white-space: pre-wrap;"> {"category": "known", "j_id": "AU298", "j_status": "Confirmed"} </pre> </body> </html> -
Django i18n_patterns language prefix
I am using i18n_patterns to change the language prefix. It s working fine ones the language cookie was set. The problem is that it's adding /en/ when I trying to access a page without the language code in private window, even though my preferred language is not en (the default one set in settings.LANGUAGE_CODE). -
How to upload files to a Django Model that are created on the fly?
Here's an example: Suppose a user, let's say 'Avi' is using my web app. Avi uploads a file (say and .xls/.xlsx) to my web app. I use django-storages to upload the files to either S3 or Azure. The problem arises when I create a dataframe from the uploaded file. When I export the dataframe, it does not get uploaded but rather gets exported to the location of the project. Current Solution: I use azure, boto3 api for converting the dataframe to a stream of bytes and then uploading to the corresponding container or s3 bucket. Is there a easier way where I could pass the newly created dataframe through a serializer so it gets uploaded to the respective location? -
Schedule ignors kwargs in django-q
I am using Django-q (https://django-q.readthedocs.io) for queuing in the Django framework. I have an async function when I use async_task: async_task('sms.tasks.send', username=username, password=password, text=text, to=to, path=path, ) everything is ok and works fine. but when I use it with schedule: schedule('sms.tasks.send', username=username, password=password, text=text, to=to, path=path, next_run=scheduled_time) Although nothing has changed and even in Scheduled task all Kwargs exist, in execution, no Kwargs passes. Does anybody has faced this problem with Django-q? -
Celery execute tasks in the order they get called (during runtime)
I have a task consisting of subtasks in a chain. How can I ensure a second call of this task does not start before the first one has finished? @shared_task def task(user): res = chain(subtask_1.s(), # each subtask takes ~1 hour subtask_2.s(), subtask_3.s()) return res.apply_async() A django view might now trigger to call this task: # user A visits page that triggers task task.delay(userA) # 10 seconds later, while task() is still executing, user B visits page task.delay(userB) This leads to the tasks racing each other instead of being executed in sequential order. E.g. once a worker has finished with subtask_1() of the first task, it begins working on subtask_1() of the second task, instead of subtask_2() and subtask_3() of the first one. Is there a way to elegently avoid this? I guess the problem is the order the subtasks get added to the queue. I have already set worker --concurreny=1, however that still doesn't change the order he consumes from the queue. Official docs (task cookbook) seem to offer a solution which I don't understand and doesn't work for me unfortunately. Perhaps include a blocking mechanism within the task, after the chain, with a while not res.ready(): sleep(1) kind …