In django admin, when there is a ForeignKey or ManyToManyField, there is [+] button to add it like this :
How can I make that [+] button with popup window using forms in templates ?
UPDATE 01:
I WANT TO CREATE POPUP WINDOW LIKE THIS IN MY TEMPLATE, NOT IN MY ADMIN PANEL.
You can use the admin reverse urls.
So in code:
<button class="button" onClick="window.open('{% url 'admin:model_add' %}');">
<span class="icon-plus">+</span>
</button>
This is not tested! It's just a quick try out of my brain.
Related
I am new to django.
What i am trying to do is to run a django function from my "main.py" file when a html button is pressed.
what I've done is created a button in html:
<input type="button" value="Formatting excel" onclick="excel_formatting">
And then in my "main.py " I have:
def excel_formatting():
print("hello world")
return()
The website loads fine, but when i press the button nothing is printed in the terminal. i must be doing something wrong.
you can have a button that is a link and when the user presses the button it will redirect them to the link which will activate the function.
views.py
def button(request):
print('hello')
urls.py
from . import views as main_views
path('button/', main_views.button, name="button"),
index.html
<a href="{% url 'button' %}" class="btn btn-primary">Button</button>
For example I have the following situation where a delete button has to be routed different depending upon who has clicked it, like if he's an admin display a message else go to the delete view. I thought I will place the function to send a message in detail generic view but how to call that function directly from template?
{% if user_detail.status == ADMIN %}
<button class="btn clearfix"><i class="icon-trash"></i>Cannot Delete Admin</button>
{% else %}
<i class="icon-trash"></i>Delete User
{% endif %}
The delete generic view is directly linked to the template, is there to display message in detail view itself before redirecting to the template?
There are many solutions to do this, one is to check for staff-status in the View, using one of the multiple mixins already made for this or check if hes a admin some other way and then either return an error message or redirect to one i.e
def myview(request):
if not request.user.is_staff:
return HttpResponseRedirect(..errormessage-url or view..)
.. do stuff here for admin users..
I am extending the Django admin. I have a click button on the change_form that takes me to another customized page. I want to add a button on the customized page to take me back to the change_form (keeping track of the instance I am working on by the object_id). What do I put in the href of the click button to go back to the change form? My guess was this:
<a class="changeform" href="{% url 'admin:change_form' object_id %}">{% trans "Click HERE to return." %}</a>
but that does not work. Thanks in advance for any advice.
Your view name (instead of admin:change_form) should be along the lines of admin:myapp_mymodel_change
The example setup from the 'effective django' tutorial works in the following manner:
On the contacts_list page, the user clicks 'delete'.
The DeleteView re-directs to the confirm delete page.
The user clicks the confirm (or cancel) button and is redirected back to contacts_list page.
What I would like to do instead is:
click 'delete' and pop up a bootstrap modal
confirm delete (or cancel) in the modal
then return to the contacts_list page.
Additionally I would like to do the same for edit and create. Delete just seemed like the simplest case.
Looking up similar topics, it appears modals don't really call a link(/view) and jquery/ajax should be able to solve the issue. I am still a little unclear on the concepts and best practices. Any insights appreciated.
url.py
url(r'^$', contacts.views.ContactListView.as_view(), name='contacts-list',),
url(r'^new$', cts.views.CreateContactView.as_view(), name='contacts-new',),
url(r'^edit/(?P<pk>\d+)$', contacts.views.UpdateContactView.as_view(), name='contacts-edit',),
url(r'^delete/(?P<pk>\d+)$', contacts.views.DeleteContactView.as_view(), name='contacts-delete',),
url(r'^detail/(?P<pk>\d+)$', contacts.views.ContactDetailView.as_view(), name='contacts-view',),
views.py
class ContactListView(ListView):
model = Contact
template_name = 'contact_list.html'
class DeleteContactView(DeleteView):
model = Contact
template_name = 'delete_contact.html'
def get_success_url(self):
return reverse('contacts-list')
contact_list.html
{% for contact in object_list %}
<li><h3>{{ contact.first_name }} </h3>
edit
delete
</li>
{% endfor %}
You need to omit the validation step provided by django's DeleteView.
Do not bother sending ajax calls.
First, figure out how to display the modal form.
Then you just have to declare the form method as POST (GET would give you the confirmation template) and leave the action as it is.
As an example we have a log in view with 'username', 'password' and 'country' fields and a 'login'.
How can one invoke the action related to the 'login' button when pressing enter on any of the inputs in a group.? For instance, all inputs within a container of some sort (div, form, fieldset, etc).
To get your form controls trigger the sumbit action you could define your controls of type submit.
...
{{input type="submit" ...}}
...
<button type="submit">...</button>
...
or the more elegant way is to having your parent view catch all the events and trigger the submit once, but this depends how nested your view is.
Hope it helps
Put your action on your form tag, like so:
<form {{action yourAction on="submit"}}>
{{input text}}
{{input text}}
<button type="submit"></button>
</form>
This will cause your event to fire whenever the form is submit (by clicking the button, by hitting enter in a field, etc.