How the url internationalization should be made? - python

I have a question regarding the internationalization of a website's URL addresses.
The question is how to make it properly?
What I would like to do is something like this.
Assume you have an URL in your address bar
www.mydomain.com/en/book
then if you type www.mydomain.com/de/book you will be redirected to a german version of a site so the url will change to www.mydomain.com/de/buch and so on.
Idea:
I thought about making a table that would be used as a dictionary between different languages. For the website content I would still use i18n, but I think that for urls it cannot be applied.
The implementation:
I thought about modifying routing.py in a way that before any connection is invoked I would recognize in the URL a language that user wants, and store it in a session variable for later use with i18n. Then I would redirect user to a correct url for this lanugage and invoke a poper controller/action.
Any ideas, suggestions or recomendations are welcome.
Edit (2011-04-04 18:35):
I have just reminded myself that I have already encountered a similar problem on Opera Blog;
there is a problem on StackOverflow: Pylons application Internationalization, but it deals solely on site internationalization, no problem with URLs included;
WZeberaFFS has pointed our a small issue that may arise if no reference or previous language settings are taken into account;
Additional question: What should be stored in database, to make it work fast? I have my own idea, but I will give it a try later on.

I believe your implementation will work as log as you don't have the same name of to different pages like if you have a page called www.mydomain.com/en/music and if music in German is book, then the problem will be that if you want to go to www.mydomain.com/fr/music then you don't know if you are coming form /de/ or /en/. But this is only a problem if you have colliding names on different pages cross languish it will still work if you have the same name for a word in 2 different languish you wont really know where they cam form but you will know where they are heading.
If you have that problem then you have to save a session or hope that the client sends a referral.

I have no idea how it could be done in Python, but usually people do such things through some kind of URL Re-writer or URL filter - something that will consume your URL and re-direct to proper page setting Locale at the same time.

Related

Flask url_for on external URLs

I have been researching this for quite a while and it seems that I cannot get my head around it. I have different ideas but all of them looks like they are not the right one to solve this problem.
I have an application in flask that does different things like for example:
loading an image (user profile image)
calling APIs (to perform specific tasks, like for example adding a comment to a forum post)
etc.
The way I am doing all of this is by using url_for in this way:
client_info = requests.post(url_for('api.read_comments', _external=True),
json={'client_name': client}).json()
So my application is at "https://www.myapp.com/comments" - The "url_for()" in my code will basically generate this url: "https://www.myapp.com/api/read_comments".
All comments are then retrieved by my API code.
This works just fine, but what I would like to do is to have the Frontend on "https://www.myapp.com/" but the Backend on "https://backend.myapp.com".
So my question is, how can I make my url_for() as dynamic as possible and able to access content on other subdomains/servers that I own?
Any thoughts? Or additional methods/functions that I should be using?
For example all comments will be located at "comments.myapp.com", all my profile details are on "profiles.myapp.com", etc. This is just a general idea on how the system can be setup. Any thoughts?
Thanks a lot and I look forward to hearing from you.

Django Multi-site with shared database

I am about to develop multiple sites for different real estate companies. All share the same html, sections, etc. The difference is in the content, specially the properties... But some of those properties can be shared among the rest of the companies.
I am thinking in sharing the same database and differentiate content using the url. In this way I can use only one project instead of one for each company.
Does anyone have recommendations for this kind of projects?
Thanks,
I have done that.
Was it a good idea? Yes, in my case it was. I had to reuse the same content and when we changed the content, it had to be changed on all pages. On a simple site, a triple deploy and changing the content in three different projects is kind of overkill. But whereas it works fine in a simple front-end page (that hardly even requires Django), I do not recommend it for "real" web apps.
What will break? Think about the things that your pages will share and see if it's a problem.
1) I'm guessing that if you'll want to have user login capability on the page (besides the admin login), then that's a problem, if I can use the same user for different companies that have no apparent connection whatsoever. You could be in for a lot of trouble if the companies find out that user private details aren't as private as they thought. And the same goes for the users who really don't have a clue how they ended up with a user account on a page they've never visited.
2) URLs. You can't have different ones for each company without some extra hacking. If one of the companies wants to have /about/ and the other one /company/ page, you're gonna start hacking a bad solution that will blow up in your face when the companies ask for the next page.
3) Anything else you might want to have on your page that is connected to hardcoded data or database values. I.e. social authentication etc.
What can you do about it?
If I was hellbound on solving the first one, here's what I would do:
- Override the user model and add info about the registering page
- Create custom managers for user model for each page
- Write a middleware that only lets you use the page-specific manager for the current request
All in all, I wouldn't do it in a million years. Way too hacky, way too vulnerable. Just create separate databases.
For solving the second one, you can create a multi-host middleware that checks from which domain the request comes from and returns the correct URL config. Sth similar to this . It's not really hard to rewrite and modify to your needs.
It's impossible to decide for you, but I've given you something to think about before going one way or the other. Good luck!

Django figure out which template rendered the view you are seeing

