Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Divide Django Apps by group permissions
I'm writing my first Django app and I am trying to find an easy way to limit access to certain pages based on groups. Here is my generic my planned group roles: Admin- add new content to the database Worker- edit content in database User- view content in database There will be a lot of separate pages for each role and I'm worried about keeping track of them if I limit access based on models/views. Is it a bad idea to divide the project up into apps for each user role and limit access based on the app? So, for example, the user could only access the pages in the user app, the worker could view content in the worker and user app, and the admin could view everything. -
How to pass extra keyword arguments when using Django RequestFactory?
I'm trying to write a simple unit test for a view but I'm having trouble passing extra keyword arguments to the view when I'm using RequestFactory to set up the request. To start, here's the urlpattern: # app/urls.py # Example URL: localhost:8000/run/user/1/foo urlpatterns = [ url(r'^user/(?P<uid>\d+)/(?P<uname>\w+)/$', views.user_kw, name='user-kw'), ] Here's the view I'm testing: # app/views.py def user_kw(request, *args, **kwargs): uid = kwargs['uid'] uname = kwargs['uname'] return render(request, 'run/user.html', context) Finally, here's the test: # app/tests.py def test_user_kw(self): factory = RequestFactory() # ??? request = factory.post('user/') response = views.user_kw(request) self.assertEqual(response.status_code, 200) As you might expect, when I run the test, I get this error: ====================================================================== ERROR: test_user_kw (run.tests.TestViews) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/jones/code/django/testing/run/tests.py", line 53, in test_user_kw response = views.user_kw(request, {"uid": 1, "uname": "foobar"}) File "/Users/jones/code/django/testing/run/views.py", line 28, in user_kw uid = kwargs['uid'] KeyError: 'uid' ---------------------------------------------------------------------- The Django documentation on the RequestFactory object doesn't discuss this situation. I looked at the RequestFactory code itself but I couldn't figure out how to set up the object to account for the two keyword arguments contained in the URL. I also couldn't find anything online addressing this situation. I should add that I did manage to write a test for … -
JS script in django html template
I want to have infinity scroll of my posts. Below are my .html file and script. (I have set up static etc correct). How can I place JS script between <div id="test"> in html? my django .html: {% block content %} <div id="test"> {% for post in posts %} {{post.title}} | {{post.text}} {% endfor %} </div> {% endblock %} JS code: $(window).scroll(function() { if($(window).scrollTop() == $(document).height() - $(window).height()) { $("#test") //HERE SUPPOSED TO BE MY CODE is #test ok? } }); So how can I place JS code? {% block content %} and {% endblock %} must be inside JS script. How to do this :( -
django-el-pagination Back Button issue
When I click on an detail page and then use the browser back button, Return to the first page of the list. (lazy pagination) How can I make context.url appear when I click the Back button on a detail page? -
With Django, not seeing request.session in Google Chrome Developer Tools?
I'm trying to follow https://docs.djangoproject.com/en/2.0/topics/http/sessions/#using-sessions-in-views to add session data to a request. I have subclass of Django's generic ListView in which the get method is overridden similar to the following: def get(self, request, *args, **kwargs): request.session['foo'] = 'bar' return super().get(request, *args, **kwargs) I would expect that after accessing the page and going to Application -> Session Data, I would see the session data. However, I don't see anything: Is saving session data supposed to work like this? The session engine is set to use signed cookies: -
Django order_by() returning wrong queryset
I am trying to get a list through a queryset that gives me an ordered list with fields across different models. I have two models: class Team(models.Model): total_score = models.PositiveSmallIntegerField(default=0) class Meta: ordering = ['-total_score'] class Individual(models.Model): team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='individuals') score = models.PositiveSmallIntegerField(default=0) class Meta: ordering = ['-score'] This is the queryset I am using: Team.objects.order_by('-total_score', '-individuals__score') From my understanding, this will order by descending total score, followed by descending individual scores. However, whenever I have teams with same total score and individuals in these teams with similar scores, this queryset does not compare each individual score across different teams. How do I fix this? I am trying to rank these teams and break ties based on the scores of individuals. I am using postgresql. -
Configuring celery with systemd in Ubuntu - module path failure
I use Ubuntu Server 10.08, and I want to use celery with systemd . I have the following error: ModuleNotFoundError: No module named 'ph' May 02 21:02:55 ph-1c1m1t sh[2646]: Traceback (most recent call last): I know that is a path error, but I tried multiple combinations regarding app, and I couldn't solve it. the project structure is: username -projects -src - ph - settings (folder) - celery.py -urls.py The conf file CELERYD_NODES="w1 w2" CELERY_BIN="/home/username/env/prod/bin/celery" CELERY_APP="ph.celery:app" CELERYD_MULTI="multi" CELERYD_OPTS="--time-limit=300 --concurrency=8" CELERYD_PID_FILE="/var/run/celery/%n.pid" CELERYD_LOG_FILE="/var/log/celery/%n%I.log" CELERYD_LOG_LEVEL="INFO" In systemd, celery.service I have: [Unit] Description=Celery Service After=network.target [Service] Type=forking User=username Group=username WorkingDirectory=/home/username/env/prod/ EnvironmentFile=-/etc/celery/celery.conf ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \ -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \ --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}' ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \ --pidfile=${CELERYD_PID_FILE}' ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \ -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \ --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}' [Install] WantedBy=multi-user.target -
add a javascript file to django admin
I seem to have hit a wall trying to do a very simple thing: Integrate a js file into a Django model admin. I am adding a Media class like so: class Media: css = { "all": ("job_run.css",) } js = ("job_run.js",) I put both files in the same folder, but only the css gets actually "published" by the server (I can only see the job_run.css included in the chrome devtools). This is probably a file (job_run.js) location problem, but I don't seem to be able to find the right folder to put it under my projects. A second possible error is that I should specify somewhere the Media folder for inclusion. Could not find a good reference how to do so. Any clues? -
Django all-auth custom login form security?
I am planning to use Django all_auth custom login/register for the authentication. I am able to customize the form successfully. I am referring to this answer on stackoverflow: Adding security questions to my Django site However, i cannot find a doc or information to add a security question and answer where i can hash the security answer? -
Django + postgreSQL. user specific table
I am currently working on a Django based service that will gather users' data from their Shopify shops and provide some insights. I use PostgreSQL as my DB server. The problem I am facing is whether it is more efficient to create a separate table for every particular user and store their data there or rather keep everything in one big table. Approximate data length - about 100k-1mln rows per user. Data is unrelated among users. Furthermore, I will need to store the user-specific results of my analysis. Any thoughts? -
Django inlineformset_factory errors returning a list of empty dicts
I'm trying to allow the ability to add/edit a paper object along with all of the articles associated with it on the same form. However, when I hit save, I'm getting an empty list of dictionaries when I call article_formset.errors while article_formset.is_valid() is True and my formset is not saving or updating any objects. ipdb> articles_formset.is_valid() True ipdb> articles_formset.errors [{}, {}, {}, {}] Models: class Paper(models.Model): ... class Article(models.Model): article_id = models.PositiveIntegerField() title = models.CharField(max_length=255, blank=True, null=True) content = models.TextField(blank=True, null=True) author = models.CharField(max_length=255, blank=True, null=True) article_type = models.CharField( max_length=255, default='regular', choices=( ('regular', 'Regular'), ('feature', 'Feature'), ), ) url = models.URLField(max_length=255, blank=True, null=True,) paper = models.ForeignKey( Paper, on_delete=models.CASCADE, related_name="articles", ) Views: def article(request, uuid=None): context = {} paper = None ArticleFormSet = inlineformset_factory( Paper, Article, fields=('article_id', 'title', 'content', 'author', 'article_type',), ) if uuid: paper = get_object_or_404(Paper, uuid=uuid) if request.method == 'GET': paper_form = PaperForm( instance=paper, user=request.user, ) article_formset = ArticleFormSet(instance=paper) elif request.POST: paper_form = PaperForm( data=request.POST, files=request.FILES, instance=paper or None, user=request.user, ) if paper_form.is_valid(): paper = paper_form.save() articles_formset = ArticleFormSet(request.POST, instance=paper) if articles_formset.is_valid(): articles_formset.save() if not uuid: uuid = article.uuid return HttpResponseRedirect( reverse('article:update', args=[uuid]) ) context['articles_formset'] = articles_formset context['form'] = paper_form return render( request, template_name='paper_form.html', context=context, ) HTML: <form class="form" … -
NoReverseMatch error in Django tutorial: argument not being picked up
I'm hitting the following error when going through the Django tutorial: Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['polls\\/(?P<question_id>[0-9]+)\\/$'] From this answer it seems like the problem is that the argument is not being passed through on line 9 of results.html. This answer suggests that it should be question.id but that's what I have. So I'm not sure where to go next on this. part following error. When I remove line 9 (which includes this link) from the template. Help appreciated. Apologies if this has already been asked before - I feel like I've looked everywhere. Hopefully this is all the relevant code. (After this went wrong a couple of times I copied and pasted everything from the tutorial to guard against typos.) Traceback Environment: Request Method: GET Request URL: http://127.0.0.1:8000/polls/1/results/ Django Version: 2.0.3 Python Version: 3.6.4 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls.apps.PollsConfig'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Template error: In template C:\Users\User\Dropbox\Personal\Projects\Python\Django\django-tut\mysite\polls\templates\polls\results.html, error at line 8 Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['polls\\/(?P<question_id>[0-9]+)\\/$'] 1 : <h1>{{ question.question_text }}</h1> 2 : 3 : <ul> 4 : {% for choice in question.choice_set.all %} 5 : … -
Django dynamic form for multiple choice selection
I have a Django app where I ask users to select amongst multiple choices of cities on an html template. The choices of cities are based on distinct instances of cities in the database table. Since the total number of cities is over 100, what I’d like to do instead is to give the user the option to select all cities (so they don’t have to manually select 100+ cities) or to select individual cities using a multiplechoicefield if they want some subset of the 100+ cities. How can I go about implementing this in Django? Here’s my code for what I am currently doing: forms.py class FilterCityForm(forms.Form): q = listing.objects.all().values('Address_locality').distinct().order_by('Address_locality') CITY_CHOICES = [[x['Address_locality'],x['Address_locality']] for x in q] city = forms.MultipleChoiceField(choices=CITY_CHOICES, required=True, widget=forms.SelectMultiple(attrs={'size':'30'})) views.py def main_select_cities(request): form = FilterCityForm(request.POST or None) city_list = dict() if form.is_valid(): city_list = form.cleaned_data.get("city") return process_cities(request, city_list) context = { 'form': form, 'city_list': city_list, } return render(request, "0_select_cities.html", context) The template {% block content %} <div class='container'> <div class='row'> <div class='col-sm-6'> <br><br> <h4>Select all the cities for which you would like to view listings</h4> <form method='POST' action=''> {% csrf_token %} {{form.as_p}} <input class='btn btn-primary btn-lg' type='submit' value='Show listings'/> </form> <br> <i> use ctrl or shift … -
Using Django allauth for multiple models by using social accounts
I am trying to create 2 different page, 1 for user and 1 for admin and for both these pages I am trying to implement only the social account authentication rather than letting user to fill up the form. Now the problem is that I am able to use Google authentication with 1 click on button but the data is getting saved inside intro_user table. But in reality I want to save these details based on which page I am clicking the social account button. For example: If I am in User page and user clicks on Google button then he will be authenticated and redirect to user profile where he will fill some forms. If user is on admin page then after authentication he will be redirect to the admin page and fill form over there. In both cases I am trying to save data with 1 extra field showing which type of data it is, whether a user or an admin. I tried extending the model and creating 2 separate model and then creating few forms. Till form it is working fine but for social authentication I am not sure how to do: 1. Redirect 2. Saving data … -
Render utf-8 as a tag in Django HTML
Question: Is is possible to decode a tag in Django HTML? Context: I'm having trouble rendering utf-8 with a Django tag and I am not sure what would be the best method of rendering it. Here is my problem, I get the data as follows: b'Line1\r\nLine2\r\nLine3'. Which is fine because once I get it and decode it, it displays properly in the command prompt. Now this is not the case when it renders it as a html page since it treats it as a string and it is all displayed in 1 line vice 3. Does anyone know what would be the best way of having handling this? -
Duplicate rows are created after bulk create with SQL Server
I'm doing a bulk create to dump a lot of information into a database at once. The information is layered - I create the top layer, then use the data as relationships for the next layer, etc. I'm unable to use PostgreSQL, so I can't take advantage of its ability to return primary keys when doing a bulk create. Instead, I create a UUID and stick it in with each layer, then use it to grab the rows I've just made so that I can use them for the next layer. A dumbed-down model looks like this: class Container(Component): parent = models.ForeignKey('self', related_name='children', null=True, blank=True, on_delete=models.CASCADE) bulk_insert_id = models.UUIDField(default=uuid.uuid4) and the bulk create/query for what I just created looks like this: bulk_insert_id = uuid.uuid4() for requirement in self.children: r = models.Container( parent=self.db, bulk_insert_id=bulk_insert_id, ) models.Container.objects.bulk_create(requirements) requirements = models.Container.objects.filter(bulk_insert_id=bulk_insert_id) The problem I'm experiencing is that when I go to actually use these elements, I'm finding duplicates of them. It's as if a copy is inserted by the bulk create, and then another copy is created when I access/save data to it for the first time. Here's an example screenshot from SSMS: As you can see from the image above, the last … -
How to get date from this format "Tue May 15 2018 00:00:00 GMT-0400 (EDT)" in python
How can i convert date from this formt "Tue May 15 2018 00:00:00 GMT-0400 (EDT)" to date in the format yyyy-mm-dd? -
Trouble for rendering a simple checkbox in Django
I try to render a simple checkbox (a boolean checkbox) in Django. form.py class Form(forms.Form): state = forms.BooleanField() html <input type="checkbox"> <label>{{ form }}</label> </input> It display the checkbox correctly but it's impossible for me to check the box... -
Django & Pandas: Get Choice Value in dataframe instead of Choice label
Again with a question regarding django... and pandas. I'm trying to get the value of two 'choices' column into a pandas dataframe; however, I'm only getting the label of the choice. Example code: my_app/models.py def Entry(models.Model): STATUS_ONE = 1 STATUS_TWO = 2 STATUS_CHOICES = ( (STATUS_ONE, 'Status one'), (STATUS_TWO, 'Status two') ) status = models.IntegerField(choices=STATUS_CHOICES, default=STATUS_ONE) status_date = models.DateTimeField(auto_now=True) contents = models.TextField() The data I'm getting: from django_pandas import read_frame from my_app.models import Entry qs = Entry.objects.all() df = read_frame(qs) df # status status_date contents # Status one 2018-04-30 20:00:00+00:00 Test contents # Status two 2018-04-30 21:00:00+00:00 More contents What I'd like to get is: # status status_date contents # 1 2018-04-30 20:00:00+00:00 Test contents # 2 2018-04-30 21:00:00+00:00 More contents Is there a way to get it? -
Django iterate through mixed array
I've got a function which returns an array consisting of a object and four strings like this: [obj1, 'date1', 'time1', 'title1', 'description1'] I give this to my template where I want to display some variables from the obj (for example obj1.name and obj1.surname) as well as the strings. Right now i'm using {% for obj in entries %} {{obj}} </br> {% endfor %} This displays the value of def __str__(self): return self.name of the obj and the all of the strings but i cant figure out how to access the object variables as well as choose the strings i want to display in one loop. I've also tried using obj.name and obj.surname which works fine, my problem is selecting the string variables i want to display. Should i give them a class to access them? The goal is not to save them in the database so i dont really want to use a model etc. Thanks for your help! -
Trying to understand if I should use existing PostgreSQL image or create my own
I am having a problem understanding how does making a separate container for existing PostgreSQL image work. Here is how my Django project is structured: Here is my docker-compose.yml file: version: '3' services: db: image: postgres environment: - POSTGRES_USER=stefan_radonjic - POSTGRES_PASSWORD=cepajecar995 - POSTGRES_DB=agent_technologies_db web: build: . command: python manage.py runserver 0.0.0.0:8000 --settings=agents.config.docker-settings volumes: - .:/agent-technologies ports: - "8000:8000" links: - db depends_on: - db Here db represents container for existing PostgreSQL image. And I am not sure if I have accomplished anything by setting up environment variable. Every time I try to google how Dockerizing Django Application with separate container for PostgreSQL is done, I find the following settings for DATABASE: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'HOST': 'db', 'PORT': 5432, } } But when I tried to run my dockerized project with sudo docker-compose up I got the error that password is required. I am also using manage.py file when I am running my project on my local machine - uses settings.py , and manage-docker.py file when i want to run dockerized Django project - uses docker-settings.py. Here is what my DATABASE settings in settings.py looks like: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', … -
Issue in form validation
I want to validate my update form using form validation. When i leave a field blank and hit update button I get a blank page showing the error message. I want that error message to be displayed on top of that particular field when I leave it blank and hit update button. This is my update view: def update(request, id): item = get_object_or_404(BookEntry, id=id) if request.method == "POST": form = UpdateForm(request.POST, instance=item) # print template error_messages = { 'error': form.errors, } if form.is_valid(): print(form.cleaned_data) post = form.save(commit=False) post.save() return HttpResponseRedirect(reverse('bms:index'), id) else: form = UpdateForm() return HttpResponse(json.dumps(error_messages)) return render(request, 'index.html',{'form':form}) This is my forms.py: class UpdateForm(forms.ModelForm): title = forms.CharField(max_length=100) author = forms.CharField(max_length=100) edition = forms.CharField(max_length=100) publisher = forms.CharField(max_length=100) genre = forms.CharField(max_length=100) detail = forms.CharField(max_length=100) language = forms.CharField(max_length=100) price = forms.IntegerField() dop = forms.CharField(max_length=100) cop = forms.CharField(max_length=100) copyright = forms.CharField(max_length=100) isbn = forms.IntegerField() class Meta: model = BookEntry fields = '__all__' This is my html form: <form action="{% url 'bms:update' book.id %}" id="updateform" name="updateform" method="POST"> {% csrf_token%} <div class = "form-group"> <label for = "title"> Title: </label> <input class = "form-control" id="book_title" type = "text" name="title" value="{{ book.title }}"> </div> <div class="form-group"> <label for = "author"> Author: </label> <input id="book_author" class … -
Django Celery Scrappy ERROR: twisted.internet.error.ReactorNotRestartable
I have next model: Command 'collect' (collect_positions.py) -> Celery task (tasks.py) -> ScrappySpider (MySpider) ... collect_positions.py: from django.core.management.base import BaseCommand from tracker.models import Keyword from tracker.tasks import positions class Command(BaseCommand): help = 'collect_positions' def handle(self, *args, **options): def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in range(0, len(l), n): yield l[i:i + n] chunk_size = 1 keywords = Keyword.objects.filter(product=product).values_list('id', flat=True) chunks_list = list(chunks(keywords, chunk_size)) positions.chunks(chunks_list, 1).apply_async(queue='collect_positions') return 0 tasks.py: from app_name.celery import app from scrapy.settings import Settings from scrapy_app import settings as scrapy_settings from scrapy_app.spiders.my_spider import MySpider from tracker.models import Keyword from scrapy.crawler import CrawlerProcess @app.task def positions(*args): s = Settings() s.setmodule(scrapy_settings) keywords = Keyword.objects.filter(id__in=list(args)) process = CrawlerProcess(s) process.crawl(MySpider, keywords_chunk=keywords) process.start() return 1 I run the command through the command line, which creates tasks for parsing. The first queue completes successfully, but other returned an error: twisted.internet.error.ReactorNotRestartable Please tell me how can I fix this error? I can provide any data if there is a need... -
How to store image files in Amazon S3 and Videos in Vimeo using Django
I want to store the image files in the S3 and the video files in the Vimeo in my django project. I am S3Boto3Storage of Django-storage as the storage backend. As Vimeo takes care of compression and content delivery for video files and it's cheap compared to S3, I want to use that for video files. I thought of uploading the file to the Vimeo using the upload_to attribute in the FileField of django model, but I can't able to get the absolute path of the video file in the method. here is my implementation of the upload_to method, def get_media_path(instance, filename): new_filename = '{}.{}'.format(uuid.uuid4(), filename.split('.')[-1]) if instance.type == 'image': file_path = 'posts/media/images/{filename}'.format( filename=new_filename) else: client = vimeo_client() file_path = client.upload( filename, data={ 'name': instance.title, 'description': instance.description } ) logger.info("File uploaded to: [{}]".format(file_path)) return file_path But it's giving me this error OSError: [Errno 2] No such file or directory: 'videoplayback.mp4' Or I could customise the S3Boto3Storage for my purpose. I would like to know what would be the better solution. -
Django CMS - Unsafe attempt to load URL <URL> from frame with URL <URL>. Domains, protocols and ports must match
I have all my django cms files on aws s3. I have problems with loading icons.svg from aws s3. I keep getting this message "Unsafe attempt to load URL from frame with URL . Domains, protocols and ports must match." The rest of my files loads without any issue its just the icons.svg that does not load correctly. Im using Heroku, with the heroku domain. Does anyone know how i solve this?