I am doing internationalisation in django admin.I am able to convert all my text to the specific langauge.But i m not able to change the 'app' name
suppose
django-admin.py startapp test
this will create a app called test inside my project.Inside this app 'test' i can create many classes in my model.py file.But when i register my app 'test' in settings.py file.I am convert all the text in the locale of my browser but my app heading 'test' is not getting changed.How to change that any idea?
this is a well known problem in django and there is a ticket since 2006
a workaround for this would be to place all the appnames (with upper und lower-case) manually in your *.po-file.
to ensure django-admin will catch this, Replaced this:
<caption>
<a href="{{ app.app_url }}" class="section">
{% blocktrans with app.name as name %}{{ name }}{% endblocktrans %}
</a>
</caption>
with this:
<caption>
{% trans app.name %}
</caption>
maybe this snippet would help too?
Defining an i18n-ized name, or any other custom app name is not natively possible.
Check out:
Can you give a Django app a verbose name for use throughout the admin?
http://code.djangoproject.com/ticket/3591
As mentioned in the question and ticket, there are several workarounds.
Related
I use django template index.html to render the frontpage. It includes another template to create a link icon. This template url_icon.html includes another template icon.html. When passing the arguments down the way, I face with an error. How to fix it?
index.html
.
.
.
{% include "url_icon.html" with name="return" url="/" %}
.
.
.
url_icon.html
{% include "icon.html" with icon={{ name }} %}
icon.html
<img src="/static/images/{{ name }}.png" />
Causing an error:
Could not parse the remainder: '{{' from '{{'
it looks like there are a few things you can do to improve/fix this. Addressing #1 and #2 should fix your issue. I've also added suggestions for best practices that would probably require refactoring (#3, #4).
It looks like you need to remove the curly-braces from name inside the {% include %} tag. Context variables can be used inside tags without extra syntax.
url_icon.html:
{% include "icon.html" with icon=name %}
icon.html will have access to name since you're not using the only keyword when updating its context, so your code might appear to work at first ({% include %} documentation). However, it looks like your intention is to refer to it as icon.
Use the variable icon in instead of name
icon.html:
<img src="/static/images/{{ icon }}.png" />
Optional suggestion: Use Django's staticfiles system
Try using the {% static %} tag for your icon. This will help make deployment easier, especially if you use a separate CDN from your webserver. There's lots of literature on how to set up staticfiles for Django projects in production, it's a large topic, but you'll be able to approach it more easily if you use the {% static %} tag from the beginning.
Optional suggestion: Django's URL routing system
Your route in index.html is hard-coded to be "/". Django has a powerful URL referencing system to leverage. If you've defined the root URL / using Django too, you can refer to it by name. Docs: {% url %}, and for the back-end, reverse().
I've got a site with two languages (it will get more in time) and a little dropdown menu to switch languages. It works as desired/expected in the development server. Urls look like this when visiting the site:
localhost:8000/en/home
localhost:8000/pl/home
The django project gets deployed on a server (Apache w/ mod-wsgi) at a subdirectory location, lets say:
mysite.com/django
Everything works as expected, even the admin site, etc, underneath that subdirectory, however, my little menu to change languages doesn't work anymore. When I change the language, the page reloads, but on the same language that it was on when I tried to change it. I can go back and forth between the languages by manually changing the url in the browser and all the pages work as expected with the translations; it's just the dropdown button that doesn't work anymore.
mysite.com/django/en/home
mysite.com/django/pl/home
At first, I thought it was my button (pretty standard off a tutorial):
<form action="{% url 'set_language' %}" method="post" class="form-inline">{% csrf_token %}
<div class="form-group mx-sm-3 mb-0">
<input type="hidden" name="text" value="{{ redirect_to }}" class="form-control form-control-sm">
<select name="language" id="langselect" onchange="this.form.submit()">
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}" {% if language.code == LANGUAGE_CODE %} selected {% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
</div>
</form>
but actually if I change the Apache config to deploy the site at server root mysite.com (just to try to isolate the problem) the dropdown button, translations, and everything else work as expected. I think this means that somewhere the i18n is generating a bogus link, i.e. it's not implementing the '/django' subdirectory prefix the right way.
I feel like this should be an easy fix —a setting to toggle somewhere— but I'm stumped after spending most of the day googling and reading documentation. I'm happy to share snippets of settings, etc, but honestly, I'm not sure what's relevant.
Could someone please point me in the right direction with a keyword or a suggestion how to solve this?
Edit 1:
I tried to add the prefix to value in the <input> tag. So it read value="/django{{ redirect_to }}" but that didn't work either.
Edit 2 & 4:
Using my switcher button with the network inspector open reveals that the setlang function is calling the wrong url.
mysite.com/django/django/i18n/setlang
I still don't know how to fix it, but I'm fairly certain that's the problem.
If I change the apache config to deploy at mysite.com the redirect is via mysite.com/i18n/setlang, so it seems like I need to somehow control how the django subdirectory prefix is interpreted. But HOW?!?!?
Edit 3:
The switcher successfully changes the cookie, despite not reloading to the proper language.
So after much frustration, I have a working solution.
I had to first create a custom filter that would cut the language code from the url. Since the existing code successfully updated the language preference cookie, and since navigating to any path without a language code automatically / successfully appends the language path, I could redirect with my switcher to a path without the language code and the site will successfully render according to the user's browser preferences or cookie. Cutting out the language code from the url seemed to be the most reasonable way for now.
Add a set up infrastructure for custom template tags according to the documentaiton here. I added a templatetags directory to my app, and inside an __init__.py file and a <myapp>_extras.py file
In my case, I needed to cut the second element from the full url path, so my filter function in <myapp>_extras.py. The file looks like this:
from django import template
register = template.Library()
#register.filter
def custom_redir_lang(url_fullpath):
ls_urls = url_fullpath.split('/')
del ls_urls[1]
return '/'.join(ls_urls)
Then I change the value of the input tag in my template from "{{ redirect_to }}" to "{{ request.get_full_path|custom_redir_lang }}"
Now the site redirects properly underneath the subdirectory prefix.
In my base.html I have:
blabla
{% ifequal alterprofile no %}
{% include 'registration/submittedprofile.html' %}
{% else %}
{% include 'registration/submittedprofile2.html' %}
{% endifnotequal %}
blabla
In views.py I have alterprofile = "no".
How do i change alterprofile to "yes". This is my submittedprofile:
<form action="" method="get">
blablabla
<input type="submit" value="Make Changes">
</form>
And this is my views.py:
def userprofile(request):
alterprofile = "no"
username = request.user
return render_to_response('registration/userprofile.html', {'user': username, 'alterprofile' = alterprofile})
Is there anyone who can code the answers for me. I've tried playing round with the previous answers but to no affect.
Django variables are rendered from the server side, so you can not change the variable after it was passed to your template. What you want to achieve is done via frontend scripting.
In this case you would pass both variables to the django template, save them in your Javascript and then switch them once you clicked the button you mentioned (via onClick event handling).
You can use url arguments like:
/myurl/
/myurl/?show2
then, in your views.py you can use request.POST['show2'] to check if exists and then send a variable again to the view to be checked with your {if}s
As an aside note, either you don't understand basic request flow with web applications or you are not explaining properly what you mean with "html button", so you are not fluent with html language. Sorry if my intuition is harsh or wrong.
its not that clear what you are asking but here is how to make the logic work as I think you want it based on yoru submitted code:
in your template change it to
ifequal alterprofile "no"
to include registration/submittedprofile.html.
When you change the view to alterprofile = "yes" the registration/submittedprofile2.html will be included instead if you keep your current template logic.
This is because in your view, alterprofile is assigned a string therefore its always a string. When you tried to test against no instead of "no" django was looking for a variable called no which doesn't exist.
This means that everytime you run it would have always included registration/submittedprofile2.html
I am trying to install django-photologue. Everything seems ok, because I install and set up following the official guidelines. I have to upload some photos as examples. However, when viewing a photo or gallery details , then an error as follows:
Caught an exception while rendering: 'Photo' object has no attribute 'get_thumbnail_url'
I tried to remove the following code from the file photo_detail.html
{% if object.public_galleries %}
<h2>This photo is found in the following galleries:</h2>
<ol>
{% for gallery in object.public_galleries %}
<li>{%previous_in_gallery object gallery%} {{ gallery.title }} {%next_in_gallery object gallery%}</li>
{% endfor %}
</ol>
{% endif %}
No more errors, but pictures do not show up. If you click on the link will still lead to correct photographs to see. I think the problem in:
{{ object.get_display_url }}
It is totally not return any value.
Please help me solve this problem. Thanks!
Did you run python manage.py plinit after install and opt to create both a thumbnail and display photosize? These photosizes need to be defined in your database.
In other versions, you have to edit photologue/templates/photolog/tags/next_in_gallery.html and replace
{{ photo.get_thumbnail_url }}
with
{{ photo.thumbnail.url }}
Same for photologue/templates/photolog/tags/prev_in_gallery.html.
Honestly from looking at the source, it looks like a bug in the project. If you search the source, thumbnail doesn't seem to be a field within the Photo class (get_FIELD_url is an easy way to access an ImageField's url btw.) So I would recommend tinkering with the source or finding another project. I might be wrong though but that's what my ~5 minute scan of the project found.
I would like to add a tool link at the top of my admin change_list.html, which I have already done, and have this link basically be able to produce some sort of printable document version of my models data based off of my current filter settings. Basically a print button in the admin change_list.html.
so far I have overridden the change_list.html to create the link, and I notice that this
<li>
<a href="{{ choice.query_string|iriencode }}" class="addlink">
{% blocktrans %}View PDF{% endblocktrans %}
</a>
</li>
gives you a link based on these choices.. but Im kinda lost as to the best/easiest way to do this..
Sorry, new to Django. I know I can use ReportLabs to generate pdfs, but not a 100% on how to get the filtered data from change_list to it.
A bit late, but for those who might be searching "in the future" like me, this might be helpful: http://djangosnippets.org/snippets/1842/