Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Can't log into localhost:8000/admin ... ERR_CONNECTION_REFUSED
I'm still pretty new to this web dev. I have been having trouble accessing the django admin site. I'm able to do localhost:8000 just fine. If I do localhost:8000/admin, I'm directed to the admin login page. I have already created id/pw through createsuperuser. Anyway, once I type in my id and pw, I noticed my dev server (localhost or 127.0.0.1) terminates and the login page will throw me an error saying "this site can't be reached" with ERR_CONNECTION_REFUSED. I've tried turning off my internet/firewall, but it still gave me the same result. I would greatly appreciate your help. I've been having so much trouble with this because I can't access the django admin site... (i'm using macOS Catalina, if that's any useful. Also I'm just following the basic Django tutorials, so i'm not using anything like gunicorn or nginx either. ) Thanks. -
Get file mime type on pre_save in django admin
I would like to save the file mime type by getting it on pre_save signal. from django.db.models.signals import pre_save from django.db import models import magic class Media (models.Media): file = models.FileField() content_type = models.CharField(max_length=128, editable=False) def media_pre_save(sender, instance, *args, **kwargs): if not instance.content_type: mime = magic.Magic(mime=True) instance.content_type = mime.from_buffer(instance.file.read()) pre_save.connect(media_pre_save, sender=Media) But I'm getting application/x-empty when I view it in db. What am I doing wrong? -
django test giving abnormal results
I am getting weird results when testing for 2 different views. They are both list views. The first one should only show active orders(active=True), the second one only showing historic views (completed=True). I am using postgresql as a database also. Issue is that both the view works perfectly fine in the browser, however when testing pytest raises an error saying that an order that shouldn't be listed, is listed. The views are as follows: class OrderHistoryView(LoginRequiredMixin, ListView): template_name = 'orders/order_history.html' def get_queryset(self): user = self.request.user qs = Order.objects.filter(Q(buyer=user, completed=True)|Q(seller=user, completed=True)) return qs class OrderListView(LoginRequiredMixin, ListView): template_name = 'orders/order_list_view.html' def get_queryset(self): user = self.request.user qs = Order.objects.filter(Q(buyer=user, active=True)|Q(seller=user, active=True)) return qs The tests are: class OrderHistoryViewTests(TestCase): @classmethod def setUp(self): self.req = RequestFactory() self.user = mixer.blend('user.CustomUser', email='test@test.com', password='1234') self.user2 = mixer.blend('user.CustomUser') self.advert = mixer.blend('advert.Advert', author=self.user) self.offer = mixer.blend( 'offer.Offer', advert=self.advert, author=self.user2, receiver=self.user, accepted=True) self.order = mixer.blend( 'orders.Order', advert=self.advert, seller=self.user2, buyer=self.user, offer=self.offer, pending=True, completed=True, active=True, disputed=False, cancelled=False) self.order2 = mixer.blend('orders.Order', completed=False, buyer=self.user) def test_only_shows_completed(self): request = self.req.get('/') request.user = self.user resp = OrderHistoryView.as_view()(request) self.assertContains(resp, self.order) self.assertNotContains(resp, order2) The second test is exactly the same, it just tests that its only showing active orders error message is : FAILED orders/tests/test_views.py::OrderHistoryViewTests::test_only_shows_completed - AssertionError: 3 != … -
Add Mutagen in Django an store result in database?
I'm just started with de Django Framework, and I'm realy new in this. :) I need a thing that print me the duration in seconds of a uploaded mp3 song. So I've found mutagen and this code: # function to convert the seconds into readable format def convert(seconds): hours = seconds // 3600 seconds %= 3600 mins = seconds // 60 seconds %= 60 return hours, mins, seconds # Create an MP3 object # Specify the directory address to the mp3 file as a parameter audio = MP3("G:\Sample.mp3") # Contains all the metadata about the mp3 file audio_info = audio.info length_in_secs = int(audio_info.length) hours, mins, seconds = convert(length_in_secs) print("Hours:", hours) print("Minutes:", mins) print("Seconds:", seconds) Now my stupid question - how can I integrate this in my django (please detailed informations if possible), and how can I store this result (with a button?) in the Django database please? An ansewer would be realy great, thanks in advance. Greetings Frank -
Serving static files from root directory in Django
I have app level static files and want to serve global static files such as JQuery, my project structure is like this: mysite ├───dashboard │ ├───migrations │ ├───static │ │ └───dashboard │ │ ├───css │ │ ├───img │ │ └───js │ └───templates │ └───dashboard ├───etc ├───static │ └───js | └───jquery.min.js └───mysite I added the followed the django docs so that the settings.py looks STATIC_ROOT = "" STATIC_URL = '/static/' STATICFILES_DIR = [ os.path.join(BASE_DIR, "static"), os.path.join("static"), ] STATICFILES_FINDERS = [ 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ] when I try to access it from a template, it returns a response code 404 in the runserver log, [09/Apr/2020 01:59:34] "GET /static/js/jquery.min.js HTTP/1.1" 404 1674 the template tag is like this {% load static %} <script src="{% static 'js/jquery.min.js' %}"></script> This is my first django project without following a tutorial, and any help would be appreciated. Thank You -
should the directory of the static file always be myapp/static/myapp/any staticfile
Though i use the staticfiles_dir in the setting and specify where to look for the static files.it works fine and have no problem,or may be there is only one app,so it works nice . i looked into django documentation and it suggests to use both. strong text -
How to use MongoDB with Django==2.2. Getting error when migrating from MySQL to MongoDB. SQLDecodeError at /en/admin/cms/page/add/
I have migrated from MySQL to MongoDB but when trying to add Django CMS pages getting below error - Error SQLDecodeError at /en/admin/cms/page/add/ Below are my requirement.txt - Django==2.2 django-classy-tags==1.0.0 django-cms==3.7.1 django-debug-toolbar==2.2 django-filer==1.7.0 django-formtools==2.2 django-js-asset==1.2.2 django-mongodb-engine==0.6.0 django-mongodb-engine-py3==0.6.0.1 django-mongokit==0.2.6 django-mptt==0.11.0 django-polymorphic==2.1.2 django-sekizai==1.1.0 django-treebeard==4.3.1 djangocms-admin-style==1.5.0 djangocms-attributes-field==1.2.0 djangocms-column==1.9.0 djangocms-file==2.4.0 djangocms-googlemap==1.4.0 djangocms-link==2.5.0 djangocms-picture==2.4.0 djangocms-snippet==2.3.0 djangocms-style==2.3.0 djangocms-text-ckeditor==3.9.0 djangocms-video==2.3.0 djangotoolbox==1.8.0 djongo==1.3.1 easy-thumbnails==2.7 fhir.resources==5.0.1 html5lib==1.0.1 isodate==0.6.0 mongoengine==0.19.1 mongokit==0.9.1.1 mysqlclient==1.4.6 Pillow==7.0.0 pkg-resources==0.0.0 pymongo==3.7.2 python-dateutil==2.8.1 pytz==2019.3 six==1.14.0 sqlparse==0.2.4 Unidecode==1.1.1 webencodings==0.5.1 -
Reading random columns from a csv file: Django
From a csv file, I want to read only 3 columns: {Id, name, approve} But when the user uploads a csv file, they could end up giving extra information in a random order e.g: {approve, reject, Id, foreign, name, approve, description} So far I have only been able to match if the "allowed headers" are present in the uploaded csv (the code below works). But how can I read the data from these "allowed headers" columns (considering their index position might be in a random order) ? My code (forms.py) def clean(self): uploaded_csv_file = self.cleaned_data['csv_file'] allowed_headers = {'id','name','approve'} validation_errors = [] if uploaded_csv_file: filename = uploaded_csv_file.name if not filename.endswith(settings.FILE_UPLOAD_TYPE): raise forms.ValidationError("Please upload .csv extension files only") # django InMemoryUploadedFile returns bytes and we need strings rows = csv.reader(StringIO(uploaded_csv_file.read().decode('utf-8'))) header = next(rows) csv_fields = set(header) csv_fields = {field.lower() for field in csv_fields} result = all(elem in allowed_headers for elem in csv_fields) # Check if list1 contains all elements of list2 using all() if not result: validation_errors.append( ValidationError('Your file could not be processed. You should provide %s columns in the file' % ', '.join(allowed_headers))) Thank you in advance :) -
Override Django Admin Landing Page
I am creating a custom django admin. I am trying to have it so that when a user logs into /admin they are directed to /admin/database (the app admin rather than the projects). I have worked overriding "index.html" with "base_site.html". This works somewhat, however, I cannot completely edit the main django-admin template so it just focuses on the apps and its causing issues for my project. Logging out issues: After logging out, when navigating to 'admin/database', I can still see the contents of the page, but when clicking on a "action" link (such as change password), it prompts to login. I think I'm fudging up the templates somehow. I want to entirely replace the django admin landing page with the custom one I built at "admin/database/index.html" path('admin/database/', include('database.urls')), path('admin/', admin.site.urls), Conceptually I would like 'admin' to point to 'admin/database' with full admin functionality. I tried deleting 'admin/', admin.site.urls and it threw an error. -
Celery task previous run date
I have some Celery tasks that I use to train some models in my Django application. I schedule them using crontab. Is there a was to get the current date for which a task is running, the current schedule date? -
How to create a Many to Many Relation in Django
I´m new in Django and I´ve been trying to create a Model, where I have many warehouses and many products. So far I have achieved this: class Product(models.Model): idProduct = models.IntegerField(primary_key=True) name = models.CharField(max_length=50) quantity = models.IntegerField() def __str__(self): return self.name class Warehouse(models.Model): idWareouse = models.IntegerField(primary_key=True) name = models.CharField(max_length=50) address = models.CharField(max_length=100) city = models.CharField(max_length=50) products = models.ManyToManyField(Product) def __str__(self): return self.name But the problem is, that one product can be in multiple warehouses with different product quantity, and I don´t know how to model this. Thank you -
Django ORM Subqueries
I'm trying to figure out how to perform the following SQL query with the Django ORM: SELECT main.A, main.B, main.C FROM (SELECT main.A, MAX(main.B) FROM main GROUP BY main.A) subq WHERE main.A = subq.A AND main.B = subq.B The last two lines are necessary because they recover the column C value when B is at a maximum in the group by. Without them, I would have A and the corresponding Max B but not the C value when B is at its max. I have searched extensively but cannot find an example that can construct this query using the Django ORM. Most examples use Django's Subquery class and show how to match the sub-queryset up with one column (so doing main.A = subq.A). But how do I match 2+ columns? -
Django Text box additional text issue
I am getting an extra text with a text box in Django. I wrote it initially for testing purpose but after even removing all lines "enter item number , this field is required" I am getting that with a text box.please check image for more clarity here and If I add any more Textboxes to my code by doing "ENTER= forms.IntegerField" I am getting "enter item number , this field is required" embedded over that textbox too. The code is as below:: forms.py from django import forms import re class InputForm(forms.Form): print("inside forms") regex = re.compile('^([1-9]{8})$', re.UNICODE) ENTER_ITEM_NUMBER= forms.RegexField(max_length=8, regex=regex,help_text=("Required 8 digits between {0-9}.")) input.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action = "{% url 'item'%}" method = "post"> {% csrf_token %} {{form}} <input type="submit" value=Submit" name="submitted"> </form> urls.py urlpatterns = [ path('inputs/', views.home_view, name='inputs'), path('item/', views.itemnumber, name='item'), ] views.py from django.db import connection from django.shortcuts import render from .forms import InputForm def home_view(request): context1 ={} context1['form'] = InputForm(request.POST) return render(request, "input.html", context1) def itemnumber(request): if (request.GET.get('submitted')): c = request.get['ENTER_ITEM_NUMBER'] cursor = connection.cursor() try: itemnumber = c C=cursor.execute(f"EXEC ValidateBusinessrule '0000000000{itemnumber}'") result_set = cursor.fetchall() result_set1= [' {} '.format(x) for x in result_set] context = {"row": result_set1} … -
Basic auth protected views in DRF
I have some API endpoints that i need to protect using HTTP Basic Authentication in Django Rest Framework. There is BasicAuthentication in DRF, but that actually authenticates against a user in Django, which is not what I'm looking for. I found a solution using a custom permission, but ti means monkey patching the views to set the correct authenticate header. Is there a better way? class BasicAuthPermission(permissions.BasePermission): def has_permission(self, request, view): credentials = view.credentials # Will raise AttributeError on missing credentials realm = getattr(view, 'realm', 'Protected') auth = request.headers.get('Authorization') with suppress(ValueError, AttributeError): auth = b64decode(auth.split()[-1]).decode() if auth != credentials: # Monkey patch style view.get_authenticate_header = lambda r: f'Basic realm="{realm}"' raise exceptions.AuthenticationFailed('Bad credentials.') return True Im my view: class ProtectedApiView(generics.GenericAPIView): permission_classes = [BasicAuthPermission] credentials = 'user:password' # ... -
Is there anything i missed in configuring django-rest-framework-datatables, getting error "DataTables warning: table id=test - Ajax error"
I tried to configure datatables with rest frame work, i'm getting error when page loads all the datatables fields like pagination, search and title is showing but no data is showing. what might be the reason? serializers.py class PermissionSerializer(serializers.ModelSerializer): class Meta: model = Permission fields = ( 'name', 'code', 'app', ) views.py from rest_framework import viewsets from .serializers import PermissionSerializer class PermissionViewSet(viewsets.ModelViewSet): queryset = Permission.objects.all() serializer_class = PermissionSerializer class ViewallPerms(View): def get(self, request): context = { 'a' : 'a', } return render(request, 'flamika_admin/view_allpermissions.html', context) urls.py url(r'^perms/$', views.PermissionViewSet, name='perms'), path('all-perms', login_required(views.ViewallPerms.as_view(), login_url='f_admin:admin_login'), name='all-perm'), view_allpermissions.html <script src="https://code.jquery.com/jquery-1.8.0.min.js"></script> <div class="row"> <div class="col-sm-12 col-xs-12"> <table id="test" class="table table-striped table-bordered" style="width:100%"> <thead> <tr> <th>Code</th> <th>Name</th> <th>App</th> </tr> </thead> </table> </div> </div> <script> $(document).ready(function() { var table = $('#test').DataTable({ "serverSide": true, dataSrc: "", "ajax": "{% url 'flamika_admin:perms' %}", "columns": [ {"data": "name"}, // Use dot notation to reference nested serializers. // This data: could alternatively be displayed with the serializer's ReadOnlyField as well, as seen in the minimal example. {"data": "code"}, {"data": "app"}, ] }); $('.btn-decade').on('click', function() { table.columns().search(''); var rel = $(this).attr('rel'); if (rel) { table.columns(3).search('^' + rel + '[0-9]$', true).draw(); } else { table.draw(); } }); $('#albums_minimal').DataTable({ "search": {"regex": true}, "language": {"searchPlaceholder": "regular expression"} … -
django.core.exceptions.ImproperlyConfigured: The included URLconf 'api.urls' does not appear to have any patterns in it
Having a hard time understanding why I am receiving this error. If I just leave the api/user/ path it works fine but when I try to add api/user/date_counter/ path I get this error. Using Django 3. Any help would be appreciated. date_counter/urls.py from django.urls import path from date_counter import views app_name = 'date_counter' urlpatterns = [ path('date_counter/', views.DateCounterViewSet.as_view(), name='date_counter'), ] date_counter/views.py from django.shortcuts import render from date_counter.models import Date_Counter from date_counter.serializers import DateCounterSerializer class DateCounterViewSet(viewsets.ModelViewSet): queryset = Date_Counter.objects.all() serializer_class = DateCounterSerializer date_counter/serializers.py from date_counter.models import Date_Counter from rest_framework import serializers class DateCounterSerializer(serializers.ModelSerializer): class meta: model = Date_Counter fields = ['user', 'date', 'count'] date_counter/models.py from django.db import models from user.models import User class Date_Counter(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) date = models.DateField(auto_now=True) count = models.IntegerField(default=0) api/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('api/user/', include('user.urls')), path('api/user/date_counter/', include('date_counter.urls')), ] api/settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'knox', 'user', 'date_counter', ] Stack Trace Exception in thread django-main-thread: Traceback (most recent call last): File "/Users/francisco_navarro/.local/share/virtualenvs/pomodoro_tracker-2HYScThJ/lib/python3.6/site-packages/django/urls/resolvers.py", line 590, in url_patterns iter(patterns) TypeError: 'module' object is not iterable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, … -
Deploy django app on linux shared hosting
I've made a django app and I'm ready to deploy it to a live server with my custom domain. The problem I'm facing is my I'm using a shared hosting and the terminal access is disabled by the administrator. SO is there any way to deploy django app successfully from cpanel. Thanks in advance. -
Django - oscar show a field in dashboard
from oscar.apps.catalogue.abstract_models import AbstractProduct from oscar.core.compat import AUTH_USER_MODEL from django.db import models class Product(AbstractProduct): seller = models.ForeignKey( AUTH_USER_MODEL, on_delete=models.CASCADE, null=True) from oscar.apps.catalogue.models import * I added this code to forked catalog model > I want to show it in the dashboard,Image of dashboard and dropdown box I tried admin.site.register but it is not working. -
I am trying to use a locally hosted Django webserver's HTML button to execute a python script that's on my local machine and I'm stumped
So, I'm pretty new to Django, python, and javascript. I have a partially functional Django webserver that, while developing it, I host locally on my machine. I've made a button with HTML which I've figured out can be tied to a javascript script. The next step for me was to make this button "execute" a python script that's sitting on my machine, particularly on the /desktop directory. I can of course move the script though, but the important part is that I want that python script (let's call it django_test_script.py) to open its own window on my machine. It's just a file that says "hi" in stdout, then closes after 5 seconds, but due to the plan for this whole project, I want that to happen. I want to have my website open on my machine, click that button, then have the script's console pop up on my desktop and run/finish. The eventual goal is to control an LED strip that's plugged into my raspberry pi. I want colored buttons on the website that, when clicked, run appropriate python scripts that turn the lights to that color. It wouldn't take me long to write up the python scripts themselves that … -
Django - Customizing admin form element: modifying change_form to add jquery plugin
I appear to have a valid output when I view the page source on my customized django admin page. But I'm trying to add a jquery datetimepicker to the "Timeslot start" form element, and nothing happens when I click on it. No javascript error, nothing. Debug is True on my settings.py and there are no errors. change_form.html changes: {% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}"> <link rel="stylesheet" type="text/css" href="{% static 'css/schedule/jquery.datetimepicker.css' %}"/ > {% endblock %} . . . {% block admin_change_form_document_ready %} {{ block.super }} <script src="{% static 'js/jquery-3.2.1.min.js' %}"></script> <script src="{% static 'js/schedule/jquery.datetimepicker.full.min.js' %}"></script> <script type="text/javascript" id="django-admin-form-add-constants" src="{% static 'admin/js/change_form.js' %}" {% if adminform and add %} data-model-name="{{ opts.model_name }}" {% endif %}> jQuery(document).ready(function () { jQuery('#DTPicker').datetimepicker(); }); </script> {% endblock %} This is the page with the datetimepicker instructions, which I think I have followed correctly in this case. admin.py class TeacherAvailabilityAdmin(admin.ModelAdmin): form = TeacherAvailabilityAdminForm list_display = ('teacher_id', 'student_id', 'timeslot_start',) forms.py class TeacherAvailabilityAdminForm(forms.ModelForm): TEACHER_CHOICES_QUERYSET = TeacherProfile.objects.available_teachers() STUDENT_CHOICES_QUERYSET = StudentProfile.objects.active_students() teacher = forms.ModelChoiceField( queryset=TEACHER_CHOICES_QUERYSET, empty_label="Select One", to_field_name="id") student = forms.ModelChoiceField( queryset=STUDENT_CHOICES_QUERYSET, empty_label="Select One", to_field_name="id") timeslot_start = forms.DateTimeField(widget=forms.DateTimeInput(attrs={'id': 'DTPicker'}), required=True) class Meta: model = TeacherAvailability fields = ('teacher', 'student', 'timeslot_start') -
Need help to mash this code. I'm trying to copy a media file from someone and the DIR is messed up
I have media player I need to put on a site but, I don't know how to combine all this code in settings.py its throwing unrelated errors. STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'firegod/static') ] STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) STATIC_ROOT = os.path.join(PROJECT_DIR, 'static') PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__)) STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, '..', 'static'), ) MEDIA_URL = 'media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') -
Using opencv to read a video file in Django, yet the cam won't open?
I am getting a video file from the user and storing it in the media folder. Then I pass a path to the file to a function, but when I use opencv to open the file, the cam won't open. start = time.time() cam = cv2.VideoCapture(video_name) fr = cam.get(cv2.CAP_PROP_FPS) The video_name is of the form /media/video_name.mp4 The error report is the following [ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (116) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:235: error: (-5:Bad argument) CAP_IMAGES: error, expected '0?[1-9][du]' pattern, got: /media/video_name.mp4 in function 'cv::icvExtractPattern' -
Pulling changed data from heroku
I have deployed a Django app in Heroku. I want the updated SQLite file to be reflected in my local repository. Is there any way I can do this. -
How to calculate total price using django?
I have simple django code, but it doesn't work. The code works to calculate total price from price and qty of the item purchased, then save it into the database. Here the code class Items(models.Model): items_name = models.CharField(max_length=50) harga = models.PositiveIntegerField() def __str__(self): return self.items_name class Transaction (models.Model): items = models.ForeignKey(Items, on_delete=models.CASCADE) qty = models.PositiveIntegerField() def total_price(self): total = self.qty * self.items.harga return total total_price = models.PositiveIntegerField(total_price) -
Django - makemigrations - No changes detected/Apps could not be found
I was trying to create migrations within an existing app using the makemigrations command but it outputs "No changes detected" The code I used for that is `python3 manage.py makemigrations` I tried python3 manage.py makemigrations polls and it shows "App 'polls' could not be found. Is it in INSTALLED_APPS?" Here is my INSTALLED_APPS of the settings.py file INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls.apps.PollsConfig', ] I know some people simply put 'polls', it doesn't work either Here is my folder structure: File structure Here is my apps.py from django.apps import AppConfig class PollsConfig(AppConfig): name = 'polls I am using django2.1 and python3.8