I have a form that is using NumberInput widget to accept a rating for a Song from the user. The number should be between 0-5. My Song model has a MaxValueValidator but I want the NumberInput widget to show options only from 0-5. Answer In your form you can add min and max value which atleast shows the user the
Tag: django
How to Get all post data from a request in Django?
Is there any way get all the form names from a request in Django ? Html request in the above I can get only from the name I know, what I need is to get all names of the django request and later parse it and get data. Answer Try use this: But if you need print a dynamic POST
How to add an url field to a serializer with Django Rest Framework
I am following the tutorial on Django Rest Framework – Tutorial 3 Class based views. How to add an url field (pointing to the current snippet) to a serializer? serializers.py urls.py Actual output Desired output Answer You have to use HyperlinkedModelSerializer serializer and HyperlinkedIdentityField field From Django Rest Framework documentation The HyperlinkedModelSerializer class is similar to the ModelSerializer class except
Catch IntegrityError in Django Admin
Working with Django 1.4 I would like to catch an Integrity Error when a user does enter a duplicate value. At this moment the application just shows the default Django error page. (debug = True) model admin form At this moment I get an error page showing a ValidationError instead of an IntegrityError. (Which makes perfect sense) How would I
Django percentage field
I’m trying to create a percentage field in Django, where the user just fills in 40 for 40%. There will be a percentage sign on the right of the input box so that they know they should fill in a percentage. 0.4 must be stored in the DB. So far I’ve tried the following: It works, but the problem is,
Django download a file
I’m quite new to using Django and I am trying to develop a website where the user is able to upload a number of excel files, these files are then stored in a media folder Webproject/project/media. The document is then displayed in a list along with any other document they have uploaded, which you can click into and it will
Django: Use firstof of if-else block inside blocktrans
I have two variables var1 and var2. I want to do this, It gives me error that ‘blocktrans’ doesn’t allow other block tags. Because we are not allowed to use any other tag inside blocktrans, what is the solution of this kind of problem? Answer From django 1.9 onwards, you can use firstof to assign result to context. See django-docs
Django admin is_staff based on group
Is it possible to have is_staff selected by choosing a group? Let’s say there are two groups: users, admins When a new user is in the users group he is not staff, but if he is in the admins group he is staff. Answer I managed to make it work by extending the UserAdmin class and in the get_form function
get mongodb “_id” in django template
My question is related to this question. I am trying to display all the _id of mongo database in django template from last 2 days but unable to get it. This is the error: This is the code I am trying: views.py product.html Though, I tried changing the name to static, cache etc, but still no luck. Answer template tag
How to return a non-zero exit code on a custom Django command while being able to test it?
I have a Django custom command that can return non-zero exit codes. I’d like to test this command, but the call to sys.exit(1) exits the test runner. Is there a “proper” way to assign the exit code in a custom Django command? My code currently looks like this: Answer I chose to mock the call to sys.exit() in the tests