Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How can i upload as 1cr rows .csv file using django?
I am getting a memory error while uploading a CSV file of size around 650 mb with a shape (10882101, 6). How can i upload such file in postgres using django framework. -
I had created a login page, but whenever i enter credentials it shows me NoReverseMatch at /login
my html login page is: <html> <head> <title>Login Page</title> </head> <body> <form method="post" action="/login"> {% csrf_token %} <table width="20%" bgcolor="0099CC" align="center"> <tr> <td colspan=2><center><font size=4><b>User Login Page</b></font></center></td> </tr> <tr> <td>Username:</td> <!-- name=Username to username --> <td><input type="text" size=25 name="username"></td> </tr> <tr> <td>Password:</td> <!-- name=Password to password--> <td><input type="Password" size=25 name="password"></td> </tr> <tr> <td><input type="submit" onclick="return check(this.form)" value="Login"></td> </tr> </table> </form> <div> {% for messages in messages %} <h3> {{messages}} </h3> {% endfor %} </div> </body> </html> my views file is: from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth.models import User, auth def login(request): if request.method == 'POST': username = request.POST.get('username', None) password = request.POST.get('password', None) user = auth.authenticate(username=username,password=password) if user is not None: auth.login(request, user) return redirect('/') else: messages.info(request,'Invalid Credentials') return redirect('login ') else: return render(request,'login.html') my URLs for views are : from Django.urls import path from. import views urlpatterns = [ path('',views.homepage, name='homepage'), path('login',views.login, name='login'), path('registration',views.registration, name='registration'), ] Error shown : NoReverseMatch at /login Reverse for 'login ' not found. 'login ' is not a valid view function or pattern name. Request Method: POST Request URL: http://127.0.0.1:8000/login Django Version: 3.0.4 Exception Type: NoReverseMatch Exception Value: Reverse for 'login ' not found. 'login ' is not a … -
How to access a fields inside values from filtering query sets in django?
my code is like this: #user model class User(models.Model): phonenum = PhoneNumberField() #bio model class Bio(models.Model): user_num = models.OneToOneField(User,on_delete=models.CASCADE,related_name='user_num_bio') #view.py result = Bio.objects.filter(user_num__phonenum__national_number__endswith=7896) I want to access the national_number that is a value of PhoneNumberField, but the last line gives error: django.core.exceptions.FieldError: Unsupported lookup '_national_number_endswith' for PhoneNumberField or join on the field not permitted. INFO: PhoneNumberField values: PhoneNumber(country_code=98, national_number=1234567896, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=20, preferred_domestic_carrier_code=None) package_add: PhoneNumberField -
FormSet with manytomany field
This is my situation: I have a performer which should be able to add to their profile individual records of their performances. The compositions are already in the database, and they should be able to choose which compositions they have performed in a specific date. This is my model: class Profile(models.Model): first_name = models.CharField(max_length=30, blank=True, null=True) surname = models.CharField(max_length=30, blank=True, null=True) class Composition(models.Model): title = models.CharField(max_length=120) composer = models.ForeignKey(Profile, on_delete=models.CASCADE) class Performance(models.Model): performed = models.ManyToManyField(Composition, blank=True) when = models.DateField(auto_now=False, auto_now_add=False) The performer will probably perform more than one composition each time, so that's why I thought of using formset in my view: from django import forms from .models import Composition, Performance from django.forms import formset_factory @never_cache def performance_create_view(request): whenForm = WhenForm(request.POST or None) PerformanceFormSet = modelformset_factory(Performance, fields=('performed',), extra=10 ) formsetPerformance = PerformanceFormSet(request.POST or None, prefix='performed', queryset=Performance.objects.all()) context = {'form':formsetPerformance, 'whenForm': whenForm} return render(request, 'performances/performance-create.html', context) forms.py: class PerformanceForm(forms.ModelForm): class Meta: model = Performance fields = [ 'performed', 'when', ] Template: {% extends 'base.html' %} {% block content %} <form action='.' enctype="multipart/form-data" method='POST'>{% csrf_token %} <div class="grid-container"> <div class="grid-x grid-padding-x"> {{ form.management_form }} {{ form }} {{ whenForm }} [...] The field when will only have to be there once of … -
Best way to run TCP server alongside Django to gather IoT data
I have a database with a frontend running on Elasticbeanstalk in AWS. Within my Django application I'd like to gather IoT data coming in via TCP/IP. Currently, I start the server and switch it to listening through a View function. This leads to the problem that the server might shut down, or stop. Furthermore, the server needs to listen on the port steadily although data is not coming in continiously. What is a more elegant way to solve this problem? Is there any Django extension tackling exactly that problem? -
How to unit test a celery task with autoretry, backoff and jitter
Using celery 4.3.0. I tried to write a unit test for the following task. from django.core.exceptions import ObjectDoesNotExist @shared_task(autoretry_for=(ObjectDoesNotExist,), max_retries=5, retry_backoff=10) def process_something(data): product = Product() product.process(data) Unit test: @mock.patch('proj.tasks.Product') @mock.patch('proj.tasks.process_something.retry') def test_process_something_retry_failed_task(self, process_something_retry, mock_product): mock_object = mock.MagicMock() mock_product.return_value = mock_object mock_object.process.side_effect = error = ObjectDoesNotExist() with pytest.raises(ObjectDoesNotExist): process_something(self.data) process_something_retry.assert_called_with(exc=error) This is the error I get after running the test: @wraps(task.run) def run(*args, **kwargs): try: return task._orig_run(*args, **kwargs) except autoretry_for as exc: if retry_backoff: retry_kwargs['countdown'] = \ get_exponential_backoff_interval( factor=retry_backoff, retries=task.request.retries, maximum=retry_backoff_max, full_jitter=retry_jitter) > raise task.retry(exc=exc, **retry_kwargs) E TypeError: exceptions must derive from BaseException I understand it is because of the exception. I replaced ObjectDoesNotExist everywhere with Exception instead. After running the test, I get this error: def assert_called_with(self, /, *args, **kwargs): """assert that the last call was made with the specified arguments. Raises an AssertionError if the args and keyword args passed in are different to the last call to the mock.""" if self.call_args is None: expected = self._format_mock_call_signature(args, kwargs) actual = 'not called.' error_message = ('expected call not found.\nExpected: %s\nActual: %s' % (expected, actual)) raise AssertionError(error_message) def _error_message(): msg = self._format_mock_failure_message(args, kwargs) return msg expected = self._call_matcher((args, kwargs)) actual = self._call_matcher(self.call_args) if expected != actual: cause = expected if … -
Avoid .all() when using prefetch_related
I have following query in my view: devices = Device.not_deleted_objects.filter( delivery_status=Device.RECEIVED ).prefetch_related( "site__users", Prefetch( "gatewaydevices", queryset=GatewayDevice.objects.filter( end_date=None, gateway__isnull=False ).prefetch_related("gateway"), ), In my serializer I need to get the gateway that is linked to the device using the gatewaydevices table. Because I filter on end_date and gateway_isnull there will always be only one gatewaydevice in the query per device. So in my serializer I do this: @staticmethod def get_gateway(device): all_gatewaydevices = device.gatewaydevices.all() for gd in all_gatewaydevices: if gd.gateway is not None: return GatewaySimpleSerializer(gd.gateway).data if gd else None return None How can I avoid doing device.gatewaydevices.all() ? -
Django db design with multiple tables
I would like to create a booking management app for hairdressers. The app should also allow normal users (clients) to book their own appointments. Let assume that each hairdresser will have 10 booking per day on average. This means that at 1000 hairdressers, I'd have 10.000 bookings per day or 3.6 million a year. Should I create a table for each shop (keeping it small and fast to query) or should I use a single table (with bookings as rows)? I image that in the first case, I should also create a booking table for each user (redundant data) in order to avoid having to check 1000 table to know if the user has an appointment. And at 10.000 shops should the design be the same or change? Whats's the correct approach? Thanks. -
How to serve static files with Waitress and Django?
I've a tiny web app built on Django that has some static files collected by python manage.py collectstatic. I'm using a lightweight server, Waitress When I run my server using the script from waitress import serve from <my app>.wsgi import application if __name__ == '__main__': serve(application, host = '0.0.0.0', port='8000') the app loads up to http:localhost:8000 but I notice the static files are not present. From the terminal, I can read Not Found: /static/<my app>/styles.min.css WARNING:django.request:Not Found: /static/<my app>/styles.min.css Not Found: /static/<my app>/buttonhover.css WARNING:django.request:Not Found: /static/<my app>/buttonhover.css Not Found: /static/<my app>/script.min.js Do I need something in addition to Waitress to serve the static files? Do I need a reverse proxy like nginx running alongside Waitress? If so, are there Python reverse proxies available? -
400 (Bad Request) on fetching image from aws s3
I have created a API using Django Rest Framework and storing the profile images in S3 using django storages and boto3. The Images are being uploaded properly but when I get the user object and try to display the image in client side (Angular and Android), I am getting 400 Bad Request from aws. -
Django throwing Module Not Found (BASE_DIR returning correctly) and not finding settings config
I tried changing the root directory for the project (to match a productions servers structure so relative importing matches) and managed to screw up pycharm to the point that I can't even run scripts on new django projects. I imagine this has to do with how the venv is configured in pycharm, but have tinkered for hours changing relative import names and can not figure it out. I vaguely remember having to change the environment variables in the pycharm configs, but can't find anything like that when googling. EDIT: When running through console I receive error: ModuleNotFoundError: No module named 'mysite.main' I'm guessing the project root is defined incorrectly somewhere outside of the settings file (BASE_DIR in settings is returning the correct project root of "PARENT DIRECTORIES/mysite" (settings.py located in mysite/mysite/settings.py, test.py located mysite/main/test.py) Thank you in advance for any and all help. The error I receive is: C:\Users\steve\Documents\www\Scripts\python.exe C:/Users/steve/Documents/www/mysite/main/test.py Traceback (most recent call last): File "C:/Users/steve/Documents/www/mysite/main/test.py", line 1, in <module> from mysite.main.models import ModelTest File "C:\Users\steve\Documents\www\mysite\main\models.py", line 5, in <module> class ModelTest(models.Model): File "C:\Users\steve\Documents\www\lib\site-packages\django\db\models\base.py", line 107, in __new__ app_config = apps.get_containing_app_config(module) File "C:\Users\steve\Documents\www\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config self.check_apps_ready() File "C:\Users\steve\Documents\www\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready settings.INSTALLED_APPS File "C:\Users\steve\Documents\www\lib\site-packages\django\conf\__init__.py", line 76, … -
How to run perioduc tasks through django-celery on xampp windows?
I am trying to run periodic tasks for my django web application. I have my website running on xampp. During development, I used redis server and commands on commandline to start worker and beat. How do I integrate this with xampp server? -
when i vote one of the choices it shows error (can only concatenate str (not "int") to str) please help i am a newbie in programming
i am following django official documentation tutorial t make a poll app i am currently stuck on part 4.So when i select one of the choices it shows an error (can only concatenate str (not "int") to str). I cant understand am i trying to add a string to integer where is the problem in the code please herlp here is my code: views.py from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from .models import Choice, Question def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context) def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/detail.html', {'question': question}) def results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': question}) def vote(request, question_id): question = get_object_or_404(Question, pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the question voting form. return render(request, 'polls/detail.html', { 'question': question, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) urls.py from django.urls import path from . import views app_name = 'polls' … -
I want to query to get the details from the command line
In The Models.py file (I have this codebase) class Person(models.Model): sex_choices = ( ('Male', 'Male'), ('Female', 'Female') ) martial_choices = ( ('Single', 'Single'), ('Married', 'Married'), ('Divorce', 'Divorce'), ('Widowed', 'Widowed') ) name = models.CharField(max_length=200) sex = models.CharField(choices=sex_choices, max_length=50) martial_status = models.CharField(choices=martial_choices, max_length=50) age = models.IntegerField() def __str__(self): return self.name class DetailsOfEducationQualification(models.Model): type_choice = ( ("Government", "Government"), ("Private", "Private"), ("Anganwadi Center", "Anganwadi Center"), ) education_proximity_choice = ( ("0-5", '0-5km'), ('5-10', '5-10km'), ('10+', 'Above 10km'), ('Outside the state', 'Outside the state'), ) person = models.ForeignKey(Person, on_delete=models.CASCADE) course_class = models.CharField(max_length=50, blank=True) type_of_education_sector = models.CharField(choices=type_choice, max_length=50, blank=True) education_facility_proximity = models.CharField(choices=education_proximity_choice, max_length=50, blank=True) In The Admin.py file (I have this) from .models import ( Person, DetailsOfEducationQualification ) class DetailsOfEducationQualificationInline(admin.TabularInline): model = DetailsOfEducationQualification extra = 0 class PersonAdmin(admin.ModelAdmin): fieldsets = [ ( 'Personal Information', { 'fields':[ 'name', 'sex', 'age', 'martial_status' ] } ), ] inlines = [ DetailsOfEducationQualificationInline ] In query shell, I want to get the person 'course_class' since the DetailsOfEducationQualification model is related to Person: like in he query: person = Person.objects.get(id=1) person.course_class this code gets an error, saying person does not have the attribute... How do I access the DetailsOfEducationQualification date from the Person model? -
Django, ho to set queryset to zero value if empty
I have the following code in my views.py, that give me the iva_debito_totaleThat I figure out in my templates as table. But if the Ricavi models are empty, also my iva_debito_totale is empty. I have the necessity to have anyhow iva_debito_totalealmost equal to zero, even if Ricavi is empty. This is my code defaults = list(0 for m in range(13)) iva_debito = dict() for year, month, totale in(Ricavi.objects.values_list( 'data_contabile__year', 'data_contabile__month'). annotate(totale=ExpressionWrapper(Sum(F('quantita') * F('ricavo')*(F('iva')+0)), output_field=FloatField())).values_list('data_contabile__year', 'data_contabile__month', 'totale')) : iva_debito[id]=list(defaults) index=month iva_debito[id][index]=totale iva_debito_totale={'IVA a Debito Totale': [sum(t) for t in zip(*iva_debito.values())],} -
Sort a QuerySet by ManyToManyField in Django
I have a model like this: class my_model(models.Model) foo = models.ManyToManyField(My_other_model) I want to get all Objects of my_model sorted by the number of objects in the foo field. In this Question A friendly Guy from russia says, that you should use django-annontation. I tried the following: my_model.objects.all().order_by("-foo") and it works fine. Is there any reason I should go the longer way with annotations?? Thx for help and stay healthy! -
Django serializer one-to-many relationship to array
Is it possible to get the nested relationship as an array from the django rest framework serializer? The response is now: [ { "location": "xxx", "text": "xxx", "starttime": "2018-04-26T21:46:25.311000", "action": [ { "eventaction": "Silenced" }, { "eventaction": "Notification Expired" }, { "eventaction": "Announced" } ] } ] But I want it like this: [ { "location": "xxx", "text": "xxx", "starttime": "2018-04-26T21:46:25.311000", "action": ["Silenced", "Notification Expired", "Announced"] } ] How can I achieve this? -
JSONField in Django continues to remain empty even after POST request
I have a model with a JSONField in Django. If I issue POST through the browser using Django Rest UI, the data gets entered into the model with no issues. However,when I use Python's requests.post in my application, everything except the JSONField data stores in the model. Here is my model from django.db import models from django.contrib.postgres.fields import JSONField class Scans(models.Model): Name = models.CharField(max_length=20) Server = models.CharField(max_length=20) Results = JSONField(default=dict) Report_Url = models.URLField(max_length=30)` Here is my Serializer from rest_framework import serializers from .models import Scans class ScansSerializer(serializers.ModelSerializer): class Meta: model = Servers fields = '__all__' Here is my view class ScansData(APIView): def get(self, request): scans = Scans.objects.all() serializer = ScansSerializer(scans, many=True) return Response(serializer.data, status=status.HTTP_200_OK) def post(self, request): serializer = ScansSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request): scans = Scans.objects.all() scans.delete() return Response(status=status.HTTP_204_NO_CONTENT) Here is my request data = { "Name": "dodo", "Server": "ubuntu", "Report_Url": "https://127.0.0.1:8000/about/" } jsonval = { "Results": { "Severity_High": 8, "Severity_Medium": 7, "Severity_Low": 5 } } requests.post('http://127.0.0.1:8000/scans/', data=data, json=jsonval) URL urlpatterns = [ path('scans/', ScansData.as_view()), ] What I see after using requests.post { "id": 10, "Name": "dodo", "Server": "ubuntu", "Results": {}, "Report_Url": "https://127.0.0.1:8000/about/" } -
How to add hidden parameter to ModelForm?
How to add hidden parameter to ModelForm? forms.py class ModelForm(forms.ModelForm): class Meta: model = Model fields = ['field'] -
Add points from json to mapbox in django not working
I would like to push data from a json file into mapbox in Django. For this i copied the json in views.py and pushed the data to default.html as context. views.py def default_map(request): mapbox_access_token = 'I put my token here'; context = { 'vessels': Vessels } return render(request, 'default.html', { 'mapbox_access_token': mapbox_access_token }, context ) #Copy pasting JSON files Vessels = [ { "vessel": "A La Marine", "date": "20/Apr 13:18", "lon": 7.88499997456869, "lat": 53.9, "speed": 13.8999996185303, "course": "0", "type": "NoonReport" }, { "vessel": "Barcelona Express", "date": "22/Apr 06:04", "lon": 11.8683216666667, "lat": 41.5915366666667, "speed": 18, "course": "139.7", "type": "CM" }, In default.html i tried different things: 1)Putting marker in for loop & 2)Putting all markers after the for loop default.html <body> <h1>{{vessels.lon}}</h1> <div id='map' class='map' width="100%" style='height:800px'></div> <script> mapboxgl.accessToken = '{{mapbox_access_token}}' <!-- {{mapbox_access_token}} --> var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v10' }); var geojson = { type: 'FeatureCollection', features: [ {% for vessel in vessels %} { type: 'Feature', geometry: { type: 'Point', coordinates: [{{vessel.lon}}, {{vessel.lat}}] }, properties: { title: '{{ vessel.vessel }}', description: '{{ vessel.type }}' } var marker = new mapboxgl.Marker() .setLngLat(coordinates) .addTo(map); }, {% endfor %} ] }; //Test Marker var marker = new mapboxgl.Marker() .setLngLat([52,3]) … -
How to add a variable within a variable in a Django template
I have a Django template with a variable as follows: {{ results_coin_data.market_data.current_price.usd }} I'm looking to change the 'usd' portion at the end by doing something like this: {{ results_coin_data.market_data.current_price.{{ currency }} }} (Obviously that doesn't work.) Where {{ currency }} is a selected currency that the user chooses from a dropdown select form. The template is then refreshed with the variable changed showing a price in the chosen currency. I can't send it to the view with a form GET request because the API endpoint doesn't have an option for a currency parameter. The currency option is part of the JSON results, so I have to chain it to the end like .current_price.usd or .current_price.gbp thank you! -
Showing an exception while adding product to admin panel
django.db.utils.OperationalError: no such column: "mfg_date" models.py from django.db import models '#Create your models here. class Product(models.Model): prod_id = models.AutoField prod_name = models.CharField(max_length=30) prod_des = models.CharField(max_length=100) mfg_date = models.DateField() admin.py from django.contrib import admin from .models import Product Register your models here. admin.site.register(Product) views.py from django.contrib import admin from django.urls import path from . import views urlpatterns = [ path('index/',views.index,name='shopindex'), path('aboutus/',views.aboutus,name='aboutus'), path('contactus/',views.contactus,name='contactus'), path('search/',views.search,name='search'), path('productview/',views.productview,name='productview'), path('tracker/',views.tracker,name='tracking'), path('checkout/',views.checkout,name='checkout') ] Image of error [image of error][1] -
Too many values to unpack (expected 2) in Django
`I want to render the checkbox in the template goals.html, what am i doing wrong here? Models.py from django.db import models # Create your models here. there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a set of goals and there should be 2 check boxes associated with them there would be a … -
angular and django issue on refresh token (jwt)
I have a django backend whith authentication based on JSON Web Token using the package djangorestframework-simplejwt. My Angular 9 frontend successfully obtain the access and refresh token after login using this function loginUser(email: string, password: string) { return this.httpClient.post<Token>(`${this.baseService.baseUrl}auth-token/`, { email, password }, this.baseService.httpOptions); } The problem comes in when I try to fetch the refresh token. This is the function I use to request the refreshing getNewAccessToken(refreshToken: string){ return this.httpClient.post<Token>(`${this.baseService.baseUrl}auth-token-refresh/`, { refresh: refreshToken }, this.baseService.httpOptions); } I keep get 401 Unauthorized as response. Both functions use the same httpHeader which is httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }) }; I test my backend refresh endpoint with Postman using refresh: refreshtokenstring... in the body and everything works perfectly. I do not understand what's going wrong with Angular -
Django Admin: How to customize autocomplete_fields width to adapt to the content?
I'm trying to customize the widget rendered by autocomplete_fields for a ForeignKey Model field. Basically the problem is that the widget is too narrow for the content and it's breaking in two lines inside the select: I have seen that the JS library or JQuery plugin is called Select2 and it has a "dropdownAutoWidth" to make it adapt to the parent element size that kind of works but I'm completely clueless on how to set that option from admin.py since it seems that options are never passed in the code at django.contrib.admin.widgets.AutocompleteMixin.media: def media(self): extra = '' if settings.DEBUG else '.min' i18n_name = SELECT2_TRANSLATIONS.get(get_language()) i18n_file = ('admin/js/vendor/select2/i18n/%s.js' % i18n_name,) if i18n_name else () return forms.Media( js=( 'admin/js/vendor/jquery/jquery%s.js' % extra, 'admin/js/vendor/select2/select2.full%s.js' % extra, ) + i18n_file + ( 'admin/js/jquery.init.js', 'admin/js/autocomplete.js', ), css={ 'screen': ( 'admin/css/vendor/select2/select2%s.css' % extra, 'admin/css/autocomplete.css', ), }, )