I would like to set the value of a field in multiple rows of Django Admin.
For example if I had database of books with shelf locations I might move several books to another shelf. I need a way, within Django Admin, to input the new shelf location and update the multiple selected items.
I have seen that you can run Admin Actions but I need an easy way to input a value into the action.
You can do this with admin actions, by providing an intermediate page with a form to input the value you want:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages
Alternatively you could use some client-side scripting to collect the value from the user and append it to the querystring (or as an extra input field int he POST data) when submitting the admin action form.
Your admin action function receives the request object as an argument so has access to the extra GET/POST fields:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/actions/#adding-actions-to-the-modeladmin
Related
i'm trying to make a user registration with multiple fields to save in the same model. The idea is have a form in the home page with username and password and when press button continue the user is redirected to another page with multiple fields to insert personal info for the user profile.
I imagine i must have two different views for that, but i don't understand how put all this data together.
Thanks!
You could use the FormWizard (https://django-formtools.readthedocs.io/en/latest/wizard.html)
Basically you would have one form for user/password and another form for the other fields.
Take a look
In your case i would prefer manipulate HTML content with JavaScript and after everything is ready, just send an ajax call.
i'm creating a app.it has manytomany field to store data about class and students.
urls.py
url(r'^class/(?p<title>[-\w]+)/(?p<id>[\d]+)/',views.list,name ='list'),
Basically one user(Teacher) can create many class_room .Each class_room have one title and many students following in that class.
problem is:
Each class_room have unique url. Eg (mywebsite.com/science/88/) this link is access only for following students not for anonymous user.This is a loop hole if any non following students try some random url like this they could see the page (mywebsite.com/maths/2500/).
How to restrict a student from access a page which he is not following?
the UserPassesTestMixin mixin can be used to this effect. Basically, write a View Class that implements the test_funcfunction. This function has access to self so you can read the URL and the user. if the test_func returns True, the user is allowed to go on, otherwise is passed to access control (probably redirected to the login form if configured).
I'm working on a project where i do need to do a communication in 3 parts. Let me explain:
1.- The user fills a form, which is used to create a set of objects from the models. This objects are not saved in the database on this moment. All the objects are related between them.
2.- This objects must be saved in some way for an admin user to check the data and decide if proceed to save it, or if any element is invalid and reject the form.
3.- If the admin decide that the data is correct, select an option to send a .save() to add the data to the database.
At least that's the idea i have on how should work. I decided to create the objects before send it to the admin because it sounded easier to show that if i sended the request.POST and the request.FILES. My problem is that i don't know where could save the objects to show it to the admin when he connects(There are 2 types of users, Normal and Admin, the normal are the ones that fill the forms, the admins only check it, the two of them has their own views). So, does anyone how could i send the data and storage it until the admin connects? I'm open to any idea if this is not possible, the only thing that is necesary is the flow of: User fills the form, admin check it, data is saved or rejected.
You will want to save() them when the user submits the form at the start. Add a BooleanField to your model that says whether the row has been moderated and accepted. Then in your application, filter out all non-moderated rows, and on the admin side, filter out only rows that need moderation.
I need to get the selected Value of a forms.Choicefield for an if/else statement which produces another forms.Choicefield based on the selected Value
forms.py:
class ReceiverForm(forms.Form):
receivers = forms.ChoiceField(choices=db_mails(), required=True, label='Receivers')
if db_certs(<- selected value from receivers ChoiceField ->):
print "cert found"
encryption = forms.ChoiceField(choices=EncryptionChoiceAll, initial='smime_mail', required=True, label='Encryption')
else:
print "no cert found"
encryption = forms.ChoiceField(choices=EncryptionChoiceNoCert, initial='smime_mail', required=True, label='Encryption')
db_mails() and db_certs(mail) are working as expected
Is there a way to achieve what i need in forms.py or am I totally wrong with the design?
When the form is built and you specify the choice list, you have no way to know which value is selected, as:
You create an instance of the form class
This instance is used to create the view (On a GET request). The user can modify the selection
He submits the form. For this you create an instance of the class, which is filled with the POST values.
So at the time you create the class you do not have the information.
There are possibilities to have dynamic values in one choice, depending on the other one, but this needs to be done on the client side, when the user changes the selection:
Use javascript/jquery to update the list of choices depending on the answer. For this, you could have hidden values in the HTML file, and Javascript will update the list from these hidden values.
If the choice to update is more dynamic (For example a list of towns depending on the post code), you still need to use Javascript, but with Ajax that will send an asynchronous request to the server to get the list and update the choices.
There are plenty of tutorials to do this, and this is not directly linked to Django. As an example:
https://css-tricks.com/dynamic-dropdowns/
Is it possible to create a model form off of the session data in DJango? I am trying to create a shopping cart and within that cart I want the user to be able to update the quantity. I'm thinking I can create my own model and insert the session keys into that model along with my fields but DJango already provides a model so I figure it would be better to use that one. Please help if possible, It's hard to understand sessions as it is (yes I read the documentation).
I want to set a list of products on a page and next to it add a field for quantity that the person chooses to buy. When they click add to cart I'm thinking that it would send over a session id along with the item names and quantities that they chose. From there I will use the item quantity number to create formsets for additional information on each item. Finally, I will send that over to paypal to pay and check out.