change verbose name of VISIT SITE in django 2.x admin - django

I only seem to find answers around changing the actual link of VISIT PAGE. How however can I change the verbose name to e.g. HOMEPAGE?
Below code only changes the actual link:
class AdminSite:
admin.site.site_url = "/hompeage"
What I'd like to achieve:
I'm looking to achieve this in the admin.py file of my application.

I haven't personally tried any ways, but looking the code
You can do it in two ways,
Override the base.html template on admin.
See: Django Github base.html
{% translate 'View site' %}
to
HomePage
If you look the code, it uses translation for
{% translate 'View site' %}
You can simply, override the locale string too.
See: Django Github en locale

Related

Is it possible to display the HTML provided by the admin panel app on-site?

I've built a site where I can create new posts (essays) by the admin panel. The output is visible to users. But when I place some HTML as content in the form it doesn't render itself on the page.
example:
Output on the page (with marked unrendered HTML):
I would like to know how to fix it and also, how to name the topic I want to know ( I couldn't find anything related to my problem, probably because I don't know how to express it).
Additionally, I just start to wonder if there is one more problem nested inside. How to link CSS from the static folder having this HTML mentioned above?
Django offer the autoescape template in the builtins tags
{% autoescape off %}
{{ myhtml }}
{% endautoescape %}
But your logic seems wrong, you don't need to create a new page with the doctype, just create a base template and use the block content tag to insert your article.
In your base template replace the description and title of your page by variables that will be populated by the article data.
You need to learn the basic of Django https://docs.djangoproject.com/en/4.1/ trust me you won't regret it !

{% url %} gives me NoReverseMatch error while reverse() returns the url just fine. Why?

I don't know if this SO question is of the same problem that I am about to describe, but it does share the same symptoms. Unfortunately, it still remains unresolved as I am writing.
So here is my problem. I am trying to add James Bennett's django-registration app to my django project. I have pretty much finished configuring it to my needs - custom templates and urls. Just when I thought everything was good to go. I got NoReverseMatch error from using {% url 'testing' item_id=123 %} (I also tried using the view name, myapp.views.test, instead but no luck) in one of the custom templates required by django-registration. Interestingly, I tried reverse('testing', kwargs={'item_id':123}) in the shell and the url was returned just fine. I thought {% url %} uses reverse() in the back-end but why did I get different outcomes?
urls.py: (the URLconf of my site)
urlpatterns = patterns('myapp.views',
url(r'^test/(?P<item_id>\d+)/$', 'test', name='testing'),
)
activation_email.txt: (the said template. Note it's intentionally in .txt extension as required by django-registration and that shouldn't be the cause of the problem.)
{% comment %}Used to generate the body of the activation email.{% endcomment %}
Welcome to {{ site }}! Please activate your account by clicking on the following link:
{% url 'testing' item_id=123 %}
Note the activation link/code will be expired in {{ expiration_days }} days.
I don't know if it matters but just thought I should mention activation_email.txt is stored in the templates directory of myapp though it is used by django-registration.
Also, I am using django 1.4
I have a feeling that the problem has something to do with the url namespaces, a topic that I have never understood, but it's just a naive guess. (IMO, the django documentation is great in explaining everything about django, except when it comes to url namespaces)
I'm no expert here, but in a Django project I'm working on at the moment I use the name of the url without quotes. I just added quotes around a similar line in one of my templates and it produced the same error as your error.
Try:
{% url testing item_id=123 %}

Django app to "switch on" other apps for specyfic user

I am new to django and I really like its modular construction so I decided to take advantage of it and put all the separated functionalities each in different app.
Now I need a way to switch on and off this apps by both user and admin.
The user options panel would look like this:
[ ] blog
---------------------
[ ] tagging [BUY]
After checking "blog" option user would get the blog in his profile and after buying and checking "tagging" he would get tagging for the blog.
The admin panel would have an ability to show or hide an app from user panel.
I wonder if:
there is an app which would help me switch on and off an app for specyfic user
and if not -
what would be a proper "architecture" for such django app?
Can it be done dynamically in middleware or should it be done during login (check available apps from database, switch them on, redirect to user home page)?
Any advices for such a task?
Thanks,
Robert
I haven't heard of any such app… But I don't expect it would be too hard to build.
If I were doing it, I would put a permissions check in the entry points to each app. For example:
check_app_permission = lambda request: permissions.check_app_permission("blog", request)
def view_blog(request, …):
check_app_permission(request)
…
(it might even be possible to do some magic and inject this check at the urls.py level…)
Additionally, I would create a has_app_permission template tag:
<div id="sidebar">
{% if has_app_permission "blog" %}
{% include "blog/sidebar_recent_posts.html" %}
{% endif %}
</div>
Or similar.
Finally, there are approximately a million ways you could implement the permission system… And without more information I wouldn't be able to comment. The simplest, though, would be something like this:
class App(Model):
name = CharField(…)
class AppPermission(object):
app = ForeignKey(App)
user = ForiegnKey(User)
def user_has_permission(user, app_name):
return AppPermission.objects.filter(app__name=app_name, user=user).exists()
I would avoid trying to do this with middleware, because if I understand the problem correctly, I expect you will (or, at least, I expect I would) end up spending a bunch of time building a generic framework which, in the end, would just have checks similar to those above.
Does that help? Is there something I can clarify?

