Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Celery task to process data from database Postgres and django
I'm trying to use celery and celery beat to run a scheduled task to process data from the database, but when I try to run the task I get this error "django.db.utils.OperationalError: FATAL: role "tanaka" does not exist". The code for the scheduled task is shown below settings.py CELERY_BEAT_SCHEDULE = { 'task-number-one': { 'task': 'loans.tasks.update_loan_book', 'schedule': 60, }, } tasks.py @shared_task def update_loan_book(): tenants = Tenant.objects.all() for tenant in tenants: #logic to update tenant object The code works when I run the task using the "celery -A proj worker -l info -B" command but does not work when I daemonize celery and celery beat. Config files for celery and celery beat are shown below. I am using supervisord. [program:projworker] command=/home/tanaka/microfinance/bin/celery -A cloud_based_microfinance worker -l info directory=/home/tanaka/Repositories/microfinance_project user=tanaka numprocs=1 stdout_logfile=/var/log/celery/proj_worker.log stderr_logfile=/var/log/celery/proj_worker.log autostart=true autorestart=true startsecs=10 stopwaitsecs = 600 killasgroup=truepriority=998 [program:projbeat] command=/home/tanaka/microfinance/bin/celery -A cloud_based_microfinance beat -l info directory=/home/tanaka/Repositories/microfinance_project user=tanaka numprocs=1 stdout_logfile=/var/log/celery/proj_beat.log stderr_logfile=/var/log/celery/proj_beat.log autostart=true autorestart=true startsecs=10 priority=999 When I try to run the task as a daemon I get "django.db.utils.OperationalError: FATAL: role "tanaka" does not exist" in the proj_worker.log file. -
Race condition in Django while using transaction
Using Django, I got the following (minimal) model : from django.db import models from django.contrib.postgres.fields import JSONField class MyModel(models.Model) files = JSONField(default=list) In a view, I have the following code, in order to append data to the files field: from django.db import transaction def my_view(request): [...] with transaction.atomic(): entry = MyModel.objects.select_for_update().get(id=some_id) entry.files += [some_value_a, some_value_b] entry.save() [...] When performing requests one by one, the code is working fine. However, when performing several requests in parralel, some values are lost. It seems to be a race condition, but since the append is performed within an atomic transaction block, it should not happen. For exemple, if I perform the request 5 times in parralel, I end up with 8 values in the field (instead of 10), while having 5 HTTP_200_OK responses. How to have a trully atomic block ? -
Django, Template not found
I'm trying to render this HTML File (An Home page) And I get an error that template is not found. #views.py from django.shortcuts import render def index(request): """Homepage""" return render(request,'templates/index.html') from django.contrib import admin from django.urls import path, include from . import views urlpatterns = [ path('', views.index, name='Home'), ] error: TemplateDoesNotExist at / templates/index.html and this just loads a blank page: from django.shortcuts import render def index(request): """Homepage""" return render(request, 'index.html') -
After contents of div adjusted via jquery rectangle from div juts into element below
Right now I have logout button which changes the content of a Div element after being clicked. The problem is that when this happens, there is a small rectangle with the same background color as the div which juts into the element below the div element. The code where this transformation takes place is the following. <script> $(document).ready(function(){ $("button#logout").click(function(){ console.log("here"); $.ajax({url: "/map/ajax/logout", success: function(result){ $("#div1").html(result); console.log("here1"); document.getElementById("userbar").innerHTML = "<div id=\"userbar\"><a href=\"/accounts/login\">Login</a></div>"; console.log("done"); }}); }); }); </script> The innerHTML content exists separately in the case where the Django session is AnonymousUser. % else %} <div id="userbar"> <a href="/accounts/login">Login</a> </div> {% endif %} In this case there is no rectangle jutting out from the div element. I do not understand why the case is different when the html is set by jQuery. -
'str' object has no attribute 'get', AttributeError at /add-to-cart/shirt
I am getting above stated error after lining some modification in models and views and here are the associated code: def add_to_cart(request, slug): item = get_object_or_404(Item, slug=slug) order_item, created = OrderedItems.objects.get_or_create( item=item, user=request.user, ordered=False ) order_qs = Order.objects.filter(user=request.user, ordered=False) if order_qs.exists(): order = order_qs[0] # check if the order item is in the order if order.items.filter(item__slug=item.slug).exists(): order_item.quantity += 1 order_item.save() messages.info(request, "This item quantity was updated.") return reverse('core:product_detail', kwargs={'slug': slug}) else: order.items.add(order_item) messages.info(request, "This item was added to your cart.") return reverse('core:product_detail', kwargs={'slug': slug}) else: ordered_date = timezone.now() order = Order.objects.create( user=request.user, ordered_date=ordered_date) order.items.add(order_item) messages.info(request, "This item was added to your cart.") return reverse('core:product_detail', kwargs={'slug': slug}) the trace back seems to be really vague: Traceback: File "C:\Users\pytuts\.virtualenvs\ecommerce_only-Npoh1MB8\lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request) File "C:\Users\pytuts\.virtualenvs\ecommerce_only-Npoh1MB8\lib\site-packages\django\utils\deprecation.py" in __call__ 96. response = self.process_response(request, response) File "C:\Users\pytuts\.virtualenvs\ecommerce_only-Npoh1MB8\lib\site-packages\django\middleware\clickjacking.py" in process_response 26. if response.get('X-Frame-Options') is not None: Exception Type: AttributeError at /add-to-cart/shirt/ Exception Value: 'str' object has no attribute 'get' and urls.py looks like: path('add-to-cart/<slug:slug>/', add_to_cart, name='add_to_cart'), anyone knows how to solve it, kindly help me with this. thank you! -
How to run WebSocket (Django) on Heroku?
I can not start websocket on Heroku. This code runs on a local server. <script> let socket = new WebSocket('{{ ws_server_path }}'); socket.onopen = function(e) { alert("[open] Connection established"); alert("We send data to the server"); socket.send("My name is John"); }; </script> local settings.py CHAT_WS_SERVER_HOST = 'localhost' CHAT_WS_SERVER_PORT = 5002 CHAT_WS_SERVER_PROTOCOL = 'ws' heroku settings.py CHAT_WS_SERVER_HOST = '0.0.0.0' CHAT_WS_SERVER_PORT = os.environ['PORT'] CHAT_WS_SERVER_PROTOCOL = 'ws' On the local server I get messages, but on Heroku I get an empty console and after a while the message: failed: WebSocket opening handshake timed out -
Python Django - Variables shared between multiple users
I am working on a project written in python and using django to build a website as well. I have a function which is pulling the information from a website and putting that information into a dictionary. When the users refresh the browser, the website will show the latest update of that dictionary, so far I am doing the updates being triggered by the browser but this is only for testing. So, after several headaches I could finally install celery and make it work, so I have my website running with "python manage.py runserver", and at the same time I have two celery processes running: "celery -A tasks worker -l info pool=solo" and "celery -A tasks beat --loglevel=info". So far everything seems to be working until I realized that the dictionary is being updated but not for all users who access the website, looks like each user has his own instance of the dictionary. So the idea is to have celery updating the dictionary with the information pulled from the website and all users just seeing what is inside the dictionary variable. Can I do this without a database or a file being written every time the update function is … -
Update whole table for new save()
I just made change to a model with a score attribute to have a new save() def save(self, force_insert=False, force_update=False, using=None, update_fields=None): if self.id: if self.best_rank <= self.rank_level: self.best_rank = self.rank_level return super(model, self).save(force_insert=False, force_update=False, using=using, update_fields=update_fields) now I want to update previous records to have apply this change, to change best_rank from 0 (default). I tried qs = model.objects.filter(best_score=0).exclude(score=0) for e in qs: e.save() the problem is that i have 800000 records in that table. There must be a better way than the one I'm trying -
Zappa with Django Material Admin can't load material icons correctly
I'm using Django Material Admin, which was working fine while I was having my app deployed to Elastic Beanstalk, or running it locally. Then I decided to move it serverless with Zappa I have followed the documentation to delivery static/media content over S3 So, my settings became: INSTALLED_APPS = [ 'material.admin', 'material.admin.default', ... 'django_s3_storage', ] ... # The AWS region to connect to. AWS_REGION = "us-east-2" # The AWS access key to use. AWS_ACCESS_KEY_ID = os.environ['S3_MEDIA_AWS_ACCESS_KEY_ID'] # The AWS secret access key to use. AWS_SECRET_ACCESS_KEY = os.environ['S3_MEDIA_AWS_SECRET_ACCESS_KEY'] DEFAULT_FILE_STORAGE = 'django_s3_storage.storage.S3Storage' YOUR_S3_BUCKET = "my-bucket-name STATICFILES_STORAGE = "django_s3_storage.storage.StaticS3Storage" AWS_S3_BUCKET_NAME_STATIC = YOUR_S3_BUCKET AWS_S3_BUCKET_NAME = YOUR_S3_BUCKET # These next two lines will serve the static files directly # from the s3 bucket AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % YOUR_S3_BUCKET STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN AWS_S3_FILE_OVERWRITE = True AWS_S3_BUCKET_AUTH = False AWS_S3_MAX_AGE_SECONDS = 60 * 60 * 24 * 365 # 1 year. Then I run the collectstatic, and everything seems to work fine However, the material icons are not displayed, I see the text instead, all the rest of the static files are showed correctly. If I inspect the website I can see that the font is loaded inline in the header: @font-face { font-family: 'Material … -
Django Rest Framework very slow when accessing user properties
Everytime I make a drf request to access user properties, such as: def StatusView(request): current_user = request.user if current_user.is_anonymous: return HttpResponse("none", content_type="text/plain") # return HttpResponse(JsonResponse(Serialize_User(current_user)), content_type="text/plain") # tmp = Serialize_User(current_user) return HttpResponse("none", content_type="text/plain") it takes up to 10 seconds to get a response. If I comment out this line if current_user.is_anonymous:, the request is instant. Same goes for tmp = Serialize_User(current_user). Any kind of user access takes too long to process. I looked into optimization methods, but disabling DEBUG or prefetching doesn't help, as my models don't include nested models. Here is my User model: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(_('email address'), unique=True) first_name = models.CharField(_('first name'), max_length=30, blank=True) last_name = models.CharField(_('last name'), max_length=30, blank=True) date_joined = models.DateTimeField(_('date joined'), auto_now_add=True) is_active = models.BooleanField(_('active'), default=True) #avatar = models.ImageField(upload_to='avatars/', null=True, blank=True) objects = UserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] class Meta: verbose_name = _('user') verbose_name_plural = _('users') def get_full_name(self): ''' Returns the first_name plus the last_name, with a space in between. ''' full_name = '%s %s' % (self.first_name, self.last_name) return full_name.strip() def get_short_name(self): ''' Returns the short name for the user. ''' return self.first_name def email_user(self, subject, message, from_email=None, **kwargs): ''' Sends an email to this User. ''' send_mail(subject, message, from_email, … -
{% if any([None]) %}show{% endif %} in template creates TemplateSyntaxError
I'm getting a strange error. Putting {% if any([None, 1]) %}show{% endif %} in my Django template creates a TemplateSyntaxError. Experimentation shows that {% if True %}show{% endif %} works so I know the problem is from any([None, 1]). I checked that any([None, 1]) in python outside of Django and it returned True as expected so my code seems like it should work in the template. What am I doing wrong? Thank you for you time. -
Django filtering many-to-many
I have this models: class A(models.Model) product = models.CharField(max_length=50) class B(models.Model) serial_number = models.CharField(max_length=50) product = models.ForeignKey(A) class C(models.Model) combine_products = models.ManyToManyField(A) serial = models.ManyToMany(B) My question is how do I block a user for selecting already selected serial from class C how to do that? If is already used to not be able to select again. I can't use OneToOne because in class C I'm combining multiple from class A with class B. Sorry for my English. Thanks! -
Sequentially execute Multiple R/W Querries in same django views function
I have read and write querries in my single Django view function. As in below code, def multi_querry_function(request): model_data = MyModel.objects.all() #first read command ...(do something)... new_data = MyModel( id=1234, first_property='random value', second_property='another value' ) new_data.save() #second write command return render(request, index.html) I need these queries in the function to be executed consecutively. For example, if multiple users use this function at the same time, it should execute this function for both users one by one. The 'read' of one user should only be allowed if the previous user has completed both of his/her 'read and write'. The queries of both users should never be intermingled. Should I use table locking feature of my PostgreSQL DB or is there any other well managed way? -
hello I cant link css in html page on django
I can't link css file in html, I tried in other example html css alone with out django it's work but in django I have problem could you help me Thanks a lot. ''' <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>First site</title> <link rel="stylesheet" type="text/css" href="styles/styles.css"> </head> <body> <h1>WeLcome</h1> <hr> {% if latest_post_list %} <ul> {% for post in latest_post_list %} <h3 ><a href="/web//{{ post.id }}">{{ post.title }}</a></h3> <p>{{ post.body }}</p> {% endfor %} </ul> {% else %} <p>no post are availabel</p> {% endif %} </body> </html> ''' ''' @import url('https://fonts.googleapis.com/css?family=Raleway&display=swap'); body{ text-decoration: none; font-family: 'Raleway', ; max-width: 300; margin: auto; padding: 20px; } ''' -
Accessing manually rendered form fields from Jquery
When I render a form normally my code will validate the image file and send it through. If I manually render the form fields the validation falls. Normal Form: {{ form }} Manually rendered fields: <div class="form-row"> {{ form.username }} </div> <div class="form-row"> {{ form.image }} </div> Jquery handling the image: $("#id_image").change(function () { if (this.files && this.files[0]) { console.log(this.files[0]); var reader = new FileReader(); reader.onload = function (e) { $("#image").attr("src", e.target.result); $("#modalCrop").modal("show"); } reader.readAsDataURL(this.files[0]); } }); I have tried to change this.files[0] to this.files[6] since the image is the 7th item in the form but that did not work. Help? -
How do I get image from Profile Model
How can I get the image from Profile Model link with foreignkey User. I tried {% for user in users %}{{ user.profile_pic }}{% endfor %} this do not display the image. class Profile(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True) profile_pic = models.ImageField(upload_to='ProfilePicture/', blank=True) users = User.objects.exclude(id=request.user.id) -
django-q qcluster starts and exits while running in Pycharm Debug
I'm running a Django project with django-q in PyCharm. manage.py runserver is running in one instance, and manage.py qcluster is running in another. qcluster starts up fine, then immediately exits gracefully. Here is the full text: /Users/user/PycharmProjects/project/venv/bin/python /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 65362 --file /Users/user/PycharmProjects/project/manage.py qcluster --settings=project.settings.dev Connected to pydev debugger (build 193.6494.30) pydev debugger: process 21339 is connecting 16:03:44 [Q] INFO Q Cluster grey-kentucky-georgia-avocado starting. 16:03:44 [Q] INFO Process-1 guarding cluster grey-kentucky-georgia-avocado 16:03:44 [Q] INFO Q Cluster grey-kentucky-georgia-avocado running. 16:03:44 [Q] INFO Process-1:1 ready for work at 21343 16:03:44 [Q] INFO Process-1:2 ready for work at 21344 16:03:44 [Q] INFO Process-1:3 ready for work at 21345 16:03:44 [Q] INFO Process-1:4 ready for work at 21346 16:03:44 [Q] INFO Process-1:5 ready for work at 21347 16:03:44 [Q] INFO Process-1:6 monitoring at 21348 16:03:44 [Q] INFO Process-1:7 pushing tasks at 21349 16:03:44 [Q] INFO Q Cluster grey-kentucky-georgia-avocado stopping. 16:03:44 [Q] INFO Process-1 stopping cluster processes 16:03:45 [Q] INFO Process-1:7 stopped pushing tasks 16:03:46 [Q] INFO Process-1:1 stopped doing work 16:03:46 [Q] INFO Process-1:2 stopped doing work 16:03:46 [Q] INFO Process-1:3 stopped doing work 16:03:46 [Q] INFO Process-1:4 stopped doing work 16:03:46 [Q] INFO Process-1:5 stopped doing work 16:03:47 [Q] INFO … -
The empty path didn't match any of these (django)
A similar question was asked here in The empty path didn't match any of these. The solution did indeed work when I included the path('',include('projects.urls')) within personal_portfolio.py. But here's what bugs ME... personal_portfolio/urls.py: urlpatterns = [ path('admin/', admin.site.urls), path('projects/', include('projects.urls')), ] projects/urls.py: urlpatterns = [ path("", views.project_index, name="project_index"), path("<int:pk>", views.project_detail, name="project_detail"), ] The empty path IS contained (in the latter)! when the path('projects/', include('projects.urls')) is called, it should call projects/urls.py. Then the empty path is there. Why do I need to include path("",include('projects.urls')) in personal_portfolio/urls.py for it to work?! -
Implementing a 3 x 3 image grid using Django
I am trying to implement a 3 x 3 grid in Django. Further notes: Within each grid square I should be able to place images sourced directly from my computer. The images should fit each grid square completely. What is the best approach to implement such specifications. I am relatively new to Django so I am mainly concerned about the relevant tools / resources I should be using I have attached a sample grid 'Image Grid' Image Grid -
Postgre not installing properly on my windows
I downloaded postgres 12.2 a couple of days ago, when installing it could not install fully at a point it said database cluster installation failed. I have tried all I could but to no avail. I really need help so I can continue on my peojectenter image description here -
Django rest api root view showing links of another app
I have created a projects which has two apps in it, app1, app2 both having, router = routers.DefaultRouter() in its url.py, both of them as a standalone app ie if other app router is commented, will show correct default root API of all links. But when launching both of them, I am having an issue of apps2 api links coming in app1, by overwriting similar links of app1 which is similar in terms of serilizer, viewset and model name This is my projects urls.py file from django.urls import path, include urlpatterns = [ path('app1/', include('app1.urls'),name='oc_url'), path('app2/', include('app2.urls'),name='pc_url'), ] The issue is coming with api which is related to models which has same name in both apps. Is it because I have same models with same name in both apps? Any suggestions to fix this? -
How to scrape google maps for all data using python
I am trying to scrape the title, phone number, website, address, rating, number of reviews of a place from google maps using python. For example, the restaurant Pike's Landing (see google maps URL below) needs all the information. I want to pull those in python. URL: https://www.google.com/maps?cid=15423079754231040967&hl=en I can see HTML code when I inspect but when I have used beautiful soup for scrapping all codes are converted. From stack overflow, I have found a solution for the only number of review as following code, import re import requests from ast import literal_eval urls = [ 'https://www.google.com/maps?cid=15423079754231040967&hl=en', 'https://www.google.com/maps?cid=16168151796978303235&hl=en'] for url in urls: for g in re.findall(r'\[\\"http.*?\d+ reviews?.*?]', requests.get(url).text): data = literal_eval(g.replace('null', 'None').replace('\\"', '"')) print(bytes(data[0], 'utf-8').decode('unicode_escape')) print(data[1]) But I need all the data. I can use Google Maps API to actual data but getting phone number, rating, review is not free now. So that I want to escape data from the frontend. Please help me. -
Unable to save relationship between two objects with Django Rest Framework
I am building a Django/React App to allow users to submit orders that need to go from A to B. The user initially saves the addresses in the database and then he/she selects it in the order form. When they submit I attempt to create a relationship in the database, I'm using Django Rest Framework serializers to create the Order object in the database. Unfortunately, I'm unable to successfully save the items as I'm not properly linking the addresses to the order. Im getting the following error: destinationAddress: ["Invalid value."] originAddress: ["Invalid value."] Models class Order(models.Model): originAddress = models.ForeignKey(Address, related_name="originAddress", null=True, on_delete=models.CASCADE) destinationAddress = models.ForeignKey(Address, related_name="destinationAddress", null=True, on_delete=models.CASCADE) packages = models.CharField("Packages", max_length=1024) class Address(models.Model): address_code = models.CharField(max_length=250) contact = models.CharField("Contact", max_length=1024) phone = models.CharField("Phone", max_length=20) company = models.CharField("Company", max_length=250) addressLine1 = models.CharField("Address line 1", max_length=1024) addressLine2 = models.CharField("Address line 2", max_length=1024, blank=True) postalCode = models.CharField("Postal Code", max_length=12) city = models.CharField("City", max_length=1024) state = models.CharField("State", max_length=250) Serializers class AddressSerializer(serializers.ModelSerializer): class Meta: model = Address fields = '__all__' class OrderSerializer(serializers.ModelSerializer): originAddress = serializers.SlugRelatedField( many=True, queryset=Address.objects.all(), slug_field='pk' ) destinationAddress = serializers.SlugRelatedField( many=True, queryset=Address.objects.all(), slug_field='pk' ) class Meta: model = Order fields = ('id', 'packages', 'destinationAddress', 'originAddress') ViewSets class OrderViewSet(viewsets.ModelViewSet): queryset = Order.objects.all() permission_classes … -
Jinja2 does not show in the webpage correctly
Using Django I have to create a basic website to display the elements inside the SQL database on the webpage. Right now, I have created a basic HTML template to start from there but since I have included the Jinja2 syntax inside the HTML document, it does nto show correclty. It shows all the Jinja2 code there in the browser. If there is another file you need to look at, just ask. index.html { % extends "header.html" % } { % block content % } <article>Article</article> <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/books.html">Books</a></li> <li><a href="/about.html">About</a></li> </ul> </nav> <aside>Aside</aside> { % endblock % } header.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Welcome</title> <link rel="stylesheet" href="/static/styles.css"> </head> <body> <header>Header</header> <div id="main"> { % block content % } { % endblock % } </div> <footer>Footer</footer> </body> </html> OUTPUT -
How to properly configure static folder for django's admin files
I have deployed project on production (django+gunicorn+nginx) My project structure is as follows forecast forecast settings.py urls.py static admin css fonts img css js In settings.py static folder configured as follows STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR,'static') STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')] While i am trying to login to admin page it's return me 500 server error. Than i tried to see what in my nginx logs /var/log/nginx/error.log.1 There are following 2020/02/20 13:01:53 [error] 11703#11703: *5 open() "/usr/share/nginx/home/isli/projects/forecast/static/static/admin/css/base.css" failed (2: No such file or directory), client: 188.170.195.79, server: isli.site, request: "GET /static/admin/css/base.css HTTP/1.1", host: "isli.site", referrer: "http://isli.site/admin/login/?next=/admin/"