In my app I'm using the django-photologue (3rd-part app) and in its default configuration, the "View on site" admin button point to its template.
I've read that the "View on site" button use get_absolute_url() model function to implement its behavior.
I need to change the url behind "View on site" button.
Do I need to override get_absolute_url() or edit other things??
Thanks in advance.
You shouldn't change files of third-party apps if you can avoid it, because you'll essentially need to maintain a fork of the app. Instead of overriding get_absolute_url, you can override the object-tools block in the admin template. Something like this:
{% extends "admin/change_form.html" %}
{% block object-tools %}
{% if change %}{% if not is_popup %}
<ul class="object-tools">
{% block object-tools-items %}
<li>
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
{% trans "History" %}
</li>
{% if has_absolute_url %}<li>{% trans "View on site" %}</li>{% endif%}
{% endblock %}
</ul>
{% endif %}{% endif %}
{% endblock %}
Save this as templates/admin/photologue/<modelnamehere>/change_form.html (change <modelnamehere> to the model you want to customize)
In a future version of Django (1.7 I guess), you will be able to override the method view_on_site to do the same thing.
Overriding get_absolute_URL() is proposed in the Django docs so it should be good to go.
See: Other model instance methods
Related
I'm trying to check if user logged in in the main template using this:
{%if request.user%}
...
{%endif%}
but it's not working maybe because main template doesn't have a view
could any one help i don't know if the question duplicated,but i didn't find my answer.
you could use:
{% if user.is_authenticated %}
...
{% endif %}
{% if user.is_authenticated %}
...
{% endif %}
Link to the Django docs. this should always be your first point of reference.
i'm new in Django developing.
I'm following the tutorial about Library on MDN (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django)
Until i follow the code all work but i'm trying implement author page by myself. Probably is very stupid issue but is one day that i'm turning around like a dog with its tail.
There is 2 page: author_list and author detail.
I set urls.py (in my project) i set view.py and crate my template.
I follow the same step of tutorial for realize book_list and book_detail but when i click on my author the page don't go to the detail of that author and stay in author_list.html.
Here the code urls.py :
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('author/<int:pk>', views.AuthorDetailView.as_view(), name='author-detail'),
Here views.py:
class AuthorListView(generic.ListView):
model = Author
class AuthorDetailView(generic.ListView):
model = Author
Here author_list.html with link get_absolute_url:
{% extends "base_generic.html"%}
{% block content %}
<h1>Author list</h1>
{% if author_list %}
<ul>
{% for aut in author_list %}
<li>{{ aut.first_name }} - {{ aut.last_name }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no author.</p>
{% endif %}
{% endblock %}
Here author_detail.html:
{% extends "base_generic.html" %}
{% block content %}
<h1>Author</h1>
{% if author %}
<p><strong>Nome: </strong> {{ author }}</p>
<p><strong>Nato il : </strong> {{ author.date_of_birth }}</p>
<p><strong>Morto il : </strong> {{ author.date_of_death }}</p>
{% endif %}
{% endblock %}
Here the screenshot
Author_list.html before click url=catalog/authors/
After click url change but page not
Thank to all for help
I believe you need DetailView instead of ListView for AuthorDetailView.
Looks like a typo to me, you want generic.DetailView (instead of ListView) for the author/<int:pk> path.
I also don't think it's right to extend base_generic for the template for the detail view. But that depends exactly what is in this base template.
I have created a template tag in django for forms that only require a submit button, without any other fields (except the hidden csrf field). It can be used as follows:
{% button_only_form "button_text" "action/path" "optionally css classes. %}
I'm currently mainly using this for delete buttons, because delete views require sending a post request, but don't need any other data.
Now I'm finding myself in a situation where I need to dynamically create the action. I'm iterating over a list of items, and the action url for each item would be the result of calling reverse("items:delete", kwargs={"item_id": item.id})
How would I specify this for my template tag? Here's a hugely simplified version of my template:
<ul>
{% for item in items %}
<li>
{{item.title}} - {% button_only_form "Delete" Im_not_sure_how_to_pass_the_action %}
</li>
{% endfor %}
</ul>
You can use the {% url ... as var %} syntax to first create the url for the action and then pass it as a parameter to the button_only_form tag:
<ul>
{% for item in items %}
<li>
{% url "items:delete" item_id=item.id as del_url %}
{{item.title}} -
{% button_only_form "Delete" del_url "..." %}
</li>
{% endfor %}
</ul>
I have a site which I am using user authentication to limit access to.
I am using django-allauth to handle social media authentication, but want to limit access to staff accounts only (set in the django admin panel).
I want to use a template as a header (bootstrap, nav bar etc.) with the main content loaded afterwards, or a message asking the user to login or request to be verified by admins.
My header template file: inventory/header.html
<body>
{% load bootstrap3 %}
{% if user.is_authenticated %}
{% if user.is_staff%}
{% block main %}{% endblock %}
{% block scripts %}{% endblock %}
{% else %}
<h1>Please ask an administrator to activate your account</h1>
{% endif %}
{% else %}
<h1>Please login to see this page</h1>
{% endif %}
</body>
And another template called by the view: inventory/home.html
{% include "inventory/header.html" %}
{% block main %}
This is the home page.
{% endblock %}
The view:
def home(request):
return render(request,'inventory/home.html')
Whenever I call the view, regardless of whether the user is logged in or a staff member, the main block is always displayed. The other error messages are displayed correctly.
I'm trying to avoid inserting is_authenticated/is_staff into all of my templates. I have also thought about using the login_required view decorator but that does not solve my is_staff issue.
Turns out I should have used
{% extends "inventory/header.html" %}
instead of
{% include "inventory/header.html" %}
Doh!
i'm developing a small app with Python and Google app engine. I'm using boilerplate (https://github.com/coto/gae-boilerplate) as front-end which follows gae direction and python templates, so nothing diffrent than plain stuff.
Now, what i would like to have is this.
When a user logged in, if the field of name and last name are not filled in i would like to have, in the home page, the profile editing.
The page for editing the profile is a template (which extend the base.html), called edit_profile.html which works well.
The homepage is a template as well (extend the base.html) called home.html.
Now, can i include the edit_profile.html in home.html? how can i do it?
this is what i've, i don't know what to put instead of ???? i tried with
{% block edit_profile.html %} {% endblock %}
but does not work
{% if user_info.name and user_info.last_name %}
..
{% else %}
????
{% endif %}
thanks.
So you want to include only some block of given template. There are two solutions:
1) Create template just for profile editing form and include it into edit_profile.html. Then include it also into home.html to if condition branch:
profile_form.html:
<form action="{% url some-action %}">
{{ form }}
<input type="submit" value="save"/>
</form
profile_edit.html
{% extends "base.html" %}
{% block main %}
{% include "profile_form.html" %}
{% endblock %}
home.html
{% if user_info.name and user_info.last_name %}
{% include "profile_form.html" %}
{% endif %}
2) use variable for extended template:
profile_form.html
{% extend BASE_TEMPLATE %}
and set it into context w/ different value as needed:
in home.html (let's say included_form.html is some basic template)
{% if user_info.name and user_info.last_name %}
{% with "included_form.html" as BASE_TEMPLATE %}
{% include "edit_profile.html" %}
{% endwith %}
{% endif %}
and if you want show form as a standalone page, set BASE_TEMPLATE to base.html