Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Loading an interactive Plotly graph at a click of a button in Django
I am working on a stock screener project using Django, Pandas and Plotly. One of the pages is the user's watchlist where certain stats get displayed for each stock (one HTML table row per stock). This HTML table gets generated in the "watchlist" view by converting a Pandas DataFrame into html using the "to_html" method (with escape=False). Each row of the HTML table has a "View graph" button which opens an interactive Plotly graph for the relevant stock. The graph is generated based on the results of the calculations made in the "watchlist" view, and is represented by an html string that gets sent to the "watchlist" template as part of the context. At the moment I am loading all the stock graphs below the HTML table each time the "watchlist" page gets loaded, but initially the graphs remain hidden with css. Each graph has an id in the dataset that allows me to "unhide" it with JavaScript when someone clicks the matching button with the same id in the table. However, loading all the graphs at once when the "watchlist" page is being loaded takes a lot of time, and I am trying to think of for a solution … -
Cannot set user.is_active to false
I have a custom user model and I am trying to deactive the user but it is not saving. There are also no error messages. My model: class User(AbstractUser): # ... just some additional attributes u = User.objects.get(email="some@email.de") u.is_active = False u.save() u.refresh_from_db() u.is_active # Still returns True but should be False -
django's urlize changing text color of entire string containing url substring
I am loading a long text from a database into a Django template. This string contains a url https://yyyyyy as part of the text. I use django's urlize in order to display the url as actual links, however adding urlize is causing the entire text including the link to change color to blue. Removing the urlize leaves the text as black. I only want the text to be black. How can I stop this color from changing to blue while using urlize? Here is my implementation : <p style="font-size: 15px; color: black" class="tm-pt-30"> {{ value.description | safe | urlize }} </p> The class tm-pt-30 is only responsible for padding. -
setUpTestData and setUp TestCase methods in Django
Can anyone explain me the differences between setUpTestData and setUp TestCase methods in Django? Documentation says. *The class-level atomic block described above allows the creation of initial data at the class level, once for the whole TestCase. This technique allows for faster tests as compared to using setUp(). Be careful not to modify any objects created in setUpTestData() in your test methods. Modifications to in-memory objects from setup work done at the class level will persist between test methods. If you do need to modify them, you could reload them in the setUp() method with refresh_from_db() * And a piece of code... from django.test import TestCase class MyTests(TestCase): @classmethod def setUpTestData(cls): # Set up data for the whole TestCase cls.foo = Foo.objects.create(bar="Test") def test1(self): # Some test -
Acces AJAX data as form data from a dictionary in Django
I am beginner in Django. I am trying to save a form data through AJAX. my code is $("#form").submit(function (e) { e.preventDefault(); var serializedData = $(this).serialize(); value = "user1"; $.ajax({ type: 'POST', url: "{% url 'create-form' %}", data: { "serializedData":serializedData, "value":value, 'csrfmiddlewaretoken': '{{ csrf_token }}', }, success: function (response) { console.log("response", response); /*window.location.replace("https://www.youtube.com/"); */ }, error: function (response) { console.log("error:", response.responseJSON.error); } }) }) And i am trying to accept data in views like this... I used class based view. I added only needed part of views.py code. def post(self, *args, **kwargs): if self.request.is_ajax and self.request.method == "POST" : form = self.form_class(self.request.POST) val = self.request.POST.get("value") if val == "user1" : if form.is_valid(): print("form is valid ") return JsonResponse({"Sucess": True}, status=200) else: print("form is not valid ") return JsonResponse({"error": form.errors} , status=400) .. My problem is when i used to save form = self.form_class(self.request.POST).. It access all values that i sended through AJAX. I want only save serialized data in form. I want to save it as form data because i want to work form with is_valid. -
TypeError: a coroutine was expected, got None
class Test(): def send(self,dela,i): time.sleep(dela) print("process",i) return async def proc(self,dela): task=list() for i in range(5): task.append(asyncio.create_task(self.send(3,i))) res = await asyncio.gather(*task) print(res) return def call(self): asyncio.run(self.proc(3)) return "123" obj=Test() obj.call() I want to run the send method in a for loop concurrently and dont want to wait for each iteration to be completed ,but in the end gather the tasks. -
How to open the file on internet and check the size
I want to open the url such as https://example.com/test.jpg and check the size of image. My idea is using subprocess with curl? However,I suspect.. it looks not good Is there any practice for this purpose? -
Weird 5 second halts for Django on Kubernetes
We have a K8 cluster to host some of our services. These services talk with each other to exchange data over HTTPS APIs. Sometimes we have seen that the execution of services halts for 5 seconds and then continues. We experience high latency during this issue and found that API calls are stalled for 5 seconds through APM traces. I have attached screenshot of one such sample. These services are running Django with gunicorn, containerised in Ubuntu images. What could be the reason of such issues? Is this related to name resolution or lookup? This even K8 related or something else totally? How can we proceed to debug this? If any additional information is required please let me know. Thank you. -
Break response when client goes from stream (StreamHttpResponse) (Django, Google Cloud Run)
I am using StreamHttpResponse to pass binary data to the client. First, I need to set the start and end times of the stream. Locally, when the client leaves the stream (I close the browser page), the close() method is triggered. From django code (from HttpResponseBase class): # These methods partially implement the file-like object interface. # See https://docs.python.org/library/io.html#io.IOBase # The WSGI server must call this method upon completion of the request. # See http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-on.html def close(self): for closer in self._resource_closers: try: closer() except Exception: pass # Free resources that were still referenced. self._resource_closers.clear() self.closed = True signals.request_finished.send(sender=self._handler_class) On Google Cloud Run, this method is called only after 300 seconds (regardless of whether the client is listening to the stream or not). Is it possible to call the close() method when the client leaves the stream on the server? I expected that the response would be interrupted on the server as soon as the client leaves the stream (web page). It worked locally. -
ModuleNotFoundError: No module named 'django' while running the application with Gunicorn
I am getting error while running the application with gunicorn. Everything is working properly with manage.py but wsgi seems not to be working properly. Gunicorn command: gunicorn --bind 0.0.0.0:8000 basicprojectmanagement.wsgi wsgi.py import os from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'basicprojectmanagement.settings') application = get_wsgi_application() Versions: Django==4.0.2 gunicorn=20.1.0 Error (venv) root@vmi819232:/home/neotricinsights/neotricinsights# gunicorn --bind 0.0.0.0:8000 basicprojectmanagement.wsgi [2022-03-30 04:36:38 -0500] [223346] [INFO] Starting gunicorn 20.0.4 [2022-03-30 04:36:38 -0500] [223346] [INFO] Listening at: http://0.0.0.0:8000 (223346) [2022-03-30 04:36:38 -0500] [223346] [INFO] Using worker: sync [2022-03-30 04:36:38 -0500] [223348] [INFO] Booting worker with pid: 223348 [2022-03-30 04:36:38 -0500] [223348] [ERROR] Exception in worker process Traceback (most recent call last): File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker worker.init_process() File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 119, in init_process self.load_wsgi() File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 144, in load_wsgi self.wsgi = self.app.wsgi() File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 49, in load return self.load_wsgiapp() File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp return util.import_app(self.app_uri) File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 383, in import_app mod = importlib.import_module(module) File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line … -
How can I serialize custom many-to-many model django?
I have 3 tables on data base: News, CustomUser(not important) and Comment: class News(models.Model): date = models.DateTimeField(auto_now_add=True) title = models.CharField(max_length=200) text = models.TextField() image = models.ImageField(upload_to='news_images/', blank=True) author = models.ForeignKey(CustomUser, on_delete=models.SET_DEFAULT, blank=True, default=1) tags = models.ManyToManyField(Tags, blank=True) published = models.BooleanField(default=False) comments = models.ManyToManyField( CustomUser, related_name='author', through='Comments.Comment', blank=True, ) class Comment(models.Model): date = models.DateTimeField(auto_now_add=True) text = models.TextField() author = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True) event = models.ForeignKey(News, on_delete=models.CASCADE, parent_link=True) As you can see, comment is a custom many-to-many model, that links Users and News, and adds some extra data (text of comment itself). I using DRF, so I need to serialize it. I need news serializer to include list of comments with all their fields(like author fields as you can see below on the screenshot).But when i getting one of the news I see that: {"id":2,"author":{"username":"Admin","email":"alexsiid29@gmail.com","first_name":"","last_name":""},"tags":[],"comments":[1],"date":"15.08.2021 10:10","title":"Тест2","text":"тестовая 2","image":"http://127.0.0.1:8000/news_images/%D0%91%D0%B5%D0%B7%D1%8B%D0%BC%D1%8F%D0%BD%D0%BD%D1%8B%D0%B9.png","published":true} Comments: "comments":[1] And when I printing during the view-function execution: self.object = News.objects.filter(pk=pk).first() print(self.object.comments.all()) I getting this: <QuerySet [<CustomUser: Admin>]> So, instead of normal representation of comments I get the authors ID. There is my serializers: class CommentSerializer(serializers.ModelSerializer): author = CustomUserSerializer(source='author.id') event = NewsSerializer(source='event.id') class NewsSerializer(serializers.ModelSerializer): author = CustomUserSerializer(read_only=True) tags = TagSerializer(required=False, allow_null=True, many=True) comments = serializers.PrimaryKeyRelatedField(allow_null=True, many=True, read_only=True) So, how can I serialize this … -
Django Failed to establish a new connection <urllib3.connection.HTTPSConnection>
I use Django 2.2 and I have an api that is connected to two Esx servers. a = 10.131.171.80 b = 10.131.171.90 everything works perfectly, except that during my test when I add a third fake server, it tells me that the server does not respond because connection time out. So the error is handled and I don't have a yellow page from Django. Except when enter fourth server this time private ip like 172.16.15.15 I have an error Max retries exceeded NewConnectionError(<urllib3.connection.HTTPSConnection'> But when I use a public IP like 1.1.1.1, I don't get any error. What I would like is to handle the error better when someone enters a private IP. My full traceback Here is my api.py file that connects to Esx servers. import requests import os import json from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) os.environ['REQUESTS_CA_BUNDLE'] = os.path.join('/etc/ssl/certs/') req = requests.Session() req.verify = False class VmwareApi: def __init__(self): self.ip = "" self.user = "" self.password = "" self.arg1 = "" self.ses = "" def session(self): try: a = req.post('https://' + self.ip + '/api/session', auth=(self.user, self.password), timeout=1, verify=False) self.ses = str(a.json()) except requests.exceptions.Timeout: return 'ConnectTimeoutError' return req def param_loader(self): if self.params: self.params = json.loads(self.params) def vapirequestget(self): try: VmwareApi.param_loader(self) myreq … -
how to use new table as a foreign key in existing table in django
Hi Everyone i have two model 1 in Dailytrip and 2nd one is Team, i need to use Team model as foreign key in Dailytrip model, while run Makemigrations getting error. i am using django-2.2 and python 3.7 Models.py class DailyTrip(BaseModel): date = models.DateField(default=None) Team=models.ForeignKey( Team, models.CASCADE, verbose_name='Team', null=True, default=None ) class Team(BaseModel): name = models.CharField(max_length=30) def __str__(self): return self.name Error class DailyTrip(BaseModel): File "E:\11-march-2022\everest_jarvis\fleet\models.py", line 595, in DailyTrip Team, NameError: name 'Team' is not defined -
Best strategy for saving profile picture using Django
This may be a very trivial question but its something I could not find a convincing answer. I am building a web application with Django as the backend. My application has a user profile which can take a profile picture. I have to decide on cloud storage solution. I wanted to ask what is best strategy for such a task. Storing on cloud, or storing on the web server itself. I want to know the pros and cons (ignoring cost). Also if I decide to go with cloud storage, what are the best for such a task. Here its profile picture (limiting single picture to say less than 1MB), so taking into account latency etc. I have worked on AWS S3 and have a solution in place to store these images on S3. But I wanted to have opinions. -
Beautifulsoup returning empty list. Not detecting div or class
I trying to scrape some data. But the beautifulsoup is not detecting the div or class. it is returning an empty list. def get_jobs(url): headers = { 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0' } response = requests.get(url, headers=headers) print("RESPONSE IS: ", response.reason) if response.reason == "OK": soup = BeautifulSoup(response.text, 'html.parser') cards = soup.find_all('a',{'class':'job-cardstyle__JobCardComponent-sc-1mbmxes-0 eCvApI'}) print(cards) I am getting results if no class I was given. i need the data of the specific class. -
Django selenium (StaticLiverServerTestCase): tests run together fail but pass if run individually
I write tests with StaticLiverServerTestCase for my Django application. My issue: tests runs together fail while pass when run individually. I have read much about this behavior on SO but none of answer/comments help. It seems that issue may come from shared files/fields/data. I have tried using setUpClass/tearDownClass class methods as well as setUp/tearDown methods. Using fixture did not resolve anymore. I also tryed to make individual class for each test instead of one class with many test ethods but doesn't work anymore. My tests involve Ajax calls and I think this is related. For example, in the 2 tests below: L_new_patient_is_created_from_index_page_TestCase run first (I don't know why) and pass. L_search_form_exist_refresh_TestCase fail L_search_form_exist_refresh_TestCase will call an ajax view to display a table and change the url with JQuery ($(location).prop('href', redirect_url);). So if the user refresh the page (F5), it will be redirected to another page that hit the changed url and display the same table. What happen when the 2 tests are run together is that url is not changed so refresh do not redirect to the right page. How can I make my tests trully independant? class L_search_form_exist_refresh_TestCase(StaticLiveServerTestCase): fixtures = ['dumpdata.json'] port = 8000 # host = "0.0.0.0" # … -
Get empty OrderedDict through Serializer when retrieving
I have built two models: class Homepage(models.Model): org_name = models.CharField(max_length=20, blank=True) org_code = models.CharField(max_length=20, blank=True) class Meta: ordering = ('org_name',) def __str__(self): return self.org_name class MainDiag(models.Model): release = models.TextField(max_length=256, blank=True) code = models.CharField(max_length=20, blank=True) condition = models.CharField(max_length=20, blank=True) homepage = models.ForeignKey(Homepage, related_name='main_diag', on_delete=models.CASCADE) Serializers: class MainDiagSerializer(serializers.ModelSerializer): class Meta: model = MainDiag fields = ( 'release', 'code', 'condition', ) class HomepageSerializer(serializers.ModelSerializer): main_diag = MainDiagSerializer(many=False) class Meta: model = Homepage fields = ( "org_name", "org_code", "main_diag", ) def create(self, validated_data): main_diag_data = validated_data.pop('main_diag') homepage = Homepage.objects.create(**validated_data) MainDiag.objects.create(homepage=homepage, **main_diag_data) return homepage I can add MainDiags into MySQL successfully, however, when I get Homepages through serializer = HomepageSerializer, the main_page in serializer.data is always an empty OrderedDict(). Is there any issue with my code? How can I get the main_page through serializer.data? Thank you very much! -
How do I access User Model field in the Serializer of extended User Model in Django Rest Framework?
I am creating a simple registration/login app in Django Rest Framework using Rest APIs with Django Rest Knox's authentication, in which, upon registration, user will also upload a CSV File (which is an optional field). I have a Person Model which is an extended (derived or OneToOne) Model of User Model and has FileField for the file to be uploaded. I have two ModelSerializers, one is based on User Model named MainRegisterSerializer and serializes username, email and password fields, and the other is based on Person Model named RegisterSerializer, which serializes FileField and register field (that is initialized with MainRegisterSerializer class's constructor. Now, in views.py, I am accessing the RegisterSerializer, to get the data from front that which includes username, email and password fields from User Model, and FileField from Person Model. But, when I enter data in front-end and upload the file, and click on POST button, I get the following error: KeyError at /api/v1/register 'username' on the code line user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'], validated_data['file']) in serializers.py file. My views.py is: from rest_framework.response import Response from knox.models import AuthToken from .serializers import RegisterSerializer class RegisterAPI(generics.GenericAPIView): serializer_class = RegisterSerializer def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user … -
uploading multiple files in django form and function in views
I am trying to upload multiple files in Django form. I followed multiple methods. I am successful via models to views but I want via models to forms to views. I followed Use view/form in Django to upload a multiple files this question but my form is not submitting. Here I am sharing my codes. I hope to get the mistake I am doing by any expert. #models.py class Question(models.Model): description = models.TextField(blank=True, null=True) created_by = models.ForeignKey(User, blank=False, on_delete=models.CASCADE) def __str__(self): return self.description class QuestionFile(models.Model): question = models.ForeignKey( Question, on_delete=models.CASCADE, related_name='files', null=True, blank=True) file = models.FileField( 'files', upload_to=path_and_rename, max_length=500, null=True, blank=True) def __str__(self): return str(self.question) #forms.py from django import forms from .models import * class QuestionForm(forms.ModelForm): class Meta: model = Question fields = ['description'] class QuestionFileForm(QuestionForm): # extending Questionform file = forms.FileField( widget=forms.ClearableFileInput(attrs={'multiple': True})) class Meta(QuestionForm.Meta): fields = QuestionForm.Meta.fields + ['file', ] def clean(self): cleaned_data = super().clean() description = cleaned_data.get("description") file = cleaned_data.get("file") if not description and not file: self.add_error( 'description', 'Kindly describe the question details or upload any file') #views.py def questionView(request): if request.method == 'POST': form = QuestionFileForm(request.POST or None, request.FILES or None) files = request.FILES.getlist('file') if form.is_valid(): question = form.save(commit=False) question.created_by = request.user # add everything needed … -
How to style an Image Field button in Django App
I want to style my Image Field Button (Choose File) with CSS. Currently, its displaying in the default theme. I am using Bootstrap V5.0.1, Django V3.2.12, Python 3.7.6. First, I tried to identify or add a class or id which I can add and style the button regularly but was unable to add as the button was added by Django Image Field. The Code in my forms.py is given below: from django import forms class ImageUploadForm(forms.Form): image = forms.ImageField(label='') Then I used the Hover functionality of the Chrome Developer Tools to identify any leads and found that the button and its area had 2 id's #file-upload-button and #id_image. I tried to add CSS to the above-mentioned id's but did not get the result i desired. I want to style the Choose File Button Below also if possible can i add any bootstrap to the button, any help would be appreciated! Thanks. HTML-Django Code <div class="form-group text-center"> <form method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form }} <br> <button type="submit" id="btnUpload" class="btn btn-primary" style="margin-top:20px;">Upload</button> </form> </div> -
ImportError: cannot import name 'profile' from 'user.models'
from django.contrib import admin from django.urls import path,include from user import views as user_view from django.contrib.auth import views as auth_views from django.conf import settings from django.conf.urls.static import staticyour text -
Check data exist in any foreign key table of pk table in MySQL database using django python
I am trying to check if there has any data in all referenced table of a primary key table. Is it possible in mysql using django, not any raw query, need to do it in django, python. Find 02 queries in mysql, but need more details in django. SELECT \* FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE SELECT DISTINCT KEY_COLUMN_USAGE.CONSTRAINT_NAME, KEY_COLUMN_USAGE.TABLE_NAME, KEY_COLUMN_USAGE.COLUMN_NAME, KEY_COLUMN_USAGE.REFERENCED_TABLE_NAME, KEY_COLUMN_USAGE.REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ON TABLE_CONSTRAINTS.CONSTRAINT_NAME=KEY_COLUMN_USAGE.CONSTRAINT_NAME WHERE TABLE_CONSTRAINTS.CONSTRAINT_TYPE="FOREIGN KEY" AND TABLE_CONSTRAINTS.CONSTRAINT*SCHEMA=\<DATABASE*NAME\>; Thanks in advance! I tried to get all referenced foreign key table of a primary key table. Then check if any data exists in those foreign key tables. But I am not getting any source in django. Is ist possible? -
Django left join or analog
I can’t figure out how to use Django ORM for left joins, please see case below. So, my models are (please, don’t mind native language): class Production(models.Model): '''Выработка.''' project = models.ForeignKey( Project, on_delete=models.PROTECT, verbose_name='проект') timeperiod = models.ForeignKey( Timeperiod, on_delete=models.PROTECT, verbose_name='период') time = models.DurationField('трудочасы') ammount = models.DecimalField('выработка', max_digits=10, decimal_places=2) note = models.TextField('заметка', blank=True, null=True) is_auto = models.BooleanField( 'признак автоматического расчета', default=False) class Timeperiod(models.Model): '''Периоды.''' start_date = models.DateField('дата начала') end_date = models.DateField('дата окончания') target_duration = models.DurationField('длительность') is_open = models.BooleanField('открыт', default=True) I would like to make following selection: Get all the Timeperiod with filters on start_date and end_date Get related Production with filter on project (if any exists for this particular period) Pass it to template_processor and render it as a table (in for loop) I know how to filter queryset, and know how to get related queryset, but I don’t understand how to filter my RELATED queryset -
Django authentication with .NET httpWebRequest and HttpWebResponse
I have an issue with the login with the "POST", following this link here: Django Authentication from .NET using HttpWebRequest and HttpWebResponse via HTTP POST Without "POST", I don't seem to get logged on because the received ResponseStream is still on the login page. Adding the request method "POST" caused me to be caught in "The remote server returned an error: (403) Forbidden." Also tried .Credentials rather than "Dim postData As String = "username=" & m_username & "&password=" & m_password" but nothing helps. Trying AllowAutoRedirect True/False, PreAuthenticate True/False doesn't help at all. myRequest.AllowAutoRedirect = True myRequest.Method = "POST" myRequest.PreAuthenticate = True myRequest.Credentials = New NetworkCredential(username, password) I am sure I can access the site with the password I am using. Any advice? -
Monitoring of loggings using Django
I have a pipeline of steps that I want to monitor and also alert the user in the front end about the process. Now, the logging messages can come from many parts of the code, is it a good approach to create a Django task that monitors the logging file (until the pipeline finishes) and via a channel sends the information to the front-end? Is there a better approach to that problem ?