Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Convert word to pdf on Python without MS Word or Libre Office and on deployed Heroku app (Django)
I want to convert a .docx file to .pdf file as part of a big project that I am working on. The only problem is that I work on widows and the project is deployed on Heroku which is Linux based. My locally tested library, packages don't work on deployed version since most of them use Word or Libre Office. Please help me with a library or code package or anything that I can implement in Python where I provide the input file as docx and want the output file to be a pdf doc. Sample input and output file: input = os.path.join(BASE_DIR, doc_name+'.pdf') output = os.path.join(BASE_DIR, doc_name+'.docx') -
Django: How to keep code and database sync'ed when deploying on Heroku?
Let's say we have a Django project called Alpha. Developers work on Alpha on their dev environment before deploying the Django project to Heroku. The Procfile may look something like: release: python manage.py migrate web: python -m gunicorn wsgi:application When a developer tries to deploy a new version of Alpha, the project is shipped altogether with its migrations files (as recommended). At the end of the deployment, Heroku will execute the release statement python manage.py migrate to make all the relevant changes to the database. Because release is part of the overall deployment process, if it fails then the new code will not be deployed. However... While the code will be reverted back to what it was before the deployment (as expected), the potential changes to the Heroku's database are going to be permament. For example, let's assume that the new version has three new migrations: 0011 0012 0013 The first two migrations are executed correctly and the database is changed accordingly. They are also added to the database table django_migrations. The last one, however, contains an issue (e.g., it defines a constraint that the stage data don't respect). In this scenario, the code on Heroku (both /migrations/ and models.py) … -
Using validate filename function in all Django sterilizers
I have a validate_filename function in a serializer but seems like I have to use the same function inside multiple serializer classes to validate the file name. It doesn't seem good because it seems redundant and the concept of DRY is not being implemented. For eg: class ExampleOne(serializers.ModelSerializer): class Meta: model = ExampleOne fields =[''] def create(self, validated_data): ... return item def validate_extension(filename): extension = os.path.splitext(filename)[1].replace(".", "") if extension.lower() not in ALLOWED_IMAGE_EXTENSIONS: raise serializers.ValidationError( (f'Invalid uploaded file type: {filename}'), code='invalid', Now, I have to use the same function inside other serializers class inside the same serializer file and don't want to repeat the same code again. Is there any way we can use a separate file like validatefile.py and import the function only inside the serializer class?? But dont really know how to start and go about it. -
Getting django.urls.exceptions.NoReverseMatch: Reverse for 'fruit' not found. 'fruit' is not a valid view function or pattern name while unit testing [duplicate]
So I'm unit testing my products app urls using unittest framework but I'm getting this error home(urls.py) urlpatterns = [ path('admin/', admin.site.urls), path('', home, name='home'), path('products/', include('products.urls', namespace='products')) ] "products" app(urls.py) urlpatterns = [ path("fruit", views.fruit, name='fruit'), ] "products" app (views.py) def fruit(request): product = Product.objects.filter(category="Fruit") n = Product.objects.filter(category="Fruit").count() params = {'product': product, 'n': n} return render(request, 'products/fruit.html', params) test_urls.py import unittest from django.urls import reverse, resolve from products.views import * class TestUrls(unittest.TestCase): def test_fruit_url_is_resolved(self): url = reverse('fruit') self.assertEquals(resolve(url).func.view_class, fruit) if __name__ == "__main__": unittest.main() -
How to save flags to Django database from admin panel?
In the Django admin panel, all fields are saved to the database, except for the flags field of the SubscriberPlan model. That is, I can (un)check any flag and try to thus update a record, but the flag statuses won't be saved to the database. If I run python manage.py shell, import SubscriberPlan, do something like plan = SubscriberPlan.objects.all()[0], plan.flags = "a" plan.save() then the database will be updated and the Active flag will be displayed in the Admin panel, but, still, it won't be possible to update it from the Admin panel. So, how is it possible in Django to save this kind of a field to the database from the Admin panel? To be honest, I don't understand why it's not saved by default, while other fields are saved. admin.py from django.contrib import admin from django.utils.safestring import mark_safe class SubscriberPlanFlagsWidget(forms.Widget): available_flags = ( ('a', ('Active')), ('n', ('New')), ('p', ('Popular')), def render(self, name, value, attrs=None, renderer=None): html = [] for f in self.available_flags: html.append('<li><input type="checkbox" id="flag_%(key)s" %(checked)s key="%(key)s"/><label for="flag_%(key)s">%(name)s</label></li>' % { 'key': f[0], 'name': f[1], 'checked': 'checked' if f[0] in value.lower() else ''}) html = '<input type="hidden" name="%s" value="%s"/><ul class="checkbox flags">%s</ul>' % (name, value, ''.join(html)) return mark_safe(html) class SubscriberPlanAdmin(admin.ModelAdmin): … -
How to pass data from form.cleaned_data as variable to another view in Django
I am new to Python and Django. I am working on a form that includes a couple of IntegerField instances such as: class Hex(models.Model): horizontal_rows = models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(20),validate_odd]) vertical_rows = models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(20)]) In the view I have the following: def square_view(request): if request.method == 'POST': form = SquareForm(request.POST) if form.is_valid(): print(form.cleaned_data) return redirect(reverse('puzzle:board')) This in return gives the following output once the form is submitted: {'horizontal_rows': 3, 'vertical_rows': 3} The request is redirected to another page via (reverse('puzzle:board'). My goal is to use the data from form.cleaned_data in order to generate a board with X number of rows and Y number of columns on the new page. I am unsure how to approach this. Should I pass the data as variables directly to the template (via jinja), or should I pass it to the view of the other page (reverse('puzzle:board'). -
Uploading files show permission denied (CentOS7 + Apache + mod_wsgi + django)
I deployed a beta version of my django app to DigitalOcean and I am serving it using Apache and WSGI. Everything works well include static files and 'get' media files(I saved it directly to DB), except uploading files. It shows 'Permission denied' error, with full directory like '/home/test/project/media/test.jpeg'. I configured httpd-vhosts like this. <VirtualHost *:80> ServerName example.com ServerAlias www.example.com WSGIScriptAlias / /home/test/project/project/wsgi.py WSGIDaemonProcess example.com python-home=/home/test/project/.venv python-path=/home/test/project/project WSGIProcessGroup example.com Alias /static/ /home/test/project/frontend/build/static/ # react build dir <Directory /home/test/project/frontend/build/static> Require all granted </Directory> Alias /uploadImg/ /home/test/project/media/ <Directory /home/test/project/media> Require all granted </Directory> <Directory /home/test/project/project> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost> So httpd is running with daemon, and my directory 'media' is owned by root, 755. But the directory 'media' is symbolic link dir which located at '/home/test/' I did many attempts to fix it but nothing works.. -
How to calculate the percentage of 5 stars, 4 stars rating for an object
I have this rating for my review django model. 5 star 4 star 3 star 2 star 1 star What I want to do is get the percentage of each component based on the rating. Something like this: # get 5 average rating five_avg_rating = qs.filter(rating=5).aggregate(Avg('rating'))['rating__avg'] five_avg_rating = (five_avg_rating / 5) * 100 if five_avg_rating != None else 0.0 # get 4 average rating four_avg_rating = qs.filter(rating=4).aggregate(Avg('rating'))['rating__avg'] four_avg_rating = (four_avg_rating / 5) * 100 if four_avg_rating != None else 0.0 .... # get 1 average rating one_avg_rating = qs.filter(rating=1).aggregate(Avg('rating'))['rating__avg'] one_avg_rating = (one_avg_rating / 5) * 100 if one_avg_rating != None else 0.0 The sum up percentages should = 100%. So if the math is correct, add all the rating percentages should = 100% Here is an image of what I get now, which is not what I really want: enter image description here How can I implement this better? -
Django: psycopg2.errors.UndefinedColumn
I have an app called map. Within map, I have the following models being specificed: class Checkin(models.Model): time = models.DateTimeField() class Pin(models.Model): name = models.CharField(max_length=64) latitude = models.FloatField() longitude = models.FloatField() user = models.ForeignKey(User, on_delete=models.CASCADE) place_id = models.TextField(default=None, null=True) address = models.TextField(default=None, null=True, blank=True) checkins = models.ForeignKey(Checkin, on_delete=models.CASCADE, default=None, null=True) class Meta: unique_together = ["name", "latitude", "longitude"] The migrations work fine. However, when I try to access the class Pin via the admin panel, I get this error message: psycopg2.errors.UndefinedColumn: column map_pin.checkins_id does not exist LINE 1: ...r_id", "map_pin"."place_id", "map_pin"."address", "map_pin".... If I try to access the class Pin via Pin.objects.all() I also receive the same error. -
I wanna update a value to request with React axios from Django
This is one of value in Django api. { "id": 89, "text": "dd", "checked": false }, { "id": 90, "text": "dd", "checked": false }, } I wanna update the "checked" value like true/false to request with react axios. Like a check toggle button. const [check, setCheck] = useState(false); return ( <> <button onClick = { () => setCheck(!check) }>X <div className = {todo ${check && 'checked'}}>{ text } </> ); -
Make a query with Django ORM?
I have used this code to make a query: views.py: reservations_table = Reservation.objects.all() total_commission = 0 for reservation in reservations_table: for city in COMMISSION_RATES.keys(): if reservation.city == city.upper(): total_commission += reservation.net_income * COMMISSION_RATES[city] / 100 models.py: from django.db import models class Reservation(models.Model): reservation_code = models.CharField(max_length=150,unique=True) checkin = models.DateField() checkout = models.DateField() flat = models.CharField(max_length=150) city = models.CharField(max_length=150) net_income = models.DecimalField(max_digits=10, decimal_places=2) def __str__(self): return self.reservation_code constants.py: COMMISSION_RATES = { 'LONDON': 10, 'PARIS': 12, 'PORTO': 9, } How to make a query but using only Django ORM? -
Get the total count of object with specific status
Trying to get the total count of objects to display on the homepage. Here's my code def dashboard(request): total_issues = Issue.objects.all().count() open_issues = Issue.objects.filter(mark_as='Open').count() closed_issues = Issue.objects.filter(mark_as='Closed').count() context = {'ordered_issues': ordered_issues, 'total_issues': total_issues, 'open_issues': open_issues, 'closed_issues': closed_issues} return render(request, 'issues/total_issues.html', context) and my model class Issue(models.Model): MARK_AS = ((True, 'Open'), (False, 'Closed')) title = models.CharField(max_length=100) content = models.TextField() date_posted = models.DateTimeField(default=timezone.now) author = models.ForeignKey(User, on_delete=models.CASCADE) assignee = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True, blank=True) mark_as = models.BooleanField(choices=MARK_AS, default=True) def __str__(self): return self.title def get_absolute_url(self): return reverse('issue-detail', kwargs={'pk': self.pk}) Nothing get outputted What did I do wrong? -
Can we filter out just the domain name from a link in a TextField object using urlize and a custom method?
Is it possible to use urlize and a custom filtering method in a TextField object to filter out the domain part of the URL, leaving the suffix and the prefix? For example: in, "hey this is a link https://www.google.com" we filter it out and the result looks something like this "hey this is a link google". I would really appreciate it if someone can help, thx! template {% for blog in blog %} <div class="description">{{ blog.description | urlizetrunc:20 }}</div> {% endfor %} -
Rotate native iPhone images based on EXIF info?
Working on a site where users can upload photos. As many of you many know, unedited iPhone photos use an "Orientation" EXIF tag to determine orientation. On my PC, I can't upload .HEIC files, and I can't upload files if I do manage.py runserver localhostIP:8000 due to SSL reasons, so it's pretty difficult to debug. Looking at the EXIF data on iPhone photos it seems like this should rotate the iPhone images properly, but they still upload with the incorrect orientation, and I can't really look at the what EXIF tags iPhone pictures are returning in the backend. im = Image.open(self.content_media) img_exif = im.getexif() for key, val in img_exif.items(): print(val) if "rotate 90 cw" in str(val).lower(): im.rotate(90) if "rotate 180 cw" in str(val).lower(): im.rotate(180) if "rotate 270 cw" in str(val).lower(): im.rotate(270) This is in the save method for one of my models that has media. -
Django - Regex validator does not show error message on wrong input
I used Regex Validator to validate phone_number field, the validator worked perfectly and saved only proper values. If the phone_number is entered in the wrong format, the form is not saved, however the error message is not showing up. I checked this Django regex validator message is not showing up, however I am not sure what I am missing. Here is my code: models.py from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.core.validators import RegexValidator # Create your models here. class MyAccountManager(BaseUserManager): def create_user(self, first_name, last_name, username, email, password=None): if not email: raise ValueError('User must have an email address') if not username: raise ValueError('User must have an username') user = self.model( email = self.normalize_email(email), username = username, first_name = first_name, last_name = last_name, ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, first_name, last_name, email, username, password): user = self.create_user( email = self.normalize_email(email), username = username, password = password, first_name = first_name, last_name = last_name, ) user.is_admin = True user.is_active = True user.is_staff = True user.is_superadmin = True user.save(using=self._db) return user class Account(AbstractBaseUser): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) username = models.CharField(max_length=50, unique=True) email = models.EmailField(max_length=100, unique=True) phone_number = models.CharField(validators=[RegexValidator(regex=r'^\+?1?\d{9,10}$', message='Invalid Phone Number', code='invalid_phone_number')], max_length=10, blank=True) # validators should be … -
How to redirect using a view name in Django, class-based views?
I have to redirect from one class-based View to another class-based View. I did it by: return redirect('report') but the suggestion was to redirect by View names. I did try this, but it doesn't work. views.py: class UploadView(View): def get(self, request, *args, **kwargs): Reservation.objects.all().delete() template = "ReportApp/upload.html" return render(request, template) def post(self, request, *args, **kwargs): # try: csv_file = request.FILES['file'] data_set = csv_file.read().decode('UTF-8') # setup a stream which is when we loop through each line we are able to handle a data in a stream io_string = io.StringIO(data_set) next(io_string) for column in csv.reader(io_string, delimiter=',', quotechar="|"): _ = Reservation.objects.update_or_create( reservation_code=column[0], checkin=column[1], checkout=column[2], flat=column[3], city=column[4], net_income=column[5], ) # return redirect('report') return redirect(ReportView.as_view()) upload_view = UploadView.as_view() class ReportView(View): def get(self, request, *args, **kwargs): urls.py: from .views import upload_view, report_view, city_commission_view, error_view from django.urls import path urlpatterns = [ path('upload', upload_view, name='upload'), path('report', report_view, name='report'), path('city_commission', city_commission_view, name='city_commission'), path('error', error_view, name='error'), ] any suggestions how to do this? -
How to add a given number of days to a django DateTimeField
I have a model with a field containing a DateTimeField and a function to calculate a return date based on this DateTimeField. How would I add a given number of days to this???? class MyModel(models.Model): startdate = models.DateTimeField(auto_now_add=True) enddate = models.DateField() def save(self) self.enddate = self.startdate + datetime.timedelta(days=5) super(MyModel, self).save(*args, **kwargs) This however does not work -
How to deal with time consuming python functions in Heroku?
I have successfully deployed a Django app to Heroku using Postgres. The only problem is that some of the python functions I have written can take up to several minutes to run (data scraping many pages with selenium and generating 50 different deep learning models with keras). If it takes longer than 30 seconds the app crashes. I ultimately plan on using this Heroku app as an API that I will connect to a frontend using React on netlify. Is there a way to automatically run these functions behind the scenes somehow? If not, how can I deploy a website that runs time consuming python functions in the backend and uses React for the frontend? -
Django 100% cpu usage, can't imagine it should be like that
[htop command on webserver][1] I am experiencing 100% cpu load on our webserver that is running a django application. It's a virtualized ubuntu 20. I just cannot imagine that it should be like that, do you know how to check what the issue might be? [VM has 5 Cores of an I7-7700K][2] [1]: https://i.stack.imgur.com/U53wE.jpg [2]: https://i.stack.imgur.com/0tcVg.png -
ChannelsLiveServerTestCase throwing URL not found
I'm wrtiting tests for my app which is a websocket app; well actually I wrote those tests and they worked but after I wrote a custom adapter for allauth, for a specific url it started throwing url not found error. I've defined my urls like this: project urls.py: urlpattens = [ ... path("messenger/", include("messenger.urls", namespace="messenger")), ... ] And then in messenger/urls.py: app_name = "messenger" urlpatterns = [ ... path("", views.chats_list_view, name="home"), ... ] Now, this is my tests.py: class TestConsumers(ChannelsLiveServerTestCase): serve_static = True # === SetUp === @classmethod def _set_chrome_options(cls): chrome_options = webdriver.chrome.options.Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_prefs = dict() chrome_options.experimental_options["prefs"] = chrome_prefs chrome_prefs["profile.default_content_settings"] = {"images": 2} return chrome_options @classmethod def _create_driver(cls, stores_logs=False): d = DesiredCapabilities.CHROME if stores_logs: d["goog:loggingPrefs"] = {'browser': 'ALL'} return webdriver.Chrome( options=cls._set_chrome_options(), executable_path="/usr/src/app/chromedriver/chromedriver", desired_capabilities=d, ) @classmethod def setUpClass(cls): super().setUpClass() try: cls.driver = cls._create_driver() except: super().tearDownClass() raise @classmethod def tearDownClass(cls): cls.driver.close() super().tearDownClass() # === Utility === def _create_user(self, driver, user_type, username="test", password="123456789", first_name="test", last_name="test"): user = get_user_model().objects.create_user( username=username, password=password, user_type=user_type, first_name=first_name, last_name=last_name, ) force_login(user, driver, self.live_server_url) return user def _create_principal_user(self, username="test", password="123456789", school_name="test school"): user = self._create_user(self.driver, username=username, password=password, user_type="SS") School.objects.create(name=school_name, support=user) return user def _create_a_group(self, driver, name="Test Group"): """ Create a chat group in the messenger app. return: … -
Heroku reject push project
i try to publish my python project but i have some issues can you help me ? i have requirements.txt also i add my git profile this is my first time with heroku i don't understand what is wrong i add my project on git repository and main branch i use python 3 this is my python project my project files and git files are in same level i tried something is i found on internet but it not worked for me and this is heroku error : -----> Building on the Heroku-20 stack -----> Using buildpack: heroku/python -----> Python app detected -----> No Python version was specified. Using the buildpack default: python-3.10.4 To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes -----> Installing python-3.10.4 -----> Installing pip 22.0.4, setuptools 60.10.0 and wheel 0.37.1 -----> Installing SQLite3 -----> Installing requirements with pip Collecting APScheduler==3.9.1 Downloading APScheduler-3.9.1-py2.py3-none-any.whl (59 kB) Collecting asgiref==3.5.0 Downloading asgiref-3.5.0-py3-none-any.whl (22 kB) Collecting beautifulsoup4==4.10.0 Downloading beautifulsoup4-4.10.0-py3-none-any.whl (97 kB) Collecting bs4==0.0.1 Downloading bs4-0.0.1.tar.gz (1.1 kB) Preparing metadata (setup.py): started Preparing metadata (setup.py): finished with status 'done' Collecting certifi==2021.10.8 Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB) Collecting cffi==1.15.0 Downloading cffi-1.15.0-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (446 kB) Collecting charset-normalizer==2.0.12 Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB) Collecting cryptography==36.0.1 Downloading cryptography-36.0.1-cp36-abi3-manylinux_2_24_x86_64.whl (3.6 MB) Collecting … -
Get choices values from variable text - Django
I have a model with a positivesmallinteger field that has these choices (from model_utils). PERSON_TYPE = Choices( (0, 'women', _('Women')), (1, 'men', _('Men')), (2, 'boys', _('Boys')), (3, 'girls', _('Girls')) ) Elsewhere in my code, I'm trying to read in a person type from a file, but when I go to save to the model, I only have the string version of the person type, not the numerical value I need to store in the db. I know how to use PERSON_TYPE.women to get the numerical value of a person type, but the issue is in my code the person type would be a string variable. It looks like this fileName = companyData.file.name df = pd.read_csv(fileName, delimiter=',', encoding='utf-8-sig') for index, row in df.iterrows(): custPersonType = row[int(form.cleaned_data['columnNames'][0])-1] #gives the person type string I basically want to do PERSON_TYPE.custPersonType, but that doesn't seem to be possible. Is there a way to do this without using a dictionary or a long if/else? -
TypeError: Object of type 'HttpResponseRedirect' is not JSON serializable
I'm trying to run a simple app which receives a payload from an external application and enters EntryLayerView. This view calls a method in utils.py which then redirects the payload to another view for processing. However, I keep seeing this not Json serializable error. Url.py path( "api/v2/myapp/assessments", views.EntryLayerView.as_view(), name="myapp-assessments", ), path( "api/v2/myapp/startconversation", views.startconversation.as_view(), name="myapp-start-assessment", ), Views.py The entry point to the app is the EntryLayerView class EntryLayerView(generics.GenericAPIView): permission_classes = (permissions.IsAuthenticated,) def post(self, request, *args, **kwargs): body = request.data response = get_endpoint(validated_body) #Don't worry about this line return Response(response, status=status.HTTP_200_OK) class startconversation(generics.GenericAPIView): permission_classes = (permissions.AllowAny,) def post(self, request, *args, **kwargs): print("I'm inside the view") redirect = request.GET.get('all the data') #This is the view I'm trying to pass data to utils.py def get_endpoint(payload): qs = '?'+ urlencode(payload) reverse_url = reverse('myapp-start-assessment') url = reverse_url + qs print(url) return HttpResponseRedirect(url) The output of url in utils.py is: /api/v2/myapp/startconversation?contact_uuid=67460e74-02e3-11e8-b443-00163e990bdb&choices=None&value=&cardType=&step=None&optionId=None&path=&title=&description=&message= Error: return json.dumps(*args, **kwargs) File "/usr/local/lib/python3.6/json/__init__.py", line 238, in dumps **kw).encode(obj) File "/usr/local/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/local/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/home/osboxes/ndoh-hub/venv/lib/python3.6/site-packages/rest_framework/utils/encoders.py", line 67, in default return super().default(obj) File "/usr/local/lib/python3.6/json/encoder.py", line 180, in default o.__class__.__name__) TypeError: Object of type 'HttpResponseRedirect' is not JSON serializable I'm unable … -
400 error when trying to login via social networks
I use the allauth package. I added allauth.socialaccount.providers.yandex in apps and created a yandex account. There I specified Callback URL: http://127.0.0.1/main/accounts/yandex/login/callback/. I go to http://127.0.0.1/main/accounts/yandex/login/, press Continue and get: 400 redirect_uri does not match the Callback URL defined for the client And no matter how I try to change something, I keep getting this error. What am I doing wrong? -
Assign clients to seller
In my application there are two types of users, sellers and clients. Each client must be assigned to exactly one seller but a seller can have multiple clients. I have created one usermodel, so all my users are saved in the same place. In the model there is a boolean field called "is_seller" and another boolean field called "is_client". These get set to true depending on which registration form the user use. The client must be assigned when they register but I am not able to get my head around how I should implement this logic. I would appreciate all sorts of guidance!