Putting links in list_detail.object_list to list_detail.object_detail

I've started using Django and am going right to generic views. Great architecture! Well, the documents are great, but for the absolute beginner it is a bit like unix docs, where they make the most sense when you already know what you're doing. I've looked about and cannot find this specifically, which is, how do you set up an object_list template so that you can click on an entry in the rendered screen and get the object_detail?
The following is working. The reason I'm asking is to see if I am taking a reasonable route or is there some better, more Djangoish way to do this?
I've got a model which has a unicode defined so that I can identify my database entries in a human readable form. I want to click on a link in the object_list generated page to get to the object_detail page. I understand that a good way to do this is to create a system where the url for the detail looks like http://www.example.com/xxx/5/ which would call up the detail page for row 5 in the database. So, I just came up with the following, and my question is am I on the right track?
I made a template page for the list view that contains the following:
<ul>
{% for aninpatient in object_list %}
<li><a href='/inpatient-detail/{{ aninpatient.id }}/'>{{ aninpatient }}</a></li>
{% endfor %}
</ul>
Here, object_list comes from the list_detail.object_list generic view. The for loop steps through the object list object_list. In each line I create an anchor in html that references the desired href, "/inpatient-detail/nn/", where nn is the id field of each of the rows in the database table. The displayed link is the unicode string which is therefore a clickable link. I've set up templates and this works just fine.
So, am I going in the right direction? It looks like it will be straightforward to extend this to be able to put edit and delete links in the template as well.
Is there a generic view that takes advantage of the model to create the detail page? I used ModelForm helper from django.forms to make the form object, which was great for creating the input form (with automatic validation! wow that was cool!), so is there something like that for creating the detail view page?
Steve
If you're on django < 1.3 then what you are doing is basically perfect. Those generic views are quite good for quickly creating pages. If you're on django 1.3 you'll want to use the class based generic views. Once you get a handle on those they are are crazy good.
Only note I have is that you should use {% url %} tags in your templates instead of hardcoding urls. In your urls.conf file(s) define named urls like:
url('inpatient-detail/(?P<inpatient_id>\d+)/$', 'your_view', name='inpatient_detail')
and in your template (for django < 1.3):
...
In 1.3 a new url tag is available that improves life even more.

Flickr albums in django admin

I want to do the following:
Having a model (p.e. a model which handles data about photographic reports) create a section which has a preview of an specific flickr album. The URL will be provided by an URLField (until the first save the preview will not be available).
After the first save, it'll show previews of all the images inside that album, and make them selectable (through jQuery for example). Then again, when the images are selected and the object is saved (I think I can use django signals for this) it will notify a specific user telling him a selection has been made.
Is there any plugins available, or any easy way to implement this in django-admin?
Update: 22 days and no anwers... does that mean it can't be done in django-admin?
I personally can't think of any easy way to implement this in the Django admin, simply because I doubt many people who've done it have thought to open source it. I can imagine that it would be very specific to a certain user's / programmer's needs.
In any case, if you wanted to solve this issue, I'd say that your best bet would be overriding the Django admin templates in your django/contrib/admin/templates/admin folder. I believe you'd be best off by editing change_form.html.
My basic approach would be this:
Check the name of the model using opts.verbose_name. For example, if you wanted to do this processing for a model whose verbose name is "Gallery", you would do
{% ifequal opts.verbose_name "Gallery" %}
<!-- neat gallery view -->
{% else %}
<!-- regular form -->
{% endifequal %}
Make a custom template tag that will display the gallery view / form given the object_id and the type of object. This way you can replace the <!-- neat gallery view --> with a {% show_gallery object_id %}. See the Django Docs for more info on creating custom template tags. It's pretty straightforward.
Add whatever Javascript or custom stuff in your template tag template. What you choose to do is up to you.
Sorry you haven't gotten many more answers to your question. Hope this helps!