Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Integration of mongoengine with django
I'm using Mongoengine for a Django project first of all I have installed it with pip install mongoengine In the project structure, I found that I'm using mongoengine 0.18.2 and pymongo 3.9.0 which are the latest versions for this moment but when I run the project I got the following error ImportError: cannot import name 'connection' from 'mongoengine' The error is raised for the following line on settings.py from mongoengine import * -
Django bulk_upsert saves queries to memory causing memory errors
For our Django web server we have quite limited resources which means we have to be careful with the amount of memory we use. One part of our web server is a crom job (using celery and rabbitmq) that parses a ~130MB csv file into our postgres database. The csv file is saved to disk and then read using the csv module from python, reading row by row. Because the csv file is basically a feed, we use the bulk_upsert from the custom postgres manager from django-postgres-extra to upsert our data and override existing entries. Recently we started experiencing memory errors and we eventually found out they were caused by Django. Running mem_top() showed us that Django was storing massive upsert queries(INSERT ... ON CONFLICT DO) including their metadata, in memory. Each bulk_upsert of 15000 rows would add 40MB memory used by python, leading to a total of 1GB memory used when the job would finish. Apparently Django does not release the query from memory after it's finished. Running the crom job without the upsert call would lead to a max memory usage of 80MB, of which 60MB is default for celery. We tried running gc.collect() and django.db.reset_queries() but the … -
How to fix 'CreateView is missing a QuerySet'
I'm trying to get a django app to manage accounts and users. Admin user can access the django admin site and do whatever the want for managing user. Simple user which have rights on users can add / modify users through forms but not access django admin site. Im' tryning to add a CreateView for simple user to adding users, but i keep getting this error : 'CreateView is missing a QuerySet' Is it cause I extend AbstractBase User ? Can someone explain me what are those queryset and how to fix my code ? Thanks you The code : ` #admin.py class UserCreationForm(forms.ModelForm): password = forms.CharField(label='Mot de passe', widget=forms.PasswordInput) password_confirm = forms.CharField(label='Confirmation du mot de passe', widget=forms.PasswordInput) class Meta: model = UserProfile fields = ('first_name', 'last_name', 'email', 'perm_on_user', 'perm_on_news', 'is_admin',) def clean_password_confirm(self): # Check that the two password entries match password = self.cleaned_data.get("password") password_confirm = self.cleaned_data.get("password_confirm") if password and password_confirm and password != password_confirm: raise forms.ValidationError("Les mots de passes ne correcpondent pas") return password def save(self, commit=True): # Save the provided password in hashed format user = super().save(commit=False) user.set_password(self.cleaned_data["password"]) if commit: user.save() return user class UserModificationForm(forms.ModelForm): password = forms.CharField(label='Mot de passe', widget=forms.PasswordInput) password_confirm = forms.CharField(label='Confirmation du mot de passe', … -
Use of regroup built-in template tag
I'm trying to regroup some data in a table using regroup. This is the model that I use: class EnographiaeVillages(models.Model): id_region = models.IntegerField() region = models.CharField(max_length=100) id_sub_region = models.IntegerField() sub_region = models.CharField(max_length=100) id_sub_region3d = models.IntegerField() sub_region3d = models.CharField(max_length=100) nom = models.CharField(max_length=100) def __str__(self): return self.nom class Meta: ordering = ['nom'] Following this indications I've use this: {% regroup villages_subregion by sub_region3d as subregion %} <table> {% for sub_region3d in subregion %} <tbody> <tr> <th>{{ sub_region3d.grouper }}</th> <td> <ol> {% for village in sub_region3d.list %} <li class="my-0">{{ village.nom }}</li> {% endfor %} </ol> </td> </tr> </tbody> {% endfor %} </table> villages_subregion cames from a context_processors: def villages_for_subregion(rerquest): villages_list = EnographiaeVillages.objects.all() villages_counter = villages_list.count() context = { 'villages_subregion': villages_list, 'villages_counter': villages_counter, } return context What I would like to see is this: sub_region3d 1 nom 1A1 nom 1B1 nom 2B1 nom 3B1 nom 1c1 nom 2C1 sub_region3d 2 nom 1B2 nom 2B2 nom 1c2 nom 2C2 nom 1D2 but I see this: sub_region3d 1 nom 1A1 sub_region3d 1 nom 1B1 nom 2B1 nom 3B1 sub_region3d 2 nom 1B2 nom 2B2 sub_region3d 1 nom 1c1 nom 2C1 sub_region3d 2 nom 1C2 nom 2C2 sub_region3d 2 nom 1D2 The table results illegible with a … -
Django Template - Select Value From List
I have a Django template that has a and I want to add the selected value to my view that is a post request to the database. When I have a normal char field I just do: name='Studyplan-Name' for example. But I can't do in the selecter name='Studyplan-Parent_Assignment'. How do I do this? @login_required def skapa_studieplan(request): if request.method == 'POST': name = request.POST.get('Studyplan-Name') description = request.POST.get('Studyplan-Description') parent_assignment = request.POST.get('Studyplan-Parent_Assignment') studyplan = Studyplan(name=name, description=description, parent_assignment=parent_assignment) studyplan.save() return redirect('allaStudieplaner') <h6>Välj en Uppgift att plugga inför</h6> <select class="browser-default custom-select"> <option selected, name="Studyplan-Parent_Assignment">Välj Uppgift</option> {% for x in Assignments %} <option value="1">{{ x.name }}</option> {% endfor %} </select> <hr> -
Image disappears after some hours
I have build a simple website using django framework,with a blog section to allow staff to write articles and upload an image. unfortunately after a few hours the articles and images will disappear,but when I check on aws s3 bucket,the images are still there. I have checked on my code and it is okay,the saved functionality is working well. I expect to have the content on the blog to be there permanently lest deleted from the back end. -
How to set environmental variables properly Gitlab CI/CD and Docker
I am new to Docker and CI/CD with Gitlab CI/CD. I have .env file in the root directory of my Django project which contains my environmental variable e.g SECRET_KEY=198191891. The .env file is included in .gitignore. I have set up these variables in Gitlab settings for CI/CD. However, the environment variables set in Gitlab CI/CD settings seem to be unavailable Also, how should the Gitlab CI/CD automation process create a user and DB to connect to and run the tests? When creating the DB and user for the DB on my local machine, I logged into the container docker exec -it <postgres_container_name> /bin/sh and created Postgres user and DB. Here are my relevant files. docker-compose.yml version: "3" services: postgres: image: postgres ports: - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data/ web: build: . command: /usr/local/bin/gunicorn writer.wsgi:application -w 2 -b :8000 environment: DEBUG: ${DEBUG} DB_HOST: ${DB_HOST} DB_NAME: ${DB_NAME} DB_USER: ${DB_USER} DB_PORT: ${DB_PORT} DB_PASSWORD: ${DB_PASSWORD} SENDGRID_API_KEY: ${SENDGRID_API_KEY} AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} AWS_STORAGE_BUCKET_NAME: ${AWS_STORAGE_BUCKET_NAME} depends_on: - postgres - redis expose: - "8000" volumes: - .:/writer-api redis: image: "redis:alpine" celery: build: . command: celery -A writer worker -l info volumes: - .:/writer-api depends_on: - postgres - redis celery-beat: build: . command: celery -A writer beat -l info … -
Virtualenv intersection (python, django)
When I did the command: mkdir djangoproject cd djangoproject/ python3 -m venv myvenv source myvenv/bin/activate echo Django~=2.0.6 > requirements.txt pip install -r requirements.txt django-admin startproject projectname . python manage.py runserver I see the error at localhost: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/catalog/ I did not create urls for "catalog" (used "catalog" in previous project) I think that the issue deals with intersection of venvs. (I deleted previous project, but the error stayed) How can I fix it? -
django-oscar How does the download work? for digital product
guys, I started an all e-shop for digital products using django-oscar reading the documentation, it states that support for digital products is available. but I haven't figured it out yet. I will be grateful for any help. -
Reverse for 'ptrips' with arguments '('', '')' not found. 1 pattern(s) tried: ['details/trips/(?P<trip_id>[0-9]+)/(?P<passenger_id>[0-9]+)/$']
This is a train tickets booking system, im trying to pass 2 id(trip_id/passenger_id) at the same url path so the django can remember the number of passengers and then create a number of tickets forms based on the number of passenger by id urls.py path('trips/<int:trip_id>/',views.ticket_booking, name='trips'), path('trips/<int:trip_id>/<int:passenger_id>/',views.passengers_page, name='ptrips'), path('review/<int:ticket_id>/',views.review_page, name='rtickets'), re_path(r'^from/(?P<start_station>[0-9]{1,50})/$', views.details_page, name='details_page'), path('', views.details_page, name='booking'), views.py from django.shortcuts import render, redirect from django.http import HttpResponse from django.core.exceptions import ValidationError from .models import Ticket, Trip, Passenger from django.http import Http404 from django.shortcuts import get_object_or_404 def ticket_booking(request, trip_id, passenger_id): trip = get_object_or_404( Trip, pk=trip_id) passengers = list(Passenger.objects.filter(pk=passenger_id)) error = None ticket = None if request.method == 'POST': first_name = request.POST.get('first_name') middle_name = request.POST.get('middle_name') last_name = request.POST.get('last_name') email = request.POST.get('email') gender = request.POST.get('gender') ticket = Ticket(trip=trip,first_name=first_name, middle_name=middle_name, last_name=last_name, email=email,gender=gender) try: ticket.full_clean() ticket.save() return redirect('tickets',ticket_id=ticket.id) except ValidationError as e: error = dict(e) print(e) context = { 'trip': trip,'error':error, 'ticket':ticket, 'passengers':passengers } return render(request, 'details/ticket.html', context) html <div class="container marketing"> {% for trip in trips %} <div class="card text-center "> <div class="card-header"> Ride Details </div> <div class="card-body"> <h4>Start</h4> <h5 value="{{trip.id}}">{{trip.start_station}}</h5> <h4>End</h4> <h5 value="{{trip.id}}">{{trip.end_station}}</h5> <a class="btn btn-primary" href="{% url 'ptrips' trip_id passenger.id %}" role="button">Book Now</a> </div> </div> {% endfor %} </div> -
How to create a GROUP of users with it MANAGER able to perform an ACTION in DJANGO 2.1
How can I implement the feature for a user in the web app to create a specific “group” then become sort of the manager of that group with the ability to accept people, delete users who belong to that group. Other users can request access to that group. Each group created has the ability to perform an action. I am looking for the best the approach to build this feature. -
How to cancel Stripe subscription in Django?
This does not work. I'm trying to cancel a user's subscription. user = request.user # here this pulls the customer customer = stripe.Customer.retrieve(user.stripe_customer_id) # this fails stripe.Subscription.delete(customer['subscription']) -
How to build json like this
I want to build json something like this { "meta":[ { "key1": 12345, "key2": "berlin", "key3": "best place", "key4": 58 }, { "key1": 6789, "key2": "bangldesh", "key3": "great place", "key4": 58 } ] } I am getting data for that in a list called mylist. mylist may have n size <class 'list'>: [('berlin', 12345, 'best place', 58), ('bangldesh', 6789, 'great place', 58)] I also have keys variable as keylist. keylist may con be of n size <class 'list'>: [('key1',), ('key2',), ('key3',), ('key4',)] I tried to build json as count = 0 for i in range(0,maxLen): key = keylist[count][0] value = mylist[0][count] data[key] = value print(data) count = count + 1 jsonResult = json.dumps(data) I am getting name error -
django-tables2 reverse access foreign attributes from through table
I have got 3 tables of Truck, Waybills, and Switch. The Switch table is a through table that is the product of a many-to-many recursive relationship of the Truck with itself. I am using a tables2-table to display the data. The problem is that I was not able to display attributes from the Waybill table in reverse from either the Truck or the Switch table. Here are the models showing @property function that I used unsuccessfully for the reverse: class Waybill(models.Model): waybill_id = models.AutoField(primary_key=True, verbose_name = 'Waybill ID') truck_no = models.ForeignKey(Truck, on_delete=models.CASCADE, to_field='truck_no', related_name='waybills', verbose_name='Truck Registration No') vis_waybill_no = models.PositiveIntegerField(default=1, unique=True, db_index=True, null=True, blank=True, verbose_name='Vision Waybill No') man_waybill_no = models.CharField(max_length=75, unique=True, default='null', null=True, blank=True, db_index=True, verbose_name='Manual Waybill No') organization = models.CharField(max_length=75, null=True, blank=True, default="UNICEF", verbose_name='Organization') destination = models.CharField(max_length=75, null=False, default="Bentiu", verbose_name='Destination') class Truck(models.Model): truck_id = models.AutoField(primary_key=True, verbose_name='Truck ID') truck_no = models.CharField(max_length=14, unique=True, db_index=True, null=False, verbose_name='Truck Plate No') trailer_no = models.OneToOneField(Trailer, on_delete=models.CASCADE, unique=False, null=True, to_field='trailer_no', related_name='trucks', verbose_name='Trailer No') make = models.CharField(max_length=14, null=True, verbose_name='Make') switches = models.ManyToManyField('self', through='Switch', symmetrical=False, related_name='switched_to') def __str__(self): return u'%s' % self.truck_no+" / "+self.trailer_no.trailer_no @property def get_organization(self): waybills = self.waybill_set.all() print(x.truck_no for x in waybills) organization = waybills.objects.filter(truck__pk=1) return organization class Switch(models.Model): from_truck = models.ForeignKey(Truck, on_delete=models.CASCADE, related_name='from_trucks') credit_truck … -
how to iterate other fields when using group-by or values() in django(how to get data of other fields in the same instance)
how to get other fields when i use values() or group by in django i want to get other fields data in the same model (instance) class Product(models.Model): product_name = models.CharField(unique=True, max_length=50) pass def __str__(self): return self.product_name class Order(models.Model): id = models.AutoField(primary_key = True) products = models.ManyToManyField(Product ,through='ProductOrder') pass def __str__(self): return str(self.products.all()) class ProductOrder(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) ordering = models.ForeignKey(Order, on_delete=models.CASCADE,blank=True,null=True) pass def __str__(self): return str(self.product) views.py class Lists(ListView): template_name = 'show.html' context_object_name = 'productss' def get_queryset(self): return ProductOrder.objects.filter(ordering__active=True).values( 'product').annotate( quantity_total=Sum(F('quantity'))) {% for product in productss %} <tr class=""> <td style="text-align: center;">{{ product.product }}</td> {% endfor %} and also tried this <td>{{ product.product.product_name }}</td> is there a way to achieve this goal? thanks for any advice ! -
How to get the HTTP request of python -m x.wsgi?
In my Server, I use SCREEN -d -m python3 -m Qn02.wsgi running my project, I can see by ps -ef | grep python3 root 2285 1 0 20:42 ? 00:00:00 SCREEN -d -m python3 -m Qn02.wsgi but I have a question, that's how to see the requests from client? Use the python3 -m x.wsgi, how to get the HTTP request log? or current HTTP request? -
Django select_for_update function in concurrent process
When I testing the function of update_or_create in multi threading condition, I found the result is not what I wanted,they created more than one record in MySQL. As the code show, update_or_create used select .. for update to lock rows in MySQL, then it should be only one record in MySQL. I used SQLAlchemy and row sql has proved that. So, is the Django codes wrong? with Django code: def get_or_create_ins(): p, created = OxalicAcid.objects.update_or_create(defaults={"formula": "20", "degree": "80"}, name="smart") def run(): for i in range(10): t = threading.Thread(target=get_or_create_ins, args=()) t.start() if __name__ == "__main__": # more than one record will be created run() with SQLAlchemy code: @contextmanager def transaction_atomic(): session = Session() try: yield session session.commit() except Exception as e: session.rollback() raise e def get_result_with_update(session, name): sql = text(""" select * from acid_oxalicacid where name = :name for update """) params = dict(name=name) cursor = session.execute(sql, params) result = cursor.fetchall() return result def get_result(session, name): sql = text(""" select * from acid_oxalicacid where name = :name """) params = dict(name=name) cursor = session.execute(sql, params) result = cursor.fetchall() return result def create_data(session, name, degree, formula): sql = text(""" insert into acid_oxalicacid (name, degree, formula) values (:name, :degree, :formula) """) params = … -
DRF: AttributeError: type object 'Plan' has no attribute 'get_extra_actions'
I an creating a rest API where I want to have a url with a query parameter like this: http://127.0.0.1:8000/API/prices?portion=small To do this I am folliwing the official docs: "Filtering against query Parameter" This is my view: class PurchaseList(generics.ListAPIView): serializer_class = PurchaseSerializer def get_queryset(self): queryset = Purchase.objects.all() portion = self.request.query_params.get('portion', None) if username is not None: if portion == "small": queryset = queryset.filter(portion=False) elif portion == "large": queryset = queryset.filter(portion=True) return queryset In my urls.py I am creating a router and registering this view like below: router = routers.DefaultRouter() router.register(r'prices', views.PurchaseList, base_name='subprices') urlpatterns = [ path('API/', include(router.urls)), ... ] However, this is producing the below error on runserver: AttributeError: type object 'PurchaseList' has no attribute 'get_extra_actions' I have already managed to make the api work without any query following their docs again. How can I solve this. im certinely doing something wrong either in the router.register() or in the view -
How to specify a list of values in a form in which CheckboxSelectMultiple is used?
I have a problem with the form in django. It uses the CheckboxSelectMultiple widget, but I need to set a list of possible choices only for posts created by the currently logged in user. Any idea? form: class CycleForm(BSModalForm): class Meta: model = Cycle fields = ['title', 'description', 'posts'] widgets = { 'posts': forms.CheckboxSelectMultiple(), } models class Cycle(models.Model): title = models.CharField(max_length=200, unique=True) description = models.TextField(max_length=500, default="Brak opisu") date_created = models.DateTimeField(default=timezone.now) author = models.ForeignKey(User, on_delete=models.CASCADE) posts = models.ManyToManyField(Post) def __str__(self): return self.title -
How to add comments form to frontend in Django?
I wanna add comments form to my Django project. I've added successfully my Comments app to Django admin panel and everything works well in the admin panel. I don't know how to show the comments form on the frontend and that form to be usable by the users. models.py class Comment(models.Model): post= models.ForeignKey(Tellmeyourstory, on_delete=models.CASCADE, related_name='comments') user2 = models.ForeignKey(User, on_delete=models.CASCADE) name = models.CharField(max_length=80) email = models.EmailField() body = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) active = models.BooleanField(default=True) class Meta: ordering = ('created',) def __str__(self): return 'Comment by {} on {}'.format(self.name, self.post) views.py def detail(request,stories_id): stories=get_object_or_404(Tellmeyourstory, pk=stories_id) return render(request, 'detail.html', {'stories': stories}) -
Append value to Django model
I have 2 DB Models: class Book(models.Model): title = models.CharField(max_length=150, null=False) author = models.CharField(max_length=150, null=False) date_pub = models.DateField(verbose_name="Date Published") and class Customer(models.Model): name = models.CharField(max_length=25, null=False) surname = models.CharField(max_length=35, null=False) age = models.IntegerField(null=False) book = models.ForeignKey(to=Book, on_delete=models.CASCADE) My app was created recently, so i use manage.py shell to insert data to these models. For example, let's say I created books with title "Kolobok" and "Zolotaya Ribka". Next, I created customer "Vasya". When I created customer "Vasya", I pointed field book as "Kolobok". But over time, i want to append book "Zolotaya Ribka" to "Vasya" 's books. How i can to that? -
how to submit dynamic table to with html post
I have a html table which basically derived from my django model <form action="{% url 'checkout' %}" method="POST"> <table id="table" class="table"> <thead> <tr> <th scope="col">#</th> <th scope="col">Item</th> <th scope="col">Quntity</th> <th scope="col">Price</th> </tr> </thead> <tbody> {% csrf_token %} {% for item in cartitems %} <tr> <th scope="row">{{ forloop.counter }}</th> <td>{{ item.name }}</td> <td data-count="1" id="counter_{{ forloop.counter }}"> <button onclick="minus({{ forloop.counter }})" id="minusButton" data-count="1" class="btn btn-default btn-xs"> <i class="fas fa-minus mr-2"></i> </button> 1 <button onclick="plus({{ forloop.counter }})" id="plusButton" data-count="1" class="btn btn-default btn-xs"> <i class="fas fa-plus ml-2"></i> </button> </td> <td data-price="{{ item.price }}" id="counter_{{ forloop.counter }}"> {{ item.price }} </td> </tr> {% endfor %} </tbody> <tfoot> <tr> <td>Total Price</td> <td></td> <td></td> <td id='totalPrice'></td> </tr> </tfoot> </table> </div> <div> <button type="submit" class="btn btn-default btn-lg">Proceed To Checkout</button> </div> </form> my quantity field in table comes from user so I want to post all data from table to django, Pls let me know if there is any easy way to do that. right now my form doesnt do anything. I know you can post quatity field through ajax but I do not want that because user might also remove some of the items from the cart -
How to disable a button after clicking it and enable it after the login process is completed by checking the server response in django
I am working on a registration functionality in Django, python. When I click the Register button, the button should disable and after successful registration, it should enable again automatically. How to achieve this. Please help. Thank you. I tried using ajax, but I don't know how to check the response from the server, whether the user is registered successfully or not. -
ModuleNotFoundError in gunicorn start
My Django project has this directory structure: . ├── gnjp │ ├── gnjp │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ ├── app1 │ ├── app2 │ ├── manage.py │ └── templates ├── bin ├── include ├── lib using a virtual environment and works fine with python manage.py run server using some pip-installed modules like django-bootstrap4, django-bootstrap-datepicker-plus. From the higher-level gnjp-directory I do gunicorn gnjp.wsgi and get this error: [2019-09-07 14:20:28 +0200] [72408] [INFO] Starting gunicorn 19.9.0 [2019-09-07 14:20:28 +0200] [72408] [INFO] Listening at: http://127.0.0.1:8000 (72408) [2019-09-07 14:20:28 +0200] [72408] [INFO] Using worker: sync [2019-09-07 14:20:28 +0200] [72411] [INFO] Booting worker with pid: 72411 [2019-09-07 14:20:28 +0200] [72411] [ERROR] Exception in worker process Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker worker.init_process() File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process self.load_wsgi() File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi self.wsgi = self.app.wsgi() File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load return self.load_wsgiapp() File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp return util.import_app(self.app_uri) File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app __import__(module) File "/..../gnjp/gnjp/wsgi.py", line 16, in <module> application = get_wsgi_application() File "/usr/local/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application django.setup(set_prefix=False) File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup … -
How to create this type of json
I have a tuple like mytuple = ('somevalue', 99999, 'jjj', 99) from this tuple I want to make json like { "key1":'somevalue', "key2":'99999, "key3":'jjj', "key4":99 } the number of keys are not constant they can change and also value and number of keys are equal to number of objects I tried as data = {} for i in range(0,maxLen): key = keydata[0][i] value = valuedata[0][i] data[key] = data[value] I am getting key error while creating json