So, I've recently inherited a large code base that is fairly obfuscated. When I navigate a page on my local machine is there any way to determine which template/view is actually being called to create the view that I'm seeing at that moment?
I would like to get a better idea of where certain parts of the page are actually coming from, but the project is so large and disorganized that going through present templates is simply not feasible.
Is there any nice way to get around this? Worth mentioning that the defined urls all seem to be poorly written, obfuscated regex, (not to mention incredibly long) so direct examination of the urls file is not extremely feasible.
When I try to run resolve on the url of the page I'm trying to view I get a 404, and I'm not really sure where to progress from there, since the page clearly works.
Any help would be greatly appreciated.
Personnaly I use this : https://github.com/django-debug-toolbar/django-debug-toolbar
The Django Debug Toolbar is a configurable set of panels that display
various debug information about the current request/response and when
clicked, display more details about the panel's content.
Currently, the following panels have been written and are working:
Django version
Request timer
A list of settings in settings.py
Common HTTP headers
GET/POST/cookie/session variable display
Templates and context used, and their template paths
SQL queries including time to execute and links to EXPLAIN each query
List of signals, their args and receivers
Logging output via Python's built-in logging, or via the logbook module
There is also one Django management command currently:
debugsqlshell: Outputs the SQL that gets executed as you work in the Python interactive shell. (See example below)
If you have ideas for other panels please let us know.
Note: The Debug Toolbar only works on Django 1.3 and newer.
0 code to add, only a few minor changes to settings.py
You will get what you want and even more.

Huge Django project

I have a new job and a huge django project (15 apps, more than 30 loc). It's pretty hard to understand it's architecture from scratch. Are there any techniques to simplify my work in the beginning? sometimes it's even hard to understand where to find a form or a view that I need... thnx in advance.
When I come to this kind of problem I open up a notebook and answer the following:
1. Infrastructure
Server configuration, OS etc
Check out the database type (mysql, postgres, nosql)
External APIS (e.g Facebook Connect)
2. Backend
Write a simple description
Write its input/output from user (try to be thorough; which fields are required and which aren't)
Write its FK and its relation to any other apps (and why)
List down each plugin the app is using. And for what purpose. For example in rails I'd write: 'gem will_paginate - To display guestbook app results on several pages'
3. Frontend
Check out the JS framework
Check the main stylesheet files (for the template)
The main html/haml (etc) files for creating a new template based page.
When you are done doing that. I think you are much more prepared and able go deeper developing/debugging the app. Good luck.
Use this http://packages.python.org/django-extensions/graph_models.html
to generate the Relationship diagrams from the models so that you can visually see how the models are related to each other. This will give you nice idea about the app
1) Try to install the site from scratch. You will find what external apps are needed for the site to run.
2) Reverse engineer. Browse through the site and try to find out what you have to do to change something to that page. Start with the url, look up in urls.py, read the view, check the model. Are there any hints to other processes?
3) Try to write down everything you don't understand, and document the answers for future reference.
I would clone the project so you can mess up endlessly.
Then I would start to reduce the code. "What happens if if just remove this function here?
Also get django debug toolbar:
https://github.com/django-debug-toolbar/django-debug-toolbar
A good terminal debugger is also golden, there are many out there, here is an example:
https://github.com/tomchristie/django-pdb
This allow you to halt the code and even inject and mutate parameters in runtime. Just like GDB in C.
If you use FireFox you can install FireBug on it and when you for example submit ajax form you can see at which url send you request after what you can easily find controller which work with this form data. At chrome this utility embedded by default and call by F12 key.

Django - Managing page content in Django Admin

I'm new to working with Django and am developing for a client who wants to be able to change page content in the Django Admin. They need to be able to change the html of the index page without editing the files on the server.
I know about flatfiles but I'm not sure that's completely what I'm after as I can't display stuff such as Django forms for example.
EDIT: Kind of like how a CMS works but without the users/group stuff and be able to use Django View modules such as forms.
Any advice?
Thanks
Honestly, the scope of what you're looking for is too huge to cover in this format. There's a number of ways this could be done, but they're all going to require some work and customization based on the client's needs.
Flatpages could work if you allow HTML content and make sure the content is rendered as "safe" in the template. This really only covers the "content" area of the site, though. It wouldn't be wise to use flatpages for an entire site template, including header, sidebar, footer, etc.
You could create editable areas. So, you actually create models for things like headers, sidebars, footers, and modules within those areas, and then just pull them into the template as needed. Then, the client is only editing pieces of the template instead of responsible for the whole HTML document.
Forms are going to be a challenge, because they require backend-processing that requires a connected view. The client won't be able to just arbitrarily drop in some form code and have a form. But, you could use a third-party service form forms and just embed them in the available content regions. Or, there's a couple of django apps that try to implement a type of "form builder" in the admin. That might somehow let the client add a form via something like the shortcodes used in Wordpress, but you'd likely have to lay down some infrastructure to make that work.
At a certain point, stuff like this reaches a point of diminishing returns, though. The only way to allow total customization of the template is to drop down into the actual physical file and make changes there. You can make certain things easier for the client, but ultimately, they either need to scale back their customization needs or deal with the fact that they'll have to work with the filesystem.
I don't believe that is possible at this time. Of course you can edit your models but templates, I think not.
I would find out how much they need to change? If they plan a complete redesign every week then you're still looking for an answer. If they just need a dynamic front page then you can split it up into variables and let them edit sections of html. Much safer and less prone to breaking the html.

Categories