Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Jupyterlab for Flask or Django web development
I recently found out about Jupyterlab. I like the improvement over plain Notebooks. I was hoping we could actually use Jupyterlab as an online IDE for web development of Django, Flask or other projects. I don't like developing in a local environment. However I cannot find anything about using Jupyter for web development. Not in the Github repo or on Google. Opening normal .py the tab function to list all functions, classes etc don't work. This also doesn't work when importing a .py file in a .ipynb file. Using nbconvert and p2j to convert all files back and forth from .py to ipynb and vice versa isn't a really efficient. And besides this, another issue with this approach is that if you import nb 2 in nb 1 and you change something in nb 2 you have to restart the entire kernel of nb 1 in order to have the changes take effect. Only rerunning the import or importlib.reload(nb2) doesn't work. Is there a good approach to this? -
Postgresql query generated by django works too slow
I have a query like select id from x where is_valid=true and id in (select another_id from y where (other_id in (1, 2, 3, 4, 5, 6, 7, 8, 11, 16, 17, 18, 19, 20, 21, 22, 24, 26, 27, 28, 30, 31, 32, 34, 35, 36, 37, 38, 41, 43, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 63, 65, 67, 69, 72, 73, 76, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 94, 95, 96, 97, 98, 100, 101, 102, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 121, 127) and is_valid=true));. I have index on is_valid fields and also has index together on other_id and is_valid. X table has +900k and Y table has +15M entry in them. another_id and other_id fields are primary keys of Y table. This query takes place at least 30sec to perform and it is a problem for API to response. I am using PostgreSql 9.6 and Django 1.11 for development. Can you suggest a way to faster this operation? -
How to properly throw error to user from admin's save_formset method?
I have ModelAdmin with customized save_formset() method. It this method I call thirdparty API, do some loginc and accordingly do save instances. Sometimes this API returns error so I need to handle it and show user error telling that data in inline forms must be changed. I can't find the way to return the error properly. Django docs warns us that if we override ModelAdmin.save_model() or ModelAdmin.delete_model(), we must save the object in any ways. Does this rule also applies to ModelAdmin.save_formset() too? -
Remove perviously added values from template created using Formset
I'm able to save values in database using Formset but when I tried to create one more form then perviously added values are getting showed in the template. This is a strange issue but I want to know how to solve this issue. -
Editing Django Form Field
I am using django with AngularJS to create a form view, which takes details from user and stores it. Now I wish to edit those details by going to the detail view page for a particular entry. How do I do that, so that I can use already created templates? -
Using interactive shell with Dropwizard
So recently I started trying out Dropwizard since I wanted to learn how to build a REST service using Java. Btw I have worked with Django and python but have no experience with Java. Is there a way to use a interactive shell in Dropwizard like the one which Django provides using python manage.py shell -i ipython. I'm using Java 8. I know JShell has been integrated with Java 9 but Dropwizard uses JDK 8. -
How to recieve not all fields from related(ForeignKey) django model using 'select_related'
Env: python >= 3.6 Django >=2.1.0 Models example: class A(models.Model): id = models.AutoField(primary_key=True) a_field = models.CharField(max_length=256, default="example") class B(models.Model): id = models.AutoField(primary_key=True) b_field = models.CharField(max_length=256, default="example") a = models.ForeignKey(A, related_name="b", on_delete=models.CASCADE) Question: How I can fetch only required fields from related models using select_related() How it could be done with prefetch_related: from django.db.models import Prefetch prefetch_obj = Prefetch("a", queryset=A.objects.only("id", "a_field")) B.objects.only("id", "b_field", "a").prefetch_related(prefetch_obj) But it produces 2 request to DB due to prefetch_related using. SELECT `main_b`.`id`, `main_b`.`b_field`, `main_b`.`a_id` FROM `main_b` LIMIT 21; args=() SELECT `main_a`.`id`, `main_a`.`a_field` FROM `main_a` WHERE `main_a`.`id` IN (1); args=(1,) If I use select_related it makes 1 DB call, but fetches all fields from A model: models.B.objects.only("id", "b_field", "a").select_related("a") SELECT `main_b`.`id`, `main_b`.`b_field`, `main_b`.`a_id`, `main_a`.`id`, `main_a`.`a_field`, `main_a`.`a_not_required_field` FROM `main_b` INNER JOIN `main_a` ON (`main_b`.`a_id` = `main_a`.`id`) LIMIT 21; args=() -
How to create a variable from an url?
My URL takes a variable "payment_option" like this: path('payment/<payment_option>/', PaymentView.as_view(), name='payment') I want to show that variable in the template rendered out by PaymentView. The problem is that "payment_option" isn't a paramenter in my models (like pk or a slug) but his value is got from a form handled by another view. class CheckoutView(View): [...] def post(self, *args, **kwargs): form = CheckoutForm(self.request.POST or None) [...] payment_option = form.cleaned_data.get('payment_option') if payment_option == 'S': return redirect('core:payment', payment_option='stripe') elif payment_option == 'P': return redirect('core:payment', payment_option='paypal') Hope my explanation was as clear a possible, I'm new to Django so any hint or suggestion is really appreciated. -
Trying to pass request image from Django to Python script
Im creating a project where i'm sending a Request with an image to my Django local server. The image is received and saved which works fine. However, after receiving the image i am trying to pass the file to my python Script. There it should run the script with the image and return data back to the user. Im stuck as i can't seem to pass the image filename to the python script and therefore run the script with the image. Does anyone know how this can be done? The many (#):s are my attempts to do this. views.py class ImageViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView): """ Upload image (Save and Run Script) """ parser_classes = (JSONParser, MultiPartParser, FormParser,) queryset = UploadImageTest.objects.all() serializer_class = ImageSerializer def output(self, request): imagename = request.FILES['image'] #imagename = request.data['image'] #imagename = models.ImageField #imagename = request.FILES['image'].name #imagename = UploadImageTest.image print(imagename) #self.request.FILES['image'] #inp = request.data['image'] inp = request.FILES.get(imagename) out = run([sys.executable,'//Users/macbookpro//LivlymapAPI//venv//Livlymap_Textcrawl.py',inp],shell=False,stdout=PIPE) print(out) return {"status": "Success", "output": str(out)} Serializer.py class ImageSerializer(serializers.ModelSerializer): class Meta: model = UploadImageTest fields = ('id', 'name', 'description', 'image') models.py class UploadImageTest(models.Model): permission_classes = (IsAuthenticated,) name = models.CharField(max_length=200) description = models.TextField(blank=True) image = models.ImageField(upload_to="images/", null=True, blank=True) def __str__(self): return "{}".format(self.name) Python Script file_name = sys.argv[1] print(file_name) img = … -
Adapt wagtail tag filter to include an operator so multiple tags can be searched
I have a working search form on my Wagtail template that finds pages based on their tags but as there is no operator (AND or OR) only one tag can be searched at a time. Can someone advise me how I would adapt this code to allow for multiple tag searches? models.py tag_posts = PostsPage.objects.live().public().order_by('-first_published_at') if request.GET.get('tag', None): tags = request.GET.get('tag') tag_posts = tag_posts.filter(tags__slug__in=[tags]) # Paginate all posts by 2 per page paginator = Paginator(tag_posts, 2) # Try to get the ?page=x value page = request.GET.get("page") try: posts = paginator.page(page) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) context["posts"] = tag_posts return context search_page.html <form role="search" method="get" class="form-search" action="posts" method="get"> <div class="input-group"> <input type="text" class="form-control search-query" name="tag" placeholder="What are you after?" title="Search for:" /> <span class="input-group-btn"> <button type="submit" class="btn btn-default" name="" id="searchsubmit" value=""> <span class="btn btn-default">GO</span> </button> </span> </div> </form> -
How to pass variable inside translation tag [duplicate]
This question already has an answer here: django translate variable content in template 8 answers Is there any way to pass app variable or any data from database inside {% trans %} tag ? I tried {%blocktrans%} but it doesn't work. views.py def index(request, *args, **kwargs): carousel = CarouselImage.objects.all() return render(request, 'home.html', {'carousel':carousel}) models.py class CarouselImage(models.Model): title = models.TextField() subtitle = models.TextField(default='') img = models.ImageField(upload_to='carousel_images') objects = models.Manager() home.html {% for slide in carousel %} <h2>{% trans 'Need object from slide here'%}</h2> {% endfor %} -
Remove object from list in Django
I'm developing tags system and I need to show similar objects by tags (ManyToMany relationship). How can I remove current opened object from QuerySet with similar objects? Main object page: similar objects - QuerySet [Checklist: Main object page, Checklist: Simple tasks, Checklist: nocat] How can I remove "Checklist: Main object page" from QuerySet? class ChecklistView(DetailView): model = Checklist template_name = 'checklistapp/page_checklist.html' def get_context_data(self, **kwargs): context = super(ChecklistView, self).get_context_data(**kwargs) context['tag_checklists'] = [] for tag in Tags.objects.filter(checklist=self.object): checklists = Checklist.objects.filter(tags=tag) context['tag_checklists'].append({ 'checklist': checklists }) return context -
Django: saving the Form does not work (The ModelForm Filters the ForeignKey choices by request.user)
I Managed to Filter the ForeignKey choices "Context.name" by "request.user" with the Code Below. First, I defined the Models. models.py class Extension(models.Model): username = models.CharField(primary_key=True, max_length=200, help_text='') callerid = models.CharField(max_length=200, help_text='') extension = models.CharField(max_length=3, help_text='') firstname = models.CharField(max_length=200, help_text='') lastname = models.CharField(max_length=200, help_text='') password = models.CharField(max_length=200, help_text='') context = models.ForeignKey('Context', on_delete=models.SET_NULL, null=True) def get_absolute_url(self): return reverse('extension-detail', args=[str(self.username)]) def my_get_absolute_url(self): return reverse('my-extension-detail', args=[str(self.username)]) def __str__(self): return self.username class Context(models.Model): name = models.CharField(primary_key=True, max_length=200, help_text='') countryprefix = models.CharField(max_length=200, help_text='') cityprefix = models.CharField(max_length=200, help_text='') number = models.CharField(max_length=200, help_text='') extensionsfrom = models.CharField(max_length=200, help_text='') extensionstill = models.CharField(max_length=200, help_text='') portscount = models.CharField(max_length=200, help_text='') def get_absolute_url(self): return reverse('context-detail', args=[str(self.name)]) def my_get_absolute_url(self): return reverse('my-context-detail', args=[str(self.name)]) def __str__(self): return self.name According to this model I created a ModelForm with an initial section init, in wich the username will be grabbed. The available contexts will then be filtered by the username. forms.py class MyExtensionCreateForm(forms.ModelForm): def __init__(self, context, *args, **kwargs): name = kwargs.pop('user') super(MyExtensionCreateForm, self).__init__(*args, **kwargs) self.fields['context'].queryset = Context.objects.filter(name=name) class Meta: model = Extension fields = '__all__' Than in the view i bassicly want to just save the to the database. The get_form_kwargs part of the view will pass the username to the ModelForm so we can filter the choices like … -
how to upload big file in django using temp folder
2 I know how to upload multiple files through django, but I have a problem when uploading a folder if there are subfolders in it. The django can't receive subfolders. I found the reason, because browser use '.' to represent a folder, but django can't parse it then stop parsing. Is there an elegant way to fix it? -
White page why clicking on button
I've created a button which is supposed to make a redirection on a new page. But when I click on it, I got a white page without any information on it. Here is the urls.py for the file. url(r'^layer_quarantine/', TemplateView.as_view(template_name='layer_quarantine.html'), name='layer_quarantine'), Here's the file named _layer_quarantine.html {% extends "layers/layer_base.html" %} {% load i18n %} {% load staticfiles %} {% load url from future %} {% load base_tags %} {% block title %} {% trans "Explore Layers" %} - {{ block.super }} {% endblock %} {% block body_class %}layers explore{% endblock %} {% block body %} <div class="page-header"> {% user_can_add_resource_base as add_tag %} {% if add_tag %} <a href="{% url "layer_upload" %}" class="btn btn-primary pull-right">{% trans "Upload Layers" %}</a> <a style="margin-right:30px" href="{% url "layer_browse" %}" class="btn btn-success pull-right"> {% if LANGUAGE_CODE == "fr" %} Toutes les couches {% else %} All layers {% endif %}</a> {% endif %} <h2 class="page-title"> {% if LANGUAGE_CODE == "fr" %} Couches en attente de validation {% else %} Layers awaiting validation {% endif %} </h2> </div> {% with include_type_filter='true' %} {% with facet_type='layers' %} {% with header='Type' %} {% with filter='type__in' %} {% include "search/_search_content.html" %} {% endwith %} {% endwith %} {% endwith %} … -
Is there a better way to schedule a task with Django?
I have made a Django project and I would like to run several scripts automatically, some of them every minutes other every hour ... My first thoughts were to make a custom Django command and create a crontab. Django - Set Up A Scheduled Job? Like this. I'm looking for if there is a better way in 2019 to made it or eventually if Django framework implemented this functionnality. My project work with docker (Image : Python / OS :Ubuntu). Thanks -
User registration by email confirmation in Django Rest API returns me "AttributeError: 'list' object has no attribute 'rsplit'
#here is my view class RegisterView(APIView): permission_classes = [AllowAny] def post(self, request, *args, **kwargs): email = request.data.get('email', False) password = request.data.get('password', False) role = request.data.get('role') if email and password and role: user = User.objects.filter(email=email) if user.exists(): return JsonResponse('Такой email уже существует', safe=False) else: temp_data = { 'email': email, 'password': password, 'role': role } serializer = CreateUserSerializer(data=temp_data) serializer.is_valid(raise_exception=True) # user.is_active = False user = serializer.save() current_site = get_current_site(request) print(current_site) subject = 'Activate Your MySite Account' message = render_to_string('account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) print(message) from_email = settings.EMAIL_HOST_USER print(from_email) to_email = email print(to_email) user_email = EmailMessage( subject, message, to=[to_email] ) print(user_email) user_email.send() return HttpResponse('Please confirm your email address to complete the registration') else: return JsonResponse('Email не указан', safe=False) here is my url url(r'^register/$', RegisterView.as_view(), name='register'), url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', views.activate, name='activate'), I'm trying to make a registration for users with email confirmation. But it turns me 'AttributeError: 'list' object has no attribute 'rsplit'. Also activation also returns 'can't set attribute'. Could anyone help me, please! -
Cannot log my superuser in ( custom user model / manager )
I'm trying to build super user using a custom user model and a custom user manager. I did exactly the same thing than the django doc about the create_superuser method and in my shell, I'm able to create a superuser with an email and a password. But when I try to log in on the django admin page, I have this wierd error : Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive. from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager from django.db import models from multiselectfield import MultiSelectField class UserManager(BaseUserManager): #custom create_user method def create_user(self, email, password=None): if not email: raise ValueError('Users must have an email address') user = self.model( email = self.normalize_email(email) ) user.set_password = password user.save(using=self._db) return user #Custom create_super_user method def create_superuser(self, email, password=None): user = self.create_user( email = self.normalize_email(email), password = password ) user.admin = True user.save(using=self._db) return user class User(AbstractBaseUser): #setting up Choices for interest, Must add other fields ... MATHS = 'mat' PHYSICS = 'phy' HISTORY = 'his' BIOLOGIE = 'bio' ECONOMICS = 'eco' POLITICS = 'pol' MUSIC = 'mus' ENGLISH = 'eng' FRENCH = 'fra' SPANISH = 'spa' LAW = 'law' COMPUTER_SCIENCE = 'cs' COMMUNICATION = … -
Django Test "database table is locked"
In my Django project, I have a view that when a user posts a zip file, it will respond immediately back and then process the data in the background with the help of threading. The view works fine in normal test, but when I run the Django's test it fails with a database table is locked error. Currently, I'm using default SQLite database and I know if I switch to another database this problem may be solved but I'm seeking an answer for the current setup. I trimmed the code for simplicity. Model.py: class DeviceReportModel(models.Model): device_id = models.PositiveIntegerField(primary_key=True) ip = models.GenericIPAddressField() created_time = models.DateTimeField(default=timezone.now) report_file = models.FileField(upload_to="DeviceReport") device_datas = models.ManyToManyField(DeviceDataReportModel) def __str__(self): return str(self.id) Serializers.py: class DeviceReportSerializer(serializers.ModelSerializer): class Meta: model = DeviceReportModel fields = '__all__' read_only_fields = ('created_time', 'ip', 'device_datas') views.py: from django.utils import timezone from django.core.files.base import ContentFile from rest_framework.response import Response from rest_framework import status, generics import time import threading from queue import Queue class DeviceReportHandler: ReportQueue = Queue() @staticmethod def save_datas(device_object, request_ip, b64datas): device_data_models = [] # ... # process device_data_models # this will take some time time.sleep(10) return device_data_models @classmethod def Check(cls): while(True): if not cls.ReportQueue.empty(): report = cls.ReportQueue.get() # ... report_model = DeviceReportModel( device_id=report['device_object'], ip=report['request_ip']) … -
What's the best choice of django rest framework + JWT + LDAP microservices authentication architecure?
I have to write a microservices project with drf+JWT+LDAP auth backend. I choose service1 as an auth service. It will work as an individual django service with JWT+LDAP. And in service2/service3 settings.py I will also set REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'] = 'rest_framework_jwt.authentication.JSONWebTokenAuthentication'. Then my question is: in service2/service3 code, do I need to override the authenticate interface of class BaseJSONWebTokenAuthentication to verify token with a restful call to service1? Or just run jwt_decode_handler locally with a same secret key to service1 in settings.py? -
Error 404 when loading css in static folder
I have created CSS files in a folder static and currently I'm getting this error 404 when its trying to load the page and no css is being recognised. my folders are DEMOPROJECT -DEMOAPP --migrations --templates ---DEMOAPP -----homepage.html ----base.html -DEMOPROJECT --static ---css ----NavMENU.css --settings.py --urls.py My terminal is this (venv) C:\Users\jferguson\PycharmProjects\WebP1\DEMOPROJECT> I have tried different settings that every other person has asked. None seem to be working. THIS is in the urls.py file for static folder. urlpatterns = [ re_path(r'^admin/', include(wagtailadmin_urls)), re_path(r'^documents/', include(wagtaildocs_urls)), re_path(r'', include(wagtail_urls)), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) This is in settings STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' -
Is there any way we can import models in django settings.py?
I am using django-auth-ldap for authentication, now I want to get data for field like AUTH_LDAP_BIND_DN, AUTH_LDAP_BIND_PASSWORD, AUTH_LDAP_SERVER_URI from database. import subprocess from django.apps import apps from django.core.exceptions import ObjectDoesNotExist ldap = False Config = apps.get_model('config', 'Config') Config = Config() try: ldap = Config.objects.get(name="ldap") except ObjectDoesNotExist: pass if ldap and check_ldap_connection(): import ldap from django_auth_ldap.config import LDAPSearch AUTH_LDAP_SERVER_URI = Config.objects.get(name="ldap_server_uri") AUTH_LDAP_BIND_DN = Config.objects.get(name="ldap_bind_dn") AUTH_LDAP_BIND_PASSWORD = Config.objects.get(name="ldap_bind_password") ldap_search = Config.objects.get(name="ldap_search") AUTH_LDAP_USER_SEARCH = LDAPSearch( ldap_search , ldap.SCOPE_SUBTREE, "(uid=%(user)s)" ) def check_ldap_connection(): try: ldap_server_uri = Config.objects.get(name="ldap_server_uri") ldap_bind_dn = Config.objects.get(name="ldap_bind_dn") ldap_search = Config.objects.get(name="ldap_search") ldap_bind_password = Config.objects.get(name="ldap_bind_password") except ObjectDoesNotExist: return False cmd = "ldapsearch -H \"" + ldap_server_uri + "\" -D \"" + ldap_bind_dn + "\" -w \"" + ldap_bind_password \ + "\" -b \"" + ldap_search + "\" | " + "grep result" try: connection = "" connection = subprocess.check_output(cmd, shell=True).decode() except Exception as e: return False connection = connection.split() if "0" and "Success" in connection: return True return False Error: raise AppRegistryNotReady("Models aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. -
Django group by a pair of values
I would want to do a group by with a pair of values in django. Consider I have the following model, class Vehicle: vehicle_id = models.PositiveIntegerField() version_id = models.PositiveIntegerField(default=1) name=models.CharField(max_length=8096) description=models.CharField(max_length=8096) vehicle_id is not the primary key since there can be multiple rows in the table with the same vehicle_id but with different version_id Now I would want to get the latest versions of all the vehicles. select * from ( select vehicle_id, MAX(version_id) as MaxVersion from Vehicle group by vehicle_id ) s1 JOIN Vehicle s2 on s2.vehicle_id = s1.vehicle_id AND s2.version_id=s1.MaxVersion; Tried to represent this in Django ORM like below, feed_max_version = ( self .values("vehicle_id") .annotate(max_version=Max("version_id")) .order_by("vehicle_id") ) q_statement = Q() for pair in feed_max_version: q_statement |= Q(vehicle_id__exact=pair["vehicle_id"]) & Q( version_id=pair["max_version"] ) return self.filter(q_statement) But this seems less efficient and takes long time to load. I am not very keen on having raw SQL since I would not be able to add any more queryset methods on top of it. -
Dynamic search using date
Can any one please help me i want to search my expenses income which is in my models.py using date wise dynamic search where user will input date and he will see the result in template i dont know how to code that template ..I tried form but not working.. :( my views.py def date_page(request): f_date = '2018-12-05' t_date = '2019-09-12' from_date = datetime.datetime.strptime(f_date, "%Y-%m-%d").date() to_date = datetime.datetime.strptime(t_date, "%Y-%m-%d").date() date_queryset = Add.objects.filter(date__range=(from_date, to_date)) print (date_queryset) return render (request , 'date_page.html',) -
What should be the port value for working on the production server?
When developing on a local machine I used standart code to authorize and work with Google disk creds = None cred = os.path.join(settings.BASE_DIR, 'credentials.json') # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( cred, SCOPES) creds = flow.run_local_server(port=8080) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) I use 8080 as the port parameter value. This works. When deploying a project on a production server, I get a message port is already in use. Server: Ubuntu 18.04, Nginx, uwsgi I tried to use the value port=None and on the local machine it worked, but on the server it gave an integer is required (got type NoneType) What values should I use?