Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to consume API without saving data in models?
Currently, I'm in the process of creating an aggregated website from different API sources. How can I continue to serve the data, while not being able to save the data in models (due to API TOS)? Still haven't decided whether to use Flask or Django, however, can't seem to find that much material on being able to consume APIs. Eventually, I would like to setup native mobile applications for the aggregated site. I'm thinking I can possibly setup cron jobs to get API ever so often, but how can the data be persistent when someone visits site? Any knowledge, tips, resources will be appreciated. Thanks. -
How to solve "Generator expression must be parenthesized" Error?
I am new to django and setting up a web project using VS 2017. on startup i am getting this Message=SyntaxError('Generator expression must be parenthesized', ('C:\\Users\\Krunal\\AppData\\Local\\Programs\\Python\\Python37- 32\\lib\\site-packages\\django\\contrib\\admin\\widgets.py', 152, 20, " '%s=%s' % (k, v) for k, v in params.items(),\n")) Source=f:\canada\lakehead\clinical decision\disease predictor\djangowebproject2\djangowebproject2\manage.py StackTrace: File "f:\canada\lakehead\clinical decision\disease predictor\djangowebproject2\djangowebproject2\manage.py", line 17, in <module> execute_from_command_line(sys.argv) error. Any help is appreciated. Thanks -
copy values of mutiple ModelMutipleChoiceField into one after Post using MPTT
I've been struggling with this issue all day and hope someone can help. I have all my hierarchies classified by category in the same table. during the form creation, I want to separate each hierarchy by category and render it using a ModelMutipleChoiceField his way not all hierarchies are displayed together. The problem comes when the form is submitted, as I need to go through each ModelMutipleChoiceField field and get the selected values and copy these to the model field before saving the form. however, I am not able to iterate through the ModelMutipleChoiceField and get the selected values. I also don't know how to set these values on the ModelField NOTE: The number of hierarchies can vary. here is my code: I'm using Django MPTT and create my hierarchy structure using 2 models. one is the category(Hierarchy) and the other is the nodes of the hierarchy (HierarchyNode_MPTT). Then I created a separate model that has ManyToManyField pointing to the HierarchyNode_MPTT. Models.py class Hierarchy(models.Model): ID = kp.ObjectIDField() name = kp.ObjectNameField() ext_hierarchy = kp.ObjectTechnicalID() seq_no = kp.SeqNoField(unique=True) mptt_seq_no = models.PositiveIntegerField() class HierarchyNode_MPTT(MPTTModel): id = kp.ObjectIDField() name = kp.ObjectNameField() description = kp.ObjectDescriptionField() ext_node_id = kp.ObjectShortNameField() parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children') … -
Why do my columns not contain class names of their respective properties in django_tables2?
On my local install when I render a table using django_tables2, every entry has a class name specifying that entry's attribute/property name. For example, the column containing that entry's product name will have the class "product_name" on both it's <th> and <td> elements. However, this does not happen in production. None of the settings have been altered when transferring my local code to the server. Is this something I should explicitly specify in some setting somewhere? If so, where? -
Django: do not create migration when adding custom manager to auth.User
I would like to add a custom manager to the built-in auth.User model. I avoid switching to my own user model because of existing data in the database linking to the auth_user table. So I add the following to models.py: from django.contrib.auth.models import User, UserManager class ActiveUserManager(UserManager): use_in_migrations = False def get_queryset(self): return super().get_queryset().filter(is_active=True) # Monkeypatch auth.User to have custom manager User.add_to_class('active_users', ActiveUserManager()) This seems to work, until when I run python manage.py makemigrations, Django creates a migration file 000n_auto_20181002_1721.py in myvenv/Lib/site-packages/django/contrib/auth/migrations folder, with the following content: # imports omitted class Migration(migrations.Migration): dependencies = [ ('auth', '0008_alter_user_username_max_length'), ] operations = [ migrations.AlterModelManagers( name='user', managers=[ ('active_users', django.db.models.manager.Manager()), ('objects', django.contrib.auth.models.UserManager()), ], ), ] Setting use_in_migrations = False in class ActiveUserManager does not help. I would appreciate your suggestions on how to avoid creating this migration file or how to add a custom manager to the built-in auth.User model without this behavior. I am using Django 1.11. -
How to solve a problem using imshow and opencv
i got the error bellow when run my application in ubuntu. cv2.error: OpenCV(3.4.3) /io/opencv/modules/highgui/src/window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' Look my code: cap = cv2.VideoCapture(0) while (True): ret,frame = cap.read() cv2.imshow('Webcam - Press q ', frame) # Wait to press 'q' key for capturing if cv2.waitKey(1) & 0xFF == ord('q'): ... -
Django small bytestrings on model, what type should be used for modelling?
I have a model where I want to store a handful of small (2-4 byte) byte strings (ex: b'foo' or b'\x02'). I'm wondering about the best way to model them on my Django model. I thought I could use CharField, but that doesn't seem to work as I'd expect. BinaryField does seem to work, but I'm not sure if it's appropriate for fields that are short length (again, typically 2-4 bytes) Given the model: class Foobar(models.Model): charfield = models.CharField(max_length=10) binaryfield = models.BinaryField() When I do: >>> fb1 = Foobar() >>> fb1.charfield = b'\0000' >>> fb1.binaryfield = b'\0000' >>> fb1.save() And then read the record back: >>> read = Foobar.objects.get(id=fb1.id) >>> read.charfield == b'\0000' False >>> read.binaryfield == b'\0000' True I'd expect both equality checks to be True. Furthermore, the docs seem to indicate that filtering a queryset on a Binaryfield is not allowed (and is something I'll need to be able to do). Having said that, it seems to work for me: >>> Foobar.objects.filter(binaryfield__in=[b'\0000', b'blarg']) <QuerySet [<Foobar: Foobar object>]> Am I missing something about CharField's? Is BinaryField the appropriate choice here? Or is there a better alternative? In case it matters, I'm using Django 1.11 (most recent LTS version currently), … -
xhtml2pdf black square symbols
I'm trying to convert HTML with Georgian symbols to PDF, but every time i try it it generates black square characters, not real symbols. My html: {% load staticfiles %} <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>test</title> <style type="text/css"> @font-face { font-family: "code2000"; src: url("{% static "application/fonts/myfont.ttf" %}") } * { font-family: code2000; } @page { size: A4; margin: 1cm; } </style> </head> <body> <div class="main-content"> <p> სატესტო სატესტო სატესტო </p> </body> </html> generate_pdf.py: def render_to_file(path: str, params: dict): template = get_template(path) html = template.render(params) file_name = "{0}-{1}.pdf".format(params['invoice_type'], randint(1, 1000000)) file_path = os.path.join(os.path.abspath(os.path.dirname("__file__")), "public", file_name) with open(file_path, 'wb') as pdf: pisa.pisaDocument(BytesIO(html.encode("UTF-8")), pdf, encoding='UTF-8') return [file_name, file_path] and result is always: ■■■■ ■■■■■ ■■■ I tried to add utf-8 at the top of html page also changed fonts but nothing works. -
django.core.exceptions.FieldError: Unknown field(s) (escolhas) specified for Questoes
When I try to create migrations, this error appears, but it does not say in which file and in which line, in my model there is no field with the name displayed in traceback. my models.py https://pastebin.com/axVYFmWq class Questoes(models.Model): materia_CHOICES = ( ('Matemática', 'Matemática'), ('Português', 'Português'), ) assunto_CHOICES = ( ('Mecânica', 'Mecânica'), ('Matemática básica', 'Matemática básica'), ('Redação', 'Redação'), ) dificuldade_CHOICES = ( ('Fácil', 'fácil'), ('medio', 'médio'), ('dificil', 'difícil'), ) titulo = models.CharField(max_length=100, unique=True, db_index=True) materia = models.CharField(max_length=20, choices=materia_CHOICES) assunto = models.CharField(max_length=35, choices=assunto_CHOICES) # slugs slug_materia = AutoSlugField(populate_from='materia') slug_assunto = AutoSlugField(populate_from='assunto') slug_titulo = AutoSlugField(populate_from='titulo') dificuldade = models.CharField(max_length=10, choices=dificuldade_CHOICES) descricao = models.TextField(max_length=1000, blank=True) escolha_correta = models.TextField(max_length=500) criado = models.DateField(auto_now_add=True) class Meta: ordering = ('dificuldade', '-criado') def __str__(self): return self.titulo def get_absolute_url(self): return reverse('questoes:questao', args=[self.slug_materia, self.slug_assunto, self.slug_titulo] ) -
Django - Ajax: send an email in the background simple ajax task
I am trying to send an email in the background. I am pretty new with AJAX. The email is just not sending even though the console is saying 'done'. The email is set up correctly. However, Django needs to verify if the object already exists before it sends the email. I don't even want to get any data. As of now, I just want to send an email in the background. Your answers don't need to be specific but it would be great if you could guide me in the right direction. urls.py urlpatterns = [ url(r'^', views.Index), url(r'^ajax/email/', views.Send_Email_ajax), ] views.py def Index(request): if request.method == 'POST': form = Booking_Form(request.POST) if form.is_valid(): room = form.cleaned_data['room'] booked_from = form.cleaned_data['booked_from'] booked_from_time = form.cleaned_data['booked_from_time'] booked_until = form.cleaned_data['booked_until'] booked_until_time = form.cleaned_data['booked_until_time'] room1 = Room_List.objects.get(room=room) time_date1 = datetime.datetime.combine(datetime.date(booked_from.year, booked_from.month, booked_from.day), datetime.time(booked_from_time.hour, booked_from_time.minute)) time_date2 = datetime.datetime.combine(datetime.date(booked_until.year, booked_until.month, booked_until.day), datetime.time(booked_until_time.hour, booked_until_time.minute)) if Room_Booked.objects.filter(room=room,booked_from__gt =time_date1, booked_from__lt = time_date2).count() >=1 or Room_Booked.objects.filter(room=room,booked_until__gt =time_date1, booked_until__lt = time_date2).count() >=1: pass messages.error(request,'try again') else: Room_Booked.objects.create(room= room1, booked_from= time_date1, booked_until=time_date2, author='user') return HttpResponseRedirect('/') else: form = Booking_Form() return render(request, 'sc/index.html', {'form':form}) def Send_Email_ajax(request): send_mail( 'Subject here', 'test', 'xxx3@gmail.com', ['xxx3@gmail.com'], fail_silently=False) return HttpResponseRedirect('/') Index.html <form method='POST' id='request_form'> {% csrf_token %} {{form.errors}} Room Type: … -
Django project on AWS elastic beanstalk always has health "severe" and CPU load 100%
I've deployed a python django web app to AWS elastic beanstalk and have been having problems. The deployment worked for a couple of days but today it stopped working and I noticed some weird log entries. My health is always "severe" and my CPU load is always at 100% (user: 90+%) here are the logs from elastic beanstalk: https://pastebin.com/1KvSP6SJ What is this /cpv, and why is there so many requests for it? At one point there were +200 r/sec. Is this some sort of attack, or am I doing something wrong? My website is not working and is giving me HTTP 408 and HTTP 504 errors. -
Is there a better way to append a nested div using Jquery into a Sortable?
When I click on a checkbox I want my div to be appended into a Jquery Sortable. I can already to this the problem is that my div has nested elements like a span and another div. The only way I think I could do this is coping the entire div with it's elements inside a string and make that a variable and append that variable but I was wondering if there's a better way to do this that doensn't require me to copy the whole div. My html is this: <div id="dragndrop" class="dragndrop"> {% for collection in publication_collections %} <div position="{{ forloop.counter }}" class="sort_square ui-state-default sortable_text" {% if not collection.display_homepage %}style="display:none;"{% endif %}> <span id="remove_{{collection.id}}" col_id="{{collection.id}}" col_name="{{collection.name}}" class="link_pointer float_right margin_right_s text_content_blue_s_b_arial">x</span> <div class="sortable_text">{{collection.name}}</div> </div> {% endfor %} </div> And my Jquery code is this: function addElement(col_id) { $('[id^="enable_home"]').unbind("change").change(function () { var col_num = col_id; var name = $(this).attr("name"); var value = $('#display_type_vitrina_' + col_num).val(); if ($(this).is(':checked')) { $(this).attr('value', 'True'); $('#display_type_vitrina_' + col_num).show(); if (value !== 'carrusel') { $('#rotacion_' + col_id).hide(); } else { $('#rotacion_' + col_id).show(); } var $div = $("<div position='0' class='sort_square ui-state-default sortable_text'>").text(name); $("#dragndrop").append($div); $("#dragndrop").sortable('refresh'); } else { $(this).attr('value', 'False'); $('#display_type_vitrina_' + col_num).hide(); $('#rotacion_' + col_num).hide(); } … -
Django Edit Form Creates New Instance
I am creating a form that allows users to edit an event saved to a django database. The problem is when it runs the code, a new instance is created in the database, the old one remains in the databse and both are then available to view in the site Below is (some of) the HTML form that is used to call the edit view and edit the instance in the database <div class="modal-footer"> <form action="edit/" method="POST"> {% csrf_token %} <input type="hidden" name="event_edit_id" id="event_edit_id"> <button class="btn btn-primary btn-block" type="submit">Save Changes</button> </form> </div> Next, is the View that is used to edit the event for the id sent with the form: def edit(request): if request.method == "POST": event_id = int(request.POST.get('event_edit_id')) event = Events.objects.get(pk=event_id) form = AddEventForm(request.POST or None, instance=event) if form.is_valid(): form.save() return redirect('home') I know I am missing something but I just can't see it, any help appreciated! -
getting errors running at cmd line
exec(open("./path/to/script.py").read(), globals()) works for me in the python interactive console shell howver i get no output and my script has 3 print functions that i am looking for to output the reason i am in intwracve shellmode..is i tried many times to run my script at command line or IDLE only to get django settings issue or main package not a package or even the no mudule errors only here on this thread am i getting some progress does any one know how to get output at the console..?? -
How to perform comparison operation for Django FloatField/DecimalField
This is my model. class Student(models.Model): email = models.EmailField(unique=True) user = models.ForeignKey(UserModel, on_delete=models.CASCADE, null=True) usn = models.CharField(max_length=20, null=False, unique=True) full_name = models.CharField(max_length=256, null=False) contact_no = models.CharField(max_length=10, null=False) tenth = models.FloatField(default=0) twelfth = models.FloatField(default=0) cgpa = models.FloatField(default=0) backlog = models.IntegerField(null=True) active_backlog = models.IntegerField(default=0) branch = models.ForeignKey(Branches, on_delete=models.SET_NULL, null=True) pc = models.ForeignKey(PC, on_delete=models.SET_NULL, null=True) def __str__(self): return self.full_name I am not able to filter the following query. Student.objects.filter(tenth__gt='some decimal value') Example Student.objects.filter(tenth__gt=10.0) And it throws an Error decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>] It works with IntegerField but not with FloatField or DecimalField -
Jenkins junit gives error "ERROR: No test report files were found. Configuration error?"
I have Jenkins master container running in a Kubernetes cluster. I have a separate VM configured as a build slave so that it can build containers. I am using a Multibranch Pipeline with the Jenkinsfile in the git repo. The pipeline builds the Docker image which is a Django app. I have installed django_nose, so it can produce xunit files with the test results. The Django settings has the following options to enable xunit test results. TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' NOSE_ARGS = [ '--with-xunit', '--xunit-file=/tmp/tests/results/results.xml', ] In the pipeline, I have the following stage: stage("Test") { sh("docker run --rm -i \ -v '${env.WORKSPACE}/tests/results/':/tmp/tests/results \ ${image} python3 manage.py test") junit '${env.WORKSPACE}/tests/results/*.xml' } It is mounting a directory in the Jenkins workspace as a volume in the container so that the saved test results can be viewed by Jenkins after the container completes. When I run the build, I get the following error: [Pipeline] { (Test) [Pipeline] sh [my_project-test-7T6G6Z...QQBYGFA] Running shell script + docker run --rm -i -v /home/jenkins/jenkins_home/workspace/my_project-test-7T6G6Z...QQBYGFA/tests/results/:/tmp/tests/results my-registry/username/my_project python3 manage.py test nosetests --with-xunit --xunit-file=/tmp/tests/results/results.xml --verbosity=1 Creating test database for alias 'default'... ............ ---------------------------------------------------------------------- Ran 12 tests in 1.421s OK Destroying test database for alias 'default'... [Pipeline] junit Recording test results No … -
Django - Searching by first name or last name with postgres - use gin index?
Basic search functionality, search using first or last or both names. Model has first/last fields. Ideas are to just search by first, search by last, join results. Another would be to use a gin index on each, do the same, and order by similarity (slow?). Perhaps even concatenate the two things, use a gin index on that, and order by similiarity. Is that possible without having the change the models? -
Django dropdown form submission invalid
Hey i am trying to use modelchoicefield to get a dropdown list in html. But the submission of form yields a invalid form. My code is given below. views.py class SubjectSelectFormView(View): form_class = SubjectSelectForm template_name = 'study/select_subject.html' def get(self, request): form = self.form_class(user=request.user) return render(request, self.template_name, {'form':form}) def post(self, request): form = self.form_class(request.POST) if form.is_valid(): subject = models.Subject.objects.get(name=form['name']) return HttpResponseRedirect('study:quiz', subject.subject_id) else: return HttpResponse('<h1>Failed</h1>') forms.py class SubjectSelectForm(forms.Form): name = forms.ModelChoiceField(queryset=Subject.objects.all().order_by('name'), widget=forms.Select()) def __init__(self, *args, **kwargs): user = kwargs.pop('user', None) super(SubjectSelectForm,self).__init__(*args, **kwargs) self.fields['name'].queryset = Subject.objects.filter(user_id=user) html {% extends 'basic_home_app/base.html' %} {% block content %} <br> <form class="form-horizontal" action="" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Start"> </form> {% endblock %} -
Share dynamic data in memory between gunicorn workers
I have a web app written in Django/Celery/Postgres/Gunicorn/Nginx. The app allows performing scientific simulations to the user. These simulations can take from 5 seconds to 5 minutes. Regular requests and quick simulations are done with the standard blocking paradigm while long simulations are run in the background (some are even submitted to several AWS Lambda instances in parallel) by celery and then the client is updated by a WebSocket. When a client logs in and opens one of his projects, a Simulation object is initialized and stored in a dict as {user:Simulation}. Initializing this Simulation object can take about 10 seconds so it is only done at the beginning. Every time the user interacts with his simulation on the client side, a particular view queries the Simulation object to the global dict and applies any changes, retrieves data, saves the simulation, runs the simulation, etc. The problem with this approach is that it only works with 1 gunicorn worker since additional workers do not have access to the Simulation objects inside the global dict. Moreover, it is not possible to pre-load the objects since they are constantly changed by the user. What is the best approach to work with such … -
Understanding implications of using multiprocessing to manage long-running db queries
We are dealing with an extremely (over several minutes) long-running Django orm keyword text query that is causing timeouts. For reasons we've yet to diagnose, when a keyword search is made on our postgres db containing over 1 million text records, the timeout occurs despite having implemented what we believe is the recommended approach to improving performance in such as scenario (e.g GIN indexes and to_tsvectors). There are probably many stones yet unturned, but as a temporary fix, initial Googling points to a solution to manage/stop the hanging query using the multiprocessing module. For example: import multiprocessing import time def run_problem_db_query(textFileSearchParameter, returned_statements): # this is the db query that hangs..... retrieved_text_files = statements.extra(where=["textfiles_tsv @@ plainto_tsquery(%s)"], params=[textFileSearchParameter]) return retrieved_text_files retrieved_text_files = None p = multiprocessing.Process(target=run_problem_db_query, name="run_problem_db_query", args=(textSearch, retrieved_text_files)) p.start() time.sleep(10) p.terminate() p.join() Even though a few blogs and SO posts have recommended a version of this approach, what are the potential problems (if any)? Ultimately, we want to fix the query of course and any potential issues with the db itself, but aside from consuming a potentially-large memory footprint (as mentioned here), what else are the potential pitfalls of such an approach? -
Reverse for 'profile-update' with arguments '('',)' not found. 1 pattern(s) tried: ['/update/(?P<pk>[0-9]+)/$']
this error occurs everytime when i try to open my profile it worked just fine in the first time but in the second time it starts showing me this error !!! views.py class updatePicture(UpdateView): model = Profile fields = ['image'] template_name = 'store/update_picture.html' def get_success_url(self): return reverse_lazy('profile', kwargs={'pk': self.get_object().id}) urls.py url(r'/update/(?P<pk>\d+)/$', views.updatePicture.as_view(), name='profile-update') profile.html <a href="{% url 'profile-update' prf.pk %}"> <div class="file btn btn-lg " > Change Photo <input type="file" name="file"/> </div></a> </div> -
How to Render MathJax with Django-Markdown-Deux
I am trying to render the following as markdown using django-markdown-deux: *Here* is an equation: $f(x) = \begin{cases} 5 & x > 0 \\ 10 $ x \leq 0 \end{cases}$ At the moment, the double slashes in the mathjax equation are being replaced by single slashes, which prevents the mathjax from correctly formatting. What can I do to prevent this? Thanks. -
filter_horizon or filter_vertical for InLine ManyToMany field at Django 2.x Admin
Here is my model relation (copied from Django 2.1 official documentation. My model is exact copy of this) class Person(models.Model): name = models.CharField(max_length=50) class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField( Person, through='Membership', through_fields=('group', 'person'), ) class Membership(models.Model): group = models.ForeignKey(Group, on_delete=models.CASCADE) person = models.ForeignKey(Person, on_delete=models.CASCADE) Here is my admin.py class MembershipInLine(admin.TabularInline): model = Group class PersonAdmin(admin.ModelAdmin): inlines = [ MembershipInLine, ] I get the field on my Person page like this; Currenct view But instead i would like to have this style veiw; Desired view I've found filter_horizontal and filter_vertical on the official documentation here but couldn't figured out how can I use them with inline. How can I do that? -
Why isn't Django's .set() working for me?
I have a model: class Questionnaire(models.Model): label = models.CharField(max_length=48) organization = models.ForeignKey('Home.Organization', on_delete=models.PROTECT) date_opened = models.DateField() date_closed = models.DateField(null=True, blank=True) eligible_users = models.ManyToManyField(Respondent, related_name="users") and it has a signal: @receiver(post_save, sender=Questionnaire) def add_new_eligible_users_to_questionnaire(sender, instance, created, **kwargs): print(one) if created: print(two) if instance.open_to_all: print(three) users = list(Respondent.objects.filter(organization=instance.organization).values_list('id', flat=True)) print(users) instance.eligible_users.set(users) print(instance.eligible_users) instance.save() and no matter what I do, I can't get .set() to save the related objects to the Questionnaire object. I get the same console output every time: one two three [5, 4, 3, 2] Survey.Respondent.None 1 I've tried all of the following ways to deliver the input to set: users = Respondent.objects.filter(organization=instance.organization).values_list('id', flat=True) users = list(Respondent.objects.filter(organization=instance.organization).values_list('id', flat=True)) users = list(Respondent.objects.filter(organization=instance.organization)) users = Respondent.objects.filter(organization=instance.organization) What might I be doing wrong? -
Using if condition with django choice field in templates
I want to conditionally display fields of a form in django template. I use a current value of a choice field as follows. However I can not see this working as expected and nothing is rendered at all. In the model: PENDING = 'PEN' CONFIRMED = 'CFM' CANCELED = 'CAN' CHOICE_BOOKING_STATUS = ( (PENDING, 'Pending'), (CONFIRMED, 'Confirmed'), (CANCELED, 'Canceled'), ) booking_status = models.CharField(max_length=4, choices=CHOICE_BOOKING_STATUS, default=PENDING) In the template: {% if form.booking_status == CHOICE_BOOKING_STATUS.PENDING %} <p>Please make a payment to confirm booking.</p> {% endif %}