Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Registration Redux Login
I am using Django-registration-redux and its templates for my website authentication.By default, users should Login to my website by providing username and password. In my case, I want users to Login with their Emails instead of usernames. But While registering, users should provide usernames, so I don't want to change the default registration form. I have seen previous StackOverflow questions, which manipulate registration forms by using RegistrationView class. I don't want to change registration form. How to do this? -
Error while calling python subprocess in docker container
I'm trying to call a subprocess in python 2.7. I'm using django in a docker container. I'm calling a function: def call_exec(lang) curdir = curdir = 'ht/exec_folder' tmp_files_dir = 'ht/temp_files' script_args = ["java","-jar",'/'+curdir + "/executable.jar", "-l",lang,"-s",'/'+tmp_files_dir] output = subprocess.check_output(script_args) return output Here, ht is a folder inside my Django app. I'm trying to use executable.jar and read the output. The other arguments are meant for running the executable. The following is the error produced: django_1 | similarity = call_exec('english') django_1 | File "/app/langswipe/submissions/check_view.py", line 80, in call_exec django_1 | output = subprocess.check_output(script_args) django_1 | File "/usr/local/lib/python2.7/subprocess.py", line 567, in check_output django_1 | process = Popen(stdout=PIPE, *popenargs, **kwargs) django_1 | File "/usr/local/lib/python2.7/subprocess.py", line 711, in __init__ django_1 | errread, errwrite) django_1 | File "/usr/local/lib/python2.7/subprocess.py", line 1343, in _execute_child django_1 | raise child_exception django_1 | OSError: [Errno 2] No such file or directory -
Django jQuery File Upload files not being posted
I'm trying to implement jQuery File Upload with Django 1.10. I read through the wiki and other attempts but I want to use a different approach than the ones listed in the wiki so I cannot reuse their implementation. My setup is they select the file(s), fill in some other data, then submit the form. The post data goes to a view that handles chunked uploads and creates a tracker object. The files are being recognized when I add them (being added to the table showing files), but they are not making it to the view. The html/view are linked but when I check the post data in the view, the file list is empty. HTML: <form id="fileupload" action="." method="POST" enctype="multipart/form-data">{% csrf_token %} <!-- Redirect browsers with JavaScript disabled to the origin page --> <noscript><input type="hidden" name="redirect" value="/no_javascript/"></noscript> <div class="row"> <input type="file" id="files_picker" name="files[]" multiple class="input_hidden"> <div class="btn btn-success" onclick="clickAdd()"> <span>Add Files</span> </div> <!-- The loading indicator is shown during file processing --> <span class="fileupload-loading"></span> </div> <!-- The global progress state --> <div class="fileupload-progress fade"> <!-- The global progress bar --> <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100"> <div class="progress-bar progress-bar-success" style="width:0;"></div> </div> <!-- The extended global progress state --> … -
Get scrapy settings in django application (celery tasks included) to execute pipeline
Identical question: Getting scrapy project settings when script is outside of root directory I had the same problem statement as this one, earlier! Scrapy spider is running perfectly using scrapy crawl <spider-name>. I need to move that scrapy project to Django code base. But as it was outside of the Django project root directory (at the same level); I was unable to import Django model to items.py itself. So, I moved the scrapy project, again, inside Django project. I've defined custom pipeline to save scraped data to Django database (using DjangoItem). Problem Statement: I want to call scrapy spider with dynamic URL from tasks.py to save it to DB using DjangoItem! How can I get existing settings of scrapy in django application to run CrawlerProcess and to access pipeline again? I ended up with this error when I called emails_spider() from tasks.py: [2017-02-02 23:48:07,699: CRITICAL/PoolWorker-1] Unhandled error in Deferred: [2017-02-02 23:48:07,700: WARNING/PoolWorker-1] 2017-02-02 19:48:07 [twisted] CRITICAL: Unhandled error in Deferred: [2017-02-02 23:48:07,700: CRITICAL/PoolWorker-1] Traceback (most recent call last): File "/home/msn/Documents/Works/project/venv/lib/python3.5/site-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks result = g.send(result) ... ... File "/home/msn/Documents/Works/project/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line … -
Always check if user is logged in
Right now, I am using the code below to check if user is not logged in. If not he is redirected to the login page. from .account_controller import logged_in from django.http import HttpResponseRedirect # Check if not logged in. def some_view(request): if not logged_in(request): return HttpResponseRedirect('/login/') What I want is to not include this code every time I am going to make a new method. How can I make it view-wide, so that every time I create a new method, it checks if the user is logged in? All answers are appreciated, thanks! -
How do I login an existing user with Django's authentication system?
I'm using Django's authentication feature and I am having difficulty logging in a user that already exists in the database. I have no problem logging in a user that I have just created: def sign_in(request): #we need to handle all the data that was just typed, we'll add a condition for that form = NameForm(request.POST) if form.is_valid(): post = form.save() post.save() username = request.POST.get(post.question_text) password = request.POST.get(post.id_text) user = User.objects.create_user('charlie', 'charlesdsmith25@gmail.com', 'smith') user = authenticate(username=username, password=password) if auth is not None: login(request,user) return HttpResponse('hi') else: return HttpResponse('bye') else: form = NameForm() return render(request, 'checkin/sign_in_new.html', {'form': form}) -
Why is it good practice to use namespaced urls in Django?
I have followed a number of tutorials and read the following link from the Django docs. It is supposed to be good practice to use namespaced url's (e.g. 'polls:index') instead of hardcoding them. I cannot fully understand why this is important, apart from convenience. Why exactly is this good practice and what problems could it help avoid? https://docs.djangoproject.com/en/1.10/topics/http/urls/#url-namespaces -
How to pass a kwarg/arg from auth_views.login to a url parameter of a different view in django
I am currently using the django auth_views.login to log my users into their accounts. I need to pass a user's attributes such as pk/username/etc. to my user profile view. my profile url is: url(r'^(?P<pk>[\w.@+-]+)$', ProfileDashboardView , name='dashboard') Once the user is logged in, how would I get their user attributes such as pk/id/username/etc. to pass into the url? I am using TDD to test before actually moving forward with my view. This is my view so far: @login_required def ProfileDashboardView(request, name=None): usr_name = IbkUser.objects.get(name=name) return render(request, 'profiles/profile_dashboard.html') I am using an AbstractBaseUser model for my user accounts that extends the USER model and uses the email as the username for login purposes. I am also using AuthenticationForm for the login form and crispy forms for styling. my form: class LoginAuthenticationForm(AuthenticationForm): """ A user login form for registered users. """ username = forms.EmailField(label='Email', required=True, widget=forms.TextInput(attrs={'id': 'login_username'})) password = forms.CharField(label='Password', required=True,widget=forms.PasswordInput(attrs={'id': 'login_password'})) def __init__(self, *args, **kwargs): super(LoginAuthenticationForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_id = 'loginForm' self.helper.form_method = 'post' self.helper.layout = Layout( PrependedText('username', "<span class='glyphicon glyphicon-envelope'></span>", active=True), PrependedText('password', "<span class='glyphicon glyphicon-lock'></span>", active=True), FormActions( Submit('submit', 'Submit', css_class ='btn btn-success btn-lg btn-block'), ), ) and this is my login template: {% extends "auths/base.html" %} {%load … -
Django How do I annotate the total number of instances of a keyword over multiple models AFTER filtering?
Okay, so I have a 'Keyword' model that is storing my keywords for my 4 other models, each of which has a 'key_list' field which is a ManyToManyField pointing back to my 'Keyword' model. Each of my models have multiple keywords, and I am searching through them and finding them successfully like so: keys_selected='term1$term2$term3$' keys_selected = keys_selected.rstrip('$') keys_selected = keys_selected.split('$') goal = len(keys_selected) remaining = list() remaining += Keyword.objects.filter(employee__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct() remaining += Keyword.objects.filter(vendor__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct() remaining += Keyword.objects.filter(application__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct() remaining += Keyword.objects.filter(machine__key_list__key__in=keys_selected).annotate(num_keys=Count('key')).filter(num_keys=goal).distinct() key_list = list() for x in remaining: if x not in key_list: key_list.append(x) This returns a dictionary of all of the keywords that are assigned to all of the entries in my 4 models that contain my chosen terms. The idea here is to create a filter that visually displays the word frequency of all keywords in the key_list of the objects that match my search query. I want it to append that to the dictionary being output to context so that I can then call the value and use it for the font size like so: {% for key in key_list %} <a href="{% url 'keysearch:index' %}?keys_selected={{ key }}${{ keys_selected }}" style="font-size: {{ key.num_keys }}px;">({{ key }})</a> {% endfor %} In … -
ImportError: No module named custom storages - django-storages boto
I am trying to follow this tutorial for using s3 but pretty much until the last step, somehow I get this error and I am not sure where I should import my own customed module Tutorial link https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/ Everything is fine, that I am able to upload / copy / use the static files using s3 then the step about creating custom storage for media usage # custom_storages.py from django.conf import settings from storages.backends.s3boto import S3BotoStorage class StaticStorage(S3BotoStorage): location = settings.STATICFILES_LOCATION I created that .py file inside the same directory as the setting.py (setting.py where it included INSTALLED_APPS and more) then inside the setting I added below as mentioned on the tutorial STATICFILES_LOCATION = 'static' STATICFILES_STORAGE = 'custom_storages.StaticStorage' STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION) then I ran python manage.py collectstatic I get this error File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) ImportError: No module named custom_storages Can someone give me a hand? Thanks in advance. -
Large Uploads in Django
I am working on a cookiecutter-django project that requires users to be able to upload files in the .5-1gb size range. From what I understand when uploading files that are larger then 2.5Mbs django uses the TemporaryFileUploadHandler to first upload the file to the tmp directory and then to then transfer them to the media directory. However when I try to upload a larger file my site just hangs, I don't see any files come into the tmp directory, and I see no errors in the logs. Is there some setting I need to turn on to instruct django to use the TemporaryFileUploadHandler? Or is there some way to possibly figure out where the site might be getting hung up? I am currently building out this project locally so I have full control over my environment. I am using Python 3.4.5 and django 1.10 -
Invalid block tag
How can I declare a new variable using jinja? The second line in the following code block result to an error: {% set msg_class = "" %} Error message: Invalid block tag on line 13: 'set', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag? Rest of the code: {% if msg %} {% set msg_class = "" %} {% if status == 1 %} {% set msg_class = "alert alert-success" %} {% elif status == 3 %} {% set msg_class = "alert alert-danger" %} {% elif status == 4 %} {% set msg_class = "alert alert-warning" %} {% else %} {% set status = 2 %} {% set msg_class = "alert alert-info" %} {% endif %} {% endif %} Using an array like in the following thread that I found, seems really ugly to me. Is it the only solution? Can a Jinja variable's scope extend beyond in an inner block? -
Secure authentication between ReactJS and Django
Been reading and watching quite a bit, and asking a lot of questions regarding ReactJS and Django. This particularly helped me to understand the the flow of data from Django REST Framework to ReactJS and from ReactJS to Django REST Framework. Django Forms and Authentication with Front-end Framework (AngularJS/ReactJS) However, the one thing I am trying to understand is authentication to the Django REST Framework. I understand from the documentation that it has built in authentication. Since this is sensitive data, I would obviously want it protected people retrieving it just by going to http://www.my_site.com/info/api. I would need to setup ReactJS to be the only thing that can request data from the API whether that is through a key or username/password credentials. I am just curious how this is handled? Obviously I don't want that hard coded in ReactJS because it will compile with the rest of ReactJS. -
Django templates not updating
I'm running a small django website with wsgi and apache. I made a minor change to one of my template files in my Django app, and the change is not showing up on my server. However, if I run python manage.py runserver, and curl -l 127.0.0.1:8000, I can see the change. I've tried restarting apache, with no luck. My urls.py for the page is just url(r'^page/', TemplateView.as_view(template_name="page.html")), I've verified that the change is not present by checking on other devices and using incognito mode. -
Quiz app with multichoice on wagtail
I need to create Quiz App with multicoice. Here is Django App https://github.com/tomwalker/django_quiz (demo: http://www.revisemrcp.com/ ) How can I create that with Wagtail? I need to save results for every user and show progress. Show me right way, please. -
Django/Nginx Is there a way to get the original relative url of a browser request?
If I have a django app running behind nginx, and the current address is http://www.foo.com/cat/dog/content.html with a web page that contains html like so, <img src="../../../../pic.png">, the browser will make a request to http://www.foo.com/pic.png, but is there any way to get the original relative url ../../../../pic.png, or the number of ../s server side, either in a django view or nginx? Thanks -
Django URL mapping
Im trying to link a button in HTML to another html in my project folder for django. Lets say its MyApp -Polls -templates -index -Votes -templates -main -truths -rigged I have a a button on main that uses rigged and truths so in main it has this button <form action="{% url 'Votes:rigged' %}"> <input type="submit" value="rigged votes" /> </form> now i want to add another button that would link polls->index into it. Is there a way to do that without copying everything from Polls into the folder Votes? -
Serializer with foreign key - GET and POST
I currently have a model serializer with a foreign key field. I would like the related model to a serialized object rather than just an ID. I know this is possible by creating a serializer for the related model and doing related_field = RelatedFieldSerializer() However, how do I handle the case when creating/updating occurs for the main object? E.g. I want to create an instance of the main object but the related field will get sent as an object (not a pk) and won't refer to the existing foreign key, it will try to create a new object instead. Hope this makes sense -
cPanel "Python Setup App" Django Install
I went with a hosting provider who said they support django installs with cPanel but they do not know how to set it up and nor do I. I should note that I do not have root access as it is shared hosting. They are using cPanel 60.0.36 and have an option called "Setup Python App". I am using Django 1.10.2 with MySQL client 1.3.8 and site-package called MySQLdb. Based on what I can see here, this looks totally possible. Could anyone help me without to setup a django app using this feature and migrating an existing django application over to cPanel? My Django app is very basic website right now with a structure like: Root_Folder -manage.py ->Main_project -- __init.py -- settings.py -- urls.py -- wsgi.py ->app_name -- admin.py -- apps.py -- models.py -- tests.py -- urls.py -- views.py If you need more information let me know and I will update the post. Any help is appreciated! Thanks, JAC -
Python Django "Killed"
New commit, works locally but not in production. The messages are strange. With uwsgi everything starts as usual. But on the first hit I send the server, everything freezes (I mean everything, even the SSH session stops responding). Eventually one of the workers die and the server starts responding again. With ./manage.py runserver this happens: $ ./manage.py runserver Performing system checks... Killed That's it. The only line in the commit is at the beginning of views.py import my_module Again, this works locally but not in production. I can't give you too much detail about my_module, but it's basically a very long list inside a class with a method that does a binary search. My suspicion is that it has to do with how big the thing is. The my_module.py file is 62MB. How can I find out what's happening? -
Scrapy item pipeline not called from spider with custom_settings and ITEM_PIPELINE
I have a scrapy project with a spider defined like: class Dogs(Spider): name = 'Dogs' start_urls = [url.Dogurl for url in Dog.objects.filter(Dogid=28247)] custom_settings = { 'ITEM_PIPELINES': { 'craw.pipelines.DogPipeline': 300, } } def parse(self, response): text = gunzip(response.body) parsed_json = json.loads(text.decode('utf-8')) for p in parsed_json['dogs']: prop_item = DogItem() for k, v in p.items(): if k not in ('vet', 'photos'): prop_item[k] = v yield prop_item and a pipeline like: class DogPipeline(object): def __init__(self): pass def process_item(self, item, spider): return item but at runtime, the spider runs, but the items never make it to the item pipeline, despite scrapy reporting that it's enabled: 2017-02-02 21:26:58 [scrapy.middleware] INFO: Enabled item pipelines:['craw.pipelines.DogPipeline'] How can I figure out where the connection is going awry? -
Run aggregate function on foreign key table
models.py class Contest(models.Model): sport = models.CharField(max_length=200) class Player(models.Model): name = models.CharField(max_length=200) contest = models.ManyToManyField(Contest) class Entries(models.Model): profit = models.FloatField() player = models.ForeignKey('Player',on_delete=models.CASCADE) How can I do a lookup to find a player based on a sport and then run the aggregate function on the profit entry in the Entries table. This was the type of approach I wanted to take: from .models import * from django.db.models import Avg player = Player.objects.filter(name='Player A').filter(contest__sport='nba').aggregate(Avg(entries__num_entries)) -
Convert Pandas DataFrame to a list suited for django templates
How would you convert the pd.DataFrame obtained after group by field1 and field2 field3 \ field1 field2 240502 000361 value1 005936 value2 006984 value3 field4 field1 field2 240502 000361 valuef1 005936 valuef2 006984 valuef3 to JSON [{ "field4": "valuef1", "field3": "value1" }, { "field4": "valuef2", "field3": "value2" }, { "field4": "valuef3", "field3": "value3" }] and then to [{ u'field4': u'valuef1', u'field3': u'value1' }, { u'field4': u'valuef2', u'field3': u'value2' }, { u'field4': u'valuef3', u'field3': u'value3' }] I've come up with json.loads(stats.to_json(orient='records')) But it seems too much. I need to consume it in the django templates. -
Chartit Module how to get the foreign key
I want to make a chart using the Polls example used on Django Doc. My problem is that I want to do it with each question. As of now I can show a pie chart with one question, but I realize that I am using one question.choice and the ids of the choices. But if I wan to use one question I cannot us id as one my arguments because it is going to get the question.id, not the choice.id. It is not easy for me to explain as clear. Here is my code def vote_results(request): #datapool with the data we want to retrieve votedata = DataPool(series=[{'options': { 'source': Choice.objects.all()}, 'terms': [ 'id', 'votes']} ]) #Create chart object cht = Chart( datasource=votedata, series_options = [{'options':{ 'type': 'pie', 'stacking': False}, 'terms':{ 'id': [ 'votes'] }}], chart_options = {'title': { 'text': 'What team will win NBA champ?' }}, x_sortf_mapf_mts = (None, monthname, False)) #Send the chart object to the template return render_to_response('polls/votes.html', {'votesdata': cht}) My question is how do I use the Question.objects.all() to get the question, but also use the Choice for each question, so that when a user vote for a question, he can see the results for that question's … -
Django form error handling architecture
In the clean method of my form class I am working with many different inputs from the billing, contact, and account sections of the form. As such there are many self.add_error statements and many fields that depend on other fields in order to validate. I have noticed that after adding an error for a field I am unable to access that field any more. This is strange as you can add more than one error to a field, but that is not the issue. I am seeing this method grow more complicated and unreadable, is there a good way to do this so the person that comes after me will understand it? I don't feel that the code ordering to prevent access after error is appropriate. My only thought is to set error variables in clean and call a different method at the end to add the errors to the fields. Thanks