Django template radio button auto select - python

I am developing an app with django. Here is the screenshot. I need to know when user will select on a plan in the next page that plan radio button should auto select.
I am able to grab all the information regarding the selected plan but dont understand how to select that plan's radio button.
here is the screenshots. Let me know anyone has the answer.
here is the next image where the radio buttons are present.

You could store the selected plan inside a sessions key and then check if that key exists in the next page... something like:
# in your first view set the session keys of posted plan
if 'express' in request.POST:
request.session['express'] = 'express chosen' # strings are easier to handle
# add the other plans to the if statement
# in the next page's view function, add keys to context if they are in the session framework like so
if 'express' in request.session:
express = request.session['express']
# add the other plans to the if statement
Now in the template, check if the variables exist to select option as default...
<input type="radio" {%if express%}checked{%endif%}>
<label>Express</label>
<!-- repeat for the other plans -->

Related

Adding more fields to the signup form

I want to know if I can add more fields to the signup form if a user checks a box. For example, if the user checks the box that says he is a photographer one more field will appear in which he can specify what type of photography he does.
This is what DHTML is. You need to add an event on the checkbox which will dynamically add the INPUT element in the form. something like.
<input type="checkbox" onclick="addInput(this)" id="photography">
now in the method add a text element dynamically with dynamic id e.g.(value-photography) so that you can take an action on server side.
you need to make use of
document.appendChild()
You can use jQuery as well.

Update database field with multiple buttons

I'm working on a small project using Django and I have a detail page of a digital product. The user should have the avability to update the status of this product through 3 Buttons. This status represents one ChoiceField in the database. The choices are: Dismiss, In Review and Approve.
Clicking the button should submit the form directly.
My question is, what is the best way to solve that (without Ajax)?
My first idea was to add 3 forms and every form has another action url.
I'm still fairly new to Django and don't know if my approach is the best solution.
As described in this question and in the docs you should be able to get what you want with a RadioSelect form widget.
If you add a bit of CSS to hide the radio inputs themselves, and style the labels as buttons, you should be good!
Edit to include my comment below:
To get a single click submission, you could write some simple javascript that catches the onclick event on your buttons, and submits the form as you click.

Check whether user is in django_db or no

I have the django model called "create-chat" and there I write names of people I want to see in a group chat.
So. I created the model, but I don't know how to make a condition whether user is in django_db or no, if so then I want to show them the button "join chat"
And after clicking the button he will be transferred to the url sitename.com/lobby/1
Now I have the view that shows the page /lobby/any_number
First of all I'd to block these pages from users and only if the user is in django_db
He is able to visit the page Lobby/1
ONLY THE lobby/1 not lobby/2
I don't ask you to do it, I just want you to explain me how to do it, or maybe advice me some articles about my problem.
https://i.stack.imgur.com/FQw4Y.jpg
..1). How to block all users for visiting this url sitename.com/1234 <- here goes any number u d liked. ( 4 characters )
So now I blocked all the users for visiting this page, and how now make it visible for a certain users/user?
I am assuming you are storing the information in User object.
usr = User.objects.get(username = whateveristheusername)
if usr is not None:
#User Exist. Click to join chat
else:
#Create User or any other viable functionality

Performing sensitive Django actions while masking/hiding underlying URL

I have a simple Django view that confirms the deletion of an instance. On this page is a simple form containing two buttons, "Cancel" and "Delete."
The Cancel button simply returns the user to the page from which the original delete button was pressed.
The Delete button jumps to a second view that performs the actual action. Thus, my URLs are defined as follows:
url(r'^confirmDeleteItem/(?P<item_key>\w+)$', 'confirm_delete_item'), # Confirms
url(r'^deleteItem/(?P<item_key>\w+)$', 'delete_item'), # Performs the action
On the confirmation page, the form is defined with a POST action that visits the second URL:
<form action="/squash/deleteItem/{{ item.key }}/" method="POST">
...
</form>
The problem I have with this is that the Items are fairly large (they store lots of data) and sensitive, so I'd like to force the user to jump through the confirmation hoop every time.
I would like to either prevent the User from visiting the /deleteItem/ page manually, or just hide the browser's loading of this page to avoid from it becoming stored in the history, accidentally bookmarked, etc.
Is wrapping the action in an AJAX call the best way to solve this problem, or are there more standard/preferred solutions? Thanks!
How about setting a session variable in confirmDeleteItem view (i.e. prepareToDelete = item.key) and checking in deleteItem view whether this session variable exists and whether the value match the item.key? Then you'd just need to remove it after actual delete occurred.
A quick idea which I'm not sure of, but should work in your case.
Check your referer in your deleteItem view. Like in this snippet of code. If user didn't come to the delete view from confirmDelete view, redirect him to the appropriate confirmDelete view.

Only displaying one radio button widget from a Django RadioSelect

I am making a form with Django, and need to manually lay out the form in the HTML. I have one field that's a ChoiceField and the widget is a RadioSelect (i.e. radio buttons rather than select drop down). There are 2 choices in this field (i.e. there will be 2 radio buttons when it's displayed).
If i do {{ form.fieldname }}, then it puts in both radio buttons there. I want to control where each radio button goes (since I need to put other things 'beside' each radio button, rather than just the textual value from the choices parameter).
I can manually enter the correct html <input type="radio" name="fieldname"…, however this won't reflect the value, if the field is set to the first choice then this HTML won't have selected="selected" on it.
Is there anyway, in the django template, to just put in the first radio button for a field? Something like this:
{{ form.fieldname.radio_button_0 }} (some of my custom html here)
Some more stuff {{ form.fieldname.radio_button_1 }} some more html here.
This page has information on how to iterate over each radio button:
https://docs.djangoproject.com/en/dev/ref/forms/widgets/
See "class RadioSelect".
Try form.fieldname.widget.renderer[%CHOICE%]. For example if you have choices = (('o', 'one'),('t', 'two')), then valid %CHOICE% would be 'o' or 't'.

Categories

Resources