Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Url Error: Unknown Specifier ?P\d
My urls.py file is this: urlpatterns = [ url(r'^chat/(?P\d+)/$', views.chatindex_view, name='chatindex_view'), ] and I'm getting this error while executing the runserver command: in regex (regex, six.text_type(e)) django.core.exceptions.ImproperlyConfigured: "^chat/(?P\d+)/$" is not a valid regular expression: unknown specifier: ?P\d -
django: extending allauth.account
allauth.account comes with a signup.html {% extends "account/base.html" %} {% load i18n %} {% block head_title %}{% trans "Signup" %}{% endblock %} {% block content %} <h1>{% trans "Sign Up" %}</h1> <p>{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}</p> <form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}"> {% csrf_token %} {{ form.as_p }} {% if redirect_field_value %} <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" /> {% endif %} <button type="submit">{% trans "Sign Up" %} &raquo;</button> </form> {% endblock %} i would like to override it instead of using the form what do i need to post to the url account_signup? thank you -
Django startswith vs endswith performance on MySQL
Lets say I have the following model class Person(models.Model): name = models.CharField(max_length=20, primary_key=True) So I would have objects in the database like Person.objects.create(name='alex white') Person.objects.create(name='alex chen') Person.objects.create(name='tony white') I could then subsequently query for all users whose first name is alex or last name is white by doing the following all_alex = Person.objects.filter(name__startswith='alex') all_white = Person.objects.filter(name__endswith='white') I do not know how Django implements this under the hood, but I am going to guess it is with a SQL LIKE 'alex%' or LIKE '%white' However, since according to MySQL index documentation, since the primary key index can only be used (e.g. as opposed to a full table scan) if % appears on the end of the LIKE query. Does that mean that, as the database grows, startswith will be viable - whereas endswith will not be since it will resort to full table scans? Am I correct or did I go wrong somewhere? Keep in mind these are not facts but just my deductions that I made from general assumptions - hence why I am asking for confirmation. -
Django 1.10 not showing user uploaded media files in templates
I have created a django 1.10 project using python3.5 where users can upload a profile picture. My problem is I can't get the profile pic to display in my html templates. I have read several questions on SO, but still no luck. So far I have: -Set up MEDIA_URL, MEDIA_ROOT in my settings.py -Added django.template.context_processors.media to context_processors inside TEMPLATES in settings.py -I've added urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in what I believe is my base urls.py. verified the uploaded image is in the correct directory Printed <p>{{MEDIA_URL}}{{profile.pic}}</p> inside my template to ensure it points to the directory where the image is yet still <img src={{ MEDIA_URL }}{{profile.pic}} is not displaying what I want, and neither is {{ profile.pic.url }}. -Project structure: -class_site -class (name of project from django-admin.py startproject class) -class *settings.py *urls.py (what i'm assuming is base urls.py -profiles *views.py -templates *profile_detailview.html -static -media -pics *mypic.jpg settings.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ .... 'django.template.context_processors.media', ], }, }, ] STATIC_URL = '/static/' #this is added and it allows you to set up your static directories if DEBUG: MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media") MEDIA_URL = '/static/media/' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static- only") STATICFILES_DIRS … -
Django upload multiple column data to postgresql
I'm now working on a little project which is uploading data to postgresql, and then show the data through web server. Now I've finished the work and successfully worked with small files. However, when I upload a data(csv, about 1~2Mb) with many columns(about 190 columns * 3 rows) in it, the web server got stucked AFTER uploading the file. It's not the upload problem, it's the problem of writing the data into the postgresql. I've tried to upload only the "column name" part(the first row), and it worked. But if I add a row with data(most of the value is NULL), it stucked. The following is my code: def csvwritein(doc):# doc is a django model contains the information of the uploading file doc = doc conn = psycopg2.connect("dbname='apidb' user='api' host='localhost' password='eric40502' port='5432'") readcur = conn.cursor() readcur.execute("select exists(select * from information_schema.tables where table_name='%s')" % doc.tablename) # check if same relation is already in database check = readcur.fetchone()[0]# check is used to check if same relation exists print(check) try:# change encoding type according to file type fr = open(doc.path,encoding = 'utf-8-sig') dr(fr,doc,check)# pass writing database part to another func fr.close() except Exception as e: fr = open(doc.path,encoding = 'big5') dr(fr,doc,check)# pass writing … -
Django "lazy query execution" principle
What would be the main problem if Django did not use "lazy query execution" principle? q = Entry.objects.filter(headline__startswith="What") q = q.filter(pub_date__lte=datetime.date.today()) q = q.exclude(body_text__icontains="food") print(q) while print(q) executes then it hit the database. -
Logging a snapshot of online users for Django website (postgresql backend, nginx webserver)
In a Django + postgresql website of mine, I need to publicly show all is online at a point in time (it's a social website). How do I do this? For instance, can there be a way to enumerate all logged in users who hit my nginx webserver in the previous 10 mins? Something like that could work. I'm a beginner and fishing for a viable solution at the moment. Currently to accomplish this, I store sessions to the database, using an external library to make sessions enumeratable. This allows me to query how many unique users are online at a point in time. But this scheme creates a lot of needless DB traffic. As a result, logging and pruning logs has become ineffective. Moreover pgFouine shows me that session related DB calls are the biggest performance bottleneck my website currently has. There's a proposed solution here, but it uses the database. -
Django doesn't connect to Mysql server
I am trying to run the command "python manage.py migrate". I am getting error "_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")" I am able to connect with MySQLWorkbench. I have tried connecting with AWS RDS but I am facing the same error. Below is the database configuration part in settings.py file of my Django server. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'tzone', 'USER': 'root', 'PASSOWRD': 'Revanth_01', 'HOST' : 'localhost', 'PORT' : '3306', } } My guess is Django isn't using the password to login.(Have tried removing password and changing it to wrong one, still facing the same issue). Is there a way to force Django to use Password. -
Django Forms/Formset - How do I save data to a separate model using the cleaned data of the formset I am working with?
In my code, I am using an InlineFormSet to pass in multiple Slot models associated with the same Node model. When I add or change a Card model in the SlotInlineFormSet, I want the system to automatically add the specified number of ports to the Port table. To clarify, the model relationships exist as Node -> Slot -> Card -> Port. To do what I want to, I have to grab some information from the Port model, then save data to a Port model instance, all when I send the Submit request, per form in formset. How do I save data to a separate model using the cleaned data of my original form? Code: #forms.py class SlotInlineFormSet(BaseInlineFormSet): def clean(self): super(SlotInlineFormSet, self).clean() y = self.cleaned_data print(y) # Access each form one at a time for idx, form in enumerate(self.forms): form_data = form.cleaned_data print('idx: ' + str(idx+1)) #Pull Necessary Port Variables port_type = (form_data['installed_card'].default_port_type) max_ports = (form_data['installed_card'].max_port_num) port_start_num = (form_data['installed_card'].port_start_num) port_naming = (form_data['installed_card'].port_naming) #Save Ports to Port model #??? #views.py def manage_slots(request, node_id): node = Node.objects.get(pk=node_id) max_slots = node.chassis.num_slots SlotFormSet = inlineformset_factory(Node, Slot, max_num=max_slots, extra = max_slots-node.slot_set.count(), can_order = False, fields=('installed_card', ), can_delete=False, formset = SlotInlineFormSet) if request.method == "POST": formset = … -
Override project settings from an app?
How could I override or change the project's settings from an app? For example, I have a context processor function in my app, and I want to include it into a context_processors option in a TEMPLATE settings. How could I do that without changing project's settings.py file directly? In the documentation i found, that they cannot be changed at runtime: You shouldn’t alter settings in your applications at runtime. For example, don’t do this in a view: from django.conf import settings settings.DEBUG = True # Don't do this! The only place you should assign to settings is in a settings file. So is it possible at all? -
Upload an image to twitter via tweepy from django form
I want to upload an image to twitter taken by a django form: <div class="panel panel-danger"> <div class="panel-heading"> <h3 class="panel-title">Panel title</h3> </div> <div class="panel-body"> <form action="" method="POST" role="form" enctype="multipart/form-data"> {% csrf_token %} <legend>Upload a file: </legend> <div class="form-group"> <input type="file" name="file" class="form-control" id=""> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> The image I got is: if fileForm.is_valid(): print "file is uploaded." paramFile = open(request.FILES['file'],'rb') # paramFile = request.FILES['file'].read() api.update_with_media(paramFile,status="Hello first image") The error I got is: coercing to Unicode: need string or buffer, InMemoryUploadedFile found How can I upload this to twitter? -
Visualizing with t-SNE barriers which are not exist in R
I did some research with R and tsne on a mixed data type dataset (factor and numeric). Now I'm researching the python capabilities for the same project with the aim of developing web application (django framework). Unfortunatelly I'm finding lot of barriers to this aim: 1. Gower distance in python with weighted variables 2. tsne ie. bh_tsne explained in Visualizing with t-SNEhttps://indico.io/blog/visualizing-with-t-sne/ do not have an option to change perplexity or theta easily. Further, I found somewhere that t-sne in sckit learn have performance problems and is not "series" solution for prodution 3. set.seed in R allows me to repeat the results. What is python alternative? What is your thoughts about the easiest way to develop powerful django + tsne web application? -
react routing and django url conflict
I am using reactjs as a frontend and django as backend. React router is used for routing. When i refresh the page that has routed by react router, i get django 404 Page Not Found error. If i refresh the homepage, i dont get any such error because the homepage is rendered by django template too using its url. Do i have to configure that in the webpack? My project structure is i have seperated django and reactjs. I have created a folder as frontend where reactjs file resides. my webpack.config file const path = require("path"); if(!process.env.NODE_ENV) { process.env.NODE_ENV = 'development'; } module.exports = { entry: [ './src/index.js' ], output: { path: path.join("../app/static/build/", "js"), filename: "app.js", publicPath: "../app/static/build/" }, devtoo: 'source-map', debug: true, module: { loaders: [{ exclude: /node_modules/, loader: 'babel', query: { presets: ['react', 'es2015', 'stage-1'] } }, {test: /\.(jpe?g|png|gif|svg)$/i, loader: "url-loader?name=images/[name].[ext]"}, ] }, resolve: { extensions: ['', '.js', '.jsx'] }, devServer: { historyApiFallback: true, contentBase: './' } }; -
Why can't I get the list of all users in a group in Django?
This is my view. def teacher_list(request): group = Group.objects.get(name='Teacher') users = group.user_set.all() return render(request, "acnt/teacher-list.html", { "users": users }) This is the template I am using. {% for teacher in users %} <a href="#">{{ teacher.get_full_name }}</a> <br/> {% endfor %} {{ users }} This is the result I am getting on my template. John Doe <QuerySet [<User: john>, <User: michel>]> As you can see I have updated the group by adding another user michel, and the users object has both the users, but it is not showing in the loop I am using in the template. Why is this happening? -
Getting numeric property.
According to this How to update a field type in elasticsearch, I have added Put Mapping AP. However Kibana, I get property as unknown (See screenshot below). In rank field, there's numeric values or 0. { "tweet" : { "properties" : { "user" : {"type" : "string", "index" : "not_analyzed"}, "message" : {"type" : "string", "null_value" : "na"}, "postDate" : {"type" : "date"}, "priority" : {"type" : "integer"}, "rank" : {"type" : "float"} } } } -
Check the size of "media" folder in Django
Is there any way to check the size of "media" folder in Django? I want the code to manage the size of the contents in order to remove the old objects to not exceed a limit of storage for the whole web. I have been searching for a while, but looks more complex than expected. Thanks! -
Django-Compressor throws UncompressableFileError with django-storages using amazonaws and heroku
I am having problem to locate the issue of the problem. I have settings folder and inside I have local.py, common.py and production.py. All is working great and it is compressing on localhost, but not on heroku. When I deploy it I am getting error: Internal Server Error: / UncompressableFileError at / 'https://xxxxx.s3.amazonaws.com/static/css/stylesheet.css' isn't accessible via COMPRESS_URL ('//xxxxx.s3.amazonaws.com/static/') and can't be compressed common.py # STATIC FILE CONFIGURATION # ------------------------------------------------------------------------------ # See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root STATIC_ROOT = str(ROOT_DIR('staticfiles')) # See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url STATIC_URL = '/static/' # See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS STATICFILES_DIRS = ( str(APPS_DIR.path('static')), ) # See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.CompressorFinder', ) # MEDIA CONFIGURATION # ------------------------------------------------------------------------------ # See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root MEDIA_ROOT = str(APPS_DIR('media')) # See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url MEDIA_URL = '/media/' COMPRESS_OFFLINE_CONTEXT = { 'STATIC_URL': STATIC_URL, 'MEDIA_URL': MEDIA_URL, } COMPRESS_ENABLED=True production.py DEFAULT_FILE_STORAGE = 'config.custom_storages.MediaStorage' THUMBNAIL_DEFAULT_STORAGE = DEFAULT_FILE_STORAGE AWS_ACCESS_KEY_ID = env('DJANGO_AWS_ACCESS_KEY_ID') AWS_SECRET_ACCESS_KEY = env('DJANGO_AWS_SECRET_ACCESS_KEY') AWS_STORAGE_BUCKET_NAME = env('DJANGO_AWS_STORAGE_BUCKET_NAME') AWS_S3_CUSTOM_DOMAIN = '{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME) AWS_AUTO_CREATE_BUCKET = True AWS_QUERYSTRING_AUTH = False AWS_S3_CALLING_FORMAT = OrdinaryCallingFormat() # AWS cache settings, don't change unless you know what you're doing: AWS_EXPIRY = 60 * 60 * 24 * 7 # TODO See: https://github.com/jschneier/django-storages/issues/47 # Revert the following and use str after the above-mentioned bug is fixed in # either django-storage-redux or boto AWS_HEADERS = { … -
iterating through list with floor loop jinja django python
I have this array: scores = [45.62, 51.87, 33.12, 39.37, 33.12] I want to iterate through the list, and pass each item to an html template. Using jinga, i tried the following: {% for items in scores %} {{ items }} <br> {% endfor %} I hoped that the above would print out each item in the list like so: 45.62 51.87 33.12 etc... but it didn't, it just prints the entire list, as a list, on one line. I also tried this: {% for items in scores %} {{ scores.0 }} <br> {% endfor %} This printed out just the first score of the list, but not the others. I want to print out each score individually. Please help! I'm using django 1.9. I know this is jinja, not sure if it's jinja2? -
Cannot connect to MySQL docker container from container with Django app
When I try to connect from a docker container running my Django app to a container running MySQL, I get the following error: django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '172.17.0.2' (111)") Here's how I'm running the MySQL container: $ docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=testdb -e MYSQL_ROOT_HOST=172.17.0.2 -d mysql/mysql-server:5.7 Here are my Django settings: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'testdb', 'USER': 'root', 'PASSWORD': 'root', 'HOST': '172.17.0.2', 'PORT': '', } } I've verified the MySQL container is using IP 172.17.0.2: $ docker inspect mysql |grep -i ipaddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", -
Unclear 404 get ajax error passing integer to django
Totally unclear 404 Ajax error. var int_page_number = 2; $.ajax({ type:'GET', url: '/loadmore/', data: { 'page_number' : int_page_number}, dataType: 'json', success: function (data) { alert(data); } }); In the place passing data, I tried both using apostrophe and not around page_number. It's 404 so error may be in frontedn, but anyways I attach django urls.py string just in case : url(r'^loadmore/(?P<page_number>[0-9]+)/$', views.loadmore), and views.py function, which is all right: @api_view(['GET', ]) def loadmore(request,page_number): answers_to_questions_objects = Question.objects.filter(whom=request.user.profile).filter(answered=True).order_by('-answered_date') paginator = Paginator(answers_to_questions_objects,10) current_page = (paginator.page_number) answers = serializers.serialize('json', current_page) data = { 'answers': answers } return Response(data)` -
Django deployment method
What is the correct way to deploy a Django project online? Currently, the method I use involves ssh-ing onto the server, git pull, performing Django chores (migrations, collectstatic etc) and then restarting the web server. There must be an automated way of doing this. E.g. If I wanted to deploy onto 10 servers, I don't want to manually update them all! I can't seem to find anything concrete about best practices. I have heard of Fabric but I don't think it is compatible with Python3 (which I am using). There seem to be many tools but I'm not sure which are reputable. Any advice would be helpful, thanks -
Data lost on form submit with md-chips in Django
I am writing an HTML form using angularjs and material design for my django application. Here is the html for my form: <form name="transactionForm" action="add/" method="post"> {% csrf_token %} <div layout="row"> <div ng-controller="CustomSeparatorCtrl as ctrl" class="chipsdemoCustomSeparatorKeys" ng-app="MyApp"> <md-chips ng-model="ctrl.tags" md-separator-keys="ctrl.keys"> <input type="text" name="comments" placeholder="Enter a tag"> </md-chips> </div> <!-- Add button --> <md-input-container> <md-button type="submit">Add</md-button> </md-input-container> </div> </form> When I click the "Add" button the content of chip input field is only preserved if the chip is not made. For example if I hit "Add" in this case my request.POST object contains the value "test". However if I hit "Add" in this case the request.POST object does not contain the value "test". How do I make sure that the value of the tag is included in both cases? Below is my javascript (I'm not sure if this is relevant): angular .module('BlankApp', ['ngMaterial', 'ngMessages']) .controller('CustomSeparatorCtrl', DemoCtrl); // Handles comments function DemoCtrl ($mdConstant) { // Use common key codes found in $mdConstant.KEY_CODE... this.keys = [$mdConstant.KEY_CODE.ENTER, $mdConstant.KEY_CODE.COMMA]; this.tags = []; } -
What is the right place to initialize third-party API in Django project for later Celery use
I have a twitter API used in my Django project. Right now I keep initialization in my settings.py and in tasks.py I just import API from django.conf.settings. I am not sure if it's a good practice as I've never seen it. Do I need to create API instance somewhere in celery.py or even in the task function? -
Permission denied on tables created by django migration postgreSQL
I'm developing a django project using a postgreSQL DB that is hosted on AWS. My problem is that I have created a user and given him all permissions on public scheme to use as django user, and after the migration if I try to access one of the tables created by the django migration (for example the django_migrations table) it shows the following error message: ERROR: permission denied for relation django_migrations Is there any way to escalate my privileges to have access to such tables? It will be more users that will have access to the DB, so using my user on django is not an option. -
Python Requests Performance is low
I am developing an RestAPI, and there is a part where I need to take a file from request, pass it to the Microsoft Cognitive Service and response back some information. Apart from the details of implementation, my problem is with the performance of the Python requests module. The total time of the response was around 12 seconds on average, so I dig a little bit of my code and found out that the requests module of Python is actually uploading the file at slow rate. And the interesting part is that file is not big at all. The size of the file that I use for the requests is exactly 201 Kb. Specifically talking about the details of the performance, post request takes around 6-7 seconds in general. Whereas, the same request-response time is 1.5 seconds on average, when I use Postman to make request. This is a huge gap, right? The request is also so simple, it is just passing the file with a required header. You can find the code below. def identification(identification_profile_ids, file, short_audio=True): headers = { 'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY, } url = _BASE_URI + '/spid/v1.0/identify?identificationProfileIds={}&shortAudio={}'.format(identification_profile_ids, short_audio) response = requests.post(url, data=file, headers=headers) return response Please do not …