Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
In Django, how do I get a file path for an uploaded file when uploading?
I am trying to add some validation for user uploaded files. This requires running through a custom script I made called "sumpin", which only takes a filepath as a variable and sends back JSON data that will verify. Everything inside my script is working independently, putting it together where the error occurs. Since this is file validation, I decided to expand my file_extension validator that was already working. models.py from allauthdemo.fileuploadapp.slic3rcheck import sumpin def user_directory_path_files(instance, filename): return os.path.join('uploads', str(instance.objectid), filename) def validate_file_extension(value): ext = os.path.splitext(value.name)[1] valid_extensions = ['.stl','.STL'] if not ext in valid_extensions: raise ValidationError(u'Please upload a .stl file type only') data = sumpin(value.path) print (data) class subfiles(models.Model): STL = models.FileField(_('STL Upload'), upload_to=user_directory_path_files, validators=[validate_file_extension]) The error that I get is that the path (value.path) is not valid. This is the incorrect path because the upload_to tag must change this at a later point. This may be obvious, but I also need to have the file at the filepath location when my script is called. So essentially my questions are... How can pass the "upload_to" path into my validator to run through my custom script? Is there a better method to deal with uploaded files, like in the main class with … -
Django How can I cache properly to reuse data?(with using django-mptt package and redis module)
I am making kind of dynamic menu. when you click menu on the top, it show sub menu on the left side. I searched with keyword 'dynamic menu' from stackoverflow and google. I got idea to build that kind of menu. I made it like below. 1) render data(menu list) in context to template by custom context processor. 2) using custom template tag which is provided by django-mptt package. 3) show top menu in base template. 4) move to another template to show sub menu according to what top menu you click I made custom context_processor to use menu in context in every template. context_processor.py from manager.models import Menu def menu(request): menu_list = list(Menu.objects.all()) return {'menu':menu_list} template.py(example) {% load mptt_tags %} <nav id="{{ menu_id }}" class="tree-menu"> <ul> {% recursetree menu %} <li class="menu {% if node.is_root_node %}root{% endif %} {% if node.is_child_node %}child{% endif %} {% if node.is_leaf_node %}leaf{% endif %} {% if current_menu in node.get_descendants %}open{% else %}closed{% endif %} "> <a href="?{{ node.menu_name }}={{ node.pk }}">{{ node.menu_name }}</a> {% if not node.is_leaf_node %} <ul class="children"> {{ children }} </ul> {% endif %} {% if node.items and node.items.exists %} <ul class="items"> {% for item in node.items.all %} {% if … -
500 error with nginx loading django admin static files, but files load when making a request to the url
I'm deploying a django web app to a nginx server running ubuntu When I try to log into the app, which uses the default django auth system, I get a "Server Error (500)" message rendered When I look at /var/log/nginx/error.log it shows the following message: 2016/10/11 22:35:53 [error] 16325#0: *42863 open() "/path_to_app/app_namestatic/admin/js/jquery.min.js" failed (2: No such file or directory), client: ip, server: _, request: "GET /static/admin/js/jquery.min.js HTTP/1.1", host: "domain_name.com", referrer: "http://nu-tab.me/accounts/login/?next=/" 2016/10/11 22:35:53 [error] 16325#0: *42865 open() "/path_to_app/app_name/static/admin/js/jquery.init.js" failed (2: No such file or directory), client: ip, server: _, request: "GET /static/admin/js/jquery.init.js HTTP/1.1", host: "domain_name.com", referrer: "http://nu-tab.me/accounts/login/?next=/" 2016/10/11 22:35:53 [error] 16325#0: *42866 open() "/path_to_app/app_name/static/admin/js/admin/RelatedObjectLookups.js" failed (2: No such file or directory), client: ip, server: _, request: "GET /static/admin/js/admin/RelatedObjectLookups.js HTTP/1.1", host: "domain_name.com", referrer: "domain_name.com/accounts/login/?next=/" However, when I load domain_name.com/static/admin/js/admin/RelatedObjectLookups.js, the file loads properly. It's also worth noting that the error.log file has not been updated for recent requests. Not very familiar with setting up servers so let me know if there's important info here that I'm leaving out. -
I don't know how to through one queryset type to manytomany relations
I want to use Django to write a blog system.But the question is I can't get tags queryset from articles queryset. models: class Article(models.Model): ... tags = models.ManyToManyField(Tags,related_name='tags') author = models.ForeignKey( User,on_delete=models.CASCADE,to_field="username", ) ... class Tags(models.Model): tag_name = models.CharField(max_length=20) def __str__(self): return self.tag_name class Profile(models.Model): user = models.OneToOneField(User,on_delete=models.CASCADE,to_field='username',) ... views: .... def get_context_data(self,**kwargs): user=self.request.user object_list=Article.objects.all().filter(author=self.request.user.username).order_by(F('created').desc())[:100] kwargs['tags']=??? # I want to get a tags queryset related to object_list return super(UserIndexView,self).get_context_data(**kwargs) -
Is it possible a login system with facebook and django to the same user?
I am trying to figure out how I can allow users to login at my website using their facebook account or using an username and password which would direct them to the same account. Pyhton Social Auth creates a new user when I login with facebook. But I want this user to be able to login with an username and a password. But they must be the same user. I have been trying to do it but I couldnt. What would be the best approach? I used pipeline to get extra info from facebook. Should I use partial? -
Django forms security, model form vs forms
I'm learning django little by little I realized that I have some way to make a form, modelforms, forms and basic html with 'action= to view'. If we talk bout security, what is the best option, I asked that because I have create a form to update data un db, do it with modelform it's very easy but I have follow the queryset rules to do it because I did not found a way to update data with a form made by forms, so I have two questions, if there any best way to make what I made in the next code but following forms, instead to do it?. def update_info(request, pk): if request.method == "POST": name = request.POST['name_form'] company = request.POST['company_form'] detail = Data.objects.get(pk=pk) Data.filter(pk=pk).update(name=name, company=company) return HttpResponse('Name: %s Company: %s' % (name,company)) This code works fine but If you guy see there is not a way or maybe I do not know how to clean the form if form is nos post like, something like this. This method work with form or modelform else: form = my_form() Aso the question is, how can I can the form if this is not a form made with model form. and … -
scrapy without djangoitem : how to use foreign key
I want to use scrapy without djangoitem ,but I don't know how to deal with django foreignkey My database is MYSQL Please guide me. Thank you. models.py: -
Returning all rows in sqlite3 with newest timestamp value in column C
I have a table, wordcloud_words, in my sqlite3 db with the following columns: [A,B,C] with column C holding a timestamp. How can I get all rows with the latest timestamp in column C? Using SQL I would execute something like the following: SELECT * FROM wordcloud_words w1 where NOT EXISTS(SELECT * FROM wordcloud_words w2 WHERE w1.C < w2.C) How would I do an equivalent query using Django's provided ORM functionality? The model for the table: class Words(models.Model): A = models.CharField(max_length=128) B = models.IntegerField(default=1) C = models.TimeField(auto_now = False) def __init__(self,word,weight,time): self.A = word self.B = weight self.C = time -
Django: Template Does Not Exist Error
In My django app I have a view called 'StatsView' given below: class StatsView(LoginRequiredMixin, View): login_url = '/signin/' def get(self, request, template='app/ad_accounts/pixel_stats.html', *args, **kwargs): #Code return render(request, template, context) app/urls.py url( r'^ad_accounts/(?P<ad_account_id>[^/]+)/pixel_stats', StatsView.as_view(), name="pixel_stats" ), template pixel_stats.html <p> test</p> However when I go to localhost:8000/ad_accounts/acctid/pixel_stats/ I keep running into a Template DoesNotExist Error. I cant seem to figure out where Im going wrong. Ive added a bunch of URLs and havent run into this issue for any one them. My app structure is as follows: project/ app/ templates/ app_folder/ ad_accounts/ pixel_stats.html views/ ad_accounts/ stats.py -
Setting Django with virtualenvironment on an Apache
I just want to know if you really need to put this code in the wsgi.py when you want to deploy in apache with your django virtual environment activate_env=os.path.expanduser("/path/to/venv") execfile(activate_env, dict(__file__=activate_env)) This is not mentioned in the Django docs. However my virtualenv seems not to be used whenever I load my django page on the browser and of course throws a 500 error because my installed packages is not available Here is my apache conf file: <VirtualHost *:80> ServerName ai-labs.co ServerAlias www.ai-labs.co ServerAdmin admin@ai-labs.co DocumentRoot /var/www/html/ai-labs.co/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/html/ai-labs.co> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> Alias /static /var/www/html/ai-labs.co/static <Directory /var/www/html/ai-labs.co/static> Require all granted </Directory> Alias /static /var/www/html/ai-labs.co/media <Directory /var/www/html/ai-labs.co/media> Require all granted </Directory> <Directory /var/www/html/ai-labs.co/ai_labs_website> <Files wsgi.py> Require all granted </Files> </Directory> WSGIDaemonProcess ai-labs.co python-path=/var/www/html/ai-labs.co:/var/www/html/.virtualenvs_copy/ai-labs-website-pure-django/local/lib/python2.7/site-packages WSGIProcessGroup ai-labs.co WSGIScriptAlias / /var/www/html/ai-labs.co/ai_labs_website/wsgi.py process-group=ai-labs.co </VirtualHost> -
How should I enforce permission checking when file is reached/downloaded in Django?
I don't know this is a typical thing to do in web application, but what we achieve is that, let's say we have a Person model, inside this model, we have a FileField stores user's photo: class Person(models.Model): photo = models.FileField(upload_to='Person_photo') What I want to achieve is that only the owner can see his or her photo. People who log on a different account or even not log in should not be able to see the photo. Let's assume we have a way to check whether a photo belongs to a certain user or not: def permission(photo_filename, pid): # return True if photo_filename exists and belongs to the person pid We can figure this part out, for example, use permission system provided in Django. Of course from the views.py we can control whatever image we want to show on the page, but for example, we want to block people make attempts to figure out the URLs and get the photo, for example, typing http://some.domain/media/Person_photo/Amy.jpg in URL bar in the browser should only work if she is Amy. What is a good way to do it? Is there a library for this purpose? -
Using Django models, how can I return N random items from a table that have the fewest references by a foreign key?
For a research project, I am using Django tables and have a simple system where users need to classify photographs (which will later be used as training data for a neural network). The database will have tens of thousands of photos and around a dozen users. It will start with no classifications. My requirements are: A small number of photos should be presented to the user to classify To ensure all photos are classified, and that they're classified more or less equally often, I want to randomly select from the photos with the fewest number of classifications No user should classify the same photo twice I have two models like this: class Photo(models.Model): image_url = models.TextField() class Classification(models.Model): user = models.ForeignKey(User) photo = models.ForeignKey(Photo) classification = models.IntegerField() What I need to do is write a function that: Finds the Photos with the fewest number of Classifications Selects N Photos at random from that list which the user in question has not classified If there are fewer than N photos available in step 2, repeat to also include Photos from the next-fewest number of Classifications as necessary I also want this to be efficient--for example, I don't want to have the … -
Azure "App Service" - Django and SQLite
I have a django app (specifically django-rest). When I run the local copy of this site, my requests can be processed in 50-400ms. Next, I managed to deploy to Microsoft Azure App Service. Now, under the most expensive tier I can buy, responses are coming back in the 800-2000ms range. The app does simple queries on sqlite database. This database file is approximately 30 megabytes with the largest table is 12000 rows. I should point out all access to the database is read only, so no contention issues. Configuration: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(PROJECT_ROOT, 'mydatabase.db'), } } web.config: <?xml version="1.0"?> <configuration> <appSettings> <add key="WSGI_ALT_VIRTUALENV_HANDLER" value="django.core.wsgi.get_wsgi_application()" /> <add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS" value="D:\home\site\wwwroot\env\Scripts\activate_this.py" /> <add key="WSGI_HANDLER" value="ptvs_virtualenv_proxy.get_virtualenv_handler()" /> <add key="PYTHONPATH" value="D:\home\site\wwwroot" /> <add key="DJANGO_SETTINGS_MODULE" value="myapp.settings" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.0" /> <!-- Required for websockets. --> <httpRuntime targetFramework="4.5"/> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <handlers> <remove name="Python273_via_FastCGI" /> <add name="Python FastCGI" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\Python27\python.exe|D:\Python27\Scripts\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" /> </handlers> <rewrite> <rules> <rule name="Static Files" stopProcessing="true"> <conditions> <add input="true" pattern="false" /> </conditions> </rule> <rule name="Configure Python" stopProcessing="true"> <match url="(.*)" ignoreCase="false" /> <conditions> <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" /> </conditions> <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" /> </rule> </rules> </rewrite> </system.webServer> </configuration> Python version … -
Serving two Django sites with WSGI results in internal server error for only one of the sites
I have a Django site and a Django CMS site which I am serving from the same ubuntu 14.04 server running MySQL 5.6, Apache2 2.4.7 and Django 1.8 via mod_wsgi version 4.5.7 using virtual hosts. Locally (on my Linux PC) I have managed to accomplish this with both sites working perfectly and hence decided to migrate to the server. After the migration, taking care that everything has the same version, the situation is such that the Django site is working properly while the Django CMS site is giving me and internal server error 500. These are my virtual hosts' .conf files, and the wsgi.py file for the broken site. # wsgi.py import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bsg.settings") application = get_wsgi_application() # Django_cms_site.conf <VirtualHost *:80> ServerName site.com ServerAdmin webmaster@localhost.com Alias /static/ /home/bsg/cms/static/ Alias /media/ /home/bsg/cms/media/ WSGIScriptAlias / /home/bsg/cms/bsg/wsgi.py WSGIDaemonProcess bsgcms python-path=/home/hicklin/bsg/cms:/home/venv-bsg/lib/python2.7/site-packages WSGIProcessGroup bsgcms <Directory /home/bsg/cms> Require all granted </Directory> LogLevel warn ErrorLog /var/log/apache2/cms-error.log CustomLog /var/log/apache2/cms-access.log combined </VirtualHost> # Django_site.conf <VirtualHost *:80> ServerName django.site.com ServerAdmin webmaster@localhost.com Alias /static/ /home/bsg/admin/site/static/ WSGIScriptAlias / /home/bsg/admin/site/wsgi.py WSGIDaemonProcess bsgadmin python-path=/home/bsg/admin:/home/hicklin/venv-bsg/lib/python2.7/site-packages WSGIProcessGroup bsgadmin <Directory /home/bsg/admin> Require all granted </Directory> LogLevel warn ErrorLog /var/log/apache2/admin-error.log CustomLog /var/log/apache2/admin-access.log combined </VirtualHost> As can be noted, I am using the same … -
Sending an access token to server using AngularJS
I'm trying to use Django REST Framework's token-based authentication scheme with an AngularJS client. I'm able to successfully retrieve and store a token from the server, but I'm having trouble figuring out how to attach the token to subsequent responses. Here's the service that manages logging in and saving a token: angular.module('mymodule') .factory('loginService', function ($http, $window) { var api_base = "link to my api"; return { async: function() { return $http.get(api_base + "authentication/login/").then(function (response) { return response.data; }, function errorCallback(response) { console.log("Testuser API Error: " + response); return null; }); }, loginUser: function(email, password) { self.saveToken = function(auth_token) { $window.localStorage['jwtToken'] = auth_token; }; self.getToken = function() { return $window.localStorage['jwtToken']; }; console.log("...to listing " + email); return $http.post("link to my api/authentication/login/", { email: email, password: password }).then(function(response) { if(response.config.url.indexOf("link to my api") === 0 && response.data.auth_token) { self.saveToken(response.data.auth_token); } return response; }); } }; }); Here's the controller associated with the above service to handle logging in: angular.module('mymodule').controller("LoginController", function(loginService, $scope) { $scope.loginusers = []; $scope.refresh = function() { loginService.async().then(function(data) { if (data == null) return; console.log(data[0]["email"]); $scope.loginusers = []; for (var loginuser in data) $scope.loginusers.push(loginuser); }); }; $scope.refresh(); // Test // $scope.loginTestUser = function() { console.log("something..."); loginService.loginUser( $scope.email, $scope.password ) }; … -
Use host data in docker container build
I am building a development environment for a python/django project using docker. This is the first time I am using docker for something, so I am pretty noob, but I think I have a reasonable grasp on docker basic concepts. The problem I am having is with building an app server container. Since this is a dev environment, want the code to reside on my host system, where I can freely edit it. I want the container to run the code. So far, so good. But I need to run some commands on the code during the container build. And it seems that volumes are not mounted during the build process. In particular, I need to install pip requirements and I need to run django migrate command. I can work around pip, by copying the requirements file to the container, running the install and then deleting the file. But running migrate requires my whole codebase and copying the whole thing over just to delete it a minute later seems like an overkill. Running these commands on container start is not an option, I want to be able to stay at the current library versions and migration level if I choose. … -
Django 10 ListView filter with many fields
i start develop with Django. Past i wrote in Yii2 PHP and there from i have my habits. Goal: Index Documents Filter with many fields My solution work but i worried about is that good pracitce. Please tell me is it good? How to filter ListView with many field ? Is my solution to pagination url concate with other query params is good? I dont want to use admin-form and any ready to use solutions because i want to self learn. View: class IndexView(generic.ListView): template_name = 'documents/index.html' context_object_name = 'documents' model = Document paginate_by = 30 def get_queryset(self): q = self.request.GET.get('q', '') document_type = self.request.GET.get('type', '') income = self.request.GET.get('income', '') settlement = self.request.GET.get('settlement') qs = super(IndexView, self).get_queryset() if q: qs = qs.filter( Q(number__icontains=q) | Q(contractor__name__icontains=q) | Q(date_issue__icontains=q) ) if document_type: qs = qs.filter(type=document_type) if income: qs = qs.filter(income=income) if settlement: if int(settlement) == 1: qs = qs.filter(date_settlement__isnull=False) if int(settlement) == 2: qs = qs.filter(date_settlement__isnull=True) return qs.order_by('-date_issue', '-number') def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) q = self.request.GET.get('q', '') type = self.request.GET.get('type') if type: type = int(type) income = self.request.GET.get('income') if income: income = int(income) settlement = self.request.GET.get('settlement') if settlement: settlement = int(settlement) context['q'] = q context['document_type'] = type context['income'] = … -
Django class based views access passed arguments
How can I log / print passed values to class based view? Here is my class class ProjectCreateView(CreateView): model = Project form_class = ProjectForm I have tried appending the following to the class but I'm not seeing anything printed in the console. def get(self, request, *args, **kwargs): logging.info(request['name']) I can't figure out what I'm doing wrong here -
How do I know which is object is protected in Django?
I have the following code to get a list of related objects to "myobj", how do I know each object is protected? links = [rel.get_accessor_name() for rel in myobj._meta.get_all_related_objects()] obj_list = [] for link in links: objects = [] try: objects = getattr(myobj, link).all() except: pass for object in objects: obj_list.append(object) -
Django - ManagementForm data is missing or has been tampered with
I've been racking my brain over this problem for the past few days and I've read numerous other questions regarding the same error but they all seem to be different cases (not including management form, forgetting to update TOTAL_FORMS, etc etc) and do not resolve my problem. I have a page which could contain multiple formsets in a single HTML form. When I am posting the data back to the server, it fails on the is_valid() check for the formsets with the error in the title. I am new to web development and Django so please forgive me if I made a silly mistake or am taking an approach that will not work. def purchase(request): return generic_form_view(request, "inventory_tracking/add_purchases.html", "Successfully added purchases for %s.", PurchaseForm, [formset_factory(PurchaseForm.LiquorForm), formset_factory(PurchaseForm.NonLiquorForm)]) def generic_form_view(request, template, success_message, ParentForm, FormSets): if request.method == 'POST': request_params = copy(request.POST) parent_form = ParentForm(request_params) formsets = list(map(lambda form_set: form_set(request_params), FormSets)) if parent_form.is_valid(): # This works. for formset in formsets: if formset.is_valid(): # Fails here. Here is a snippet from my template: <form action="{% block form_action %}{% endblock %}" method="post"> {% csrf_token %} <div class="row"> <div class="row"> <div class=" well well-lg"> <div class="row"> {{ parent_form.management_form }} {% for field in parent_form %} <div … -
Django check if file in form exist on updateView
I know that there is a way to detect if a file exist with something like this {% if user.profilePic %} <p>it exist</p> {% endif %} but i want to detect if that file exist when im updating a form with UpdateView class. My objective is to create a different code with jasny file upload, so the user can see the image if that image exist. -
Is there a way to check if a string is a valid filter for a django queryset?
I'm trying to add some functionality to give a user the ability to filter a paginated queryset in Django via URL get parameters, and have got this successfully working: for f in self.request.GET.getlist('f'): try: k,v = f.split(':', 1) queryset = queryset.filter(**{k:v}) except: pass However, I am hoping to do so in a way that doesn't use try/except blocks. Is there a standard way in django to check if a string is a valid filter parameter? For example something like: my_str = "bad_string_not_in_database" if some_queryset.is_valid_filter_string(my_str): some_queryset.filter(**{my_str:100}) -
Which Python version used in Django project? 2 or 3?
When I open someone's Django project, I need a time for understanding which Python version is used here. I try to find print or print() methods, but some Django projects contain both print methods. How I can at once know, which version of Python is used in the Django project? -
chunked file uploads in django
I need to get chunked file uploads working in my django app (with nginx/uwsgi in front on multiple load-balanced EC2 instances with no shared storage). It seems like a combination of https://github.com/juliomalegria/django-chunked-upload and https://github.com/blueimp/jQuery-File-Upload would be ideal, but this django package seems to want to use a model/db to store the file objects and my application does not use models or a db at all (and needs to stay that way). What is a good alternative to get chunked file uploads working? -
Serialize Haystack SearchQuerySet
I have some Django queries dumped in files that are delayed so I pass as parameter sql_with_params to later execute in the delayed a raw query. I have migrated all queries to haystack so I wan't to do the same with SearchQuerySet. Is there any way to get the raw_query of an already constructed SearchQuerySet? PD: I am using ElasticSearch