问题
I have a Django Website that I am trying to internationalize. Until now it looked like this:
Homepage:
www.myhomepage.com
Another page:
www.myhomepage.com/content/cities
Now I am trying to make it like this:
Homepage:
www.myhomepage.com/en
www.myhomepage.com/de
Another page:
www.myhomepage.com/en/content/cities
www.myhomepage.com/de/content/cities
Following this and this, I managed to make the homepage work, so with www.myhomepage.com/en
I see the homepage in English and with www.myhomepage.com/de
I see it in German.
The problem comes when I want to go to any other page, like www.myhomepage.com/en/content/cities
. Then, the page rendered is still the homepage. Before changing any settings to internationalize it www.myhomepage.com/content/cities
was showing up properly.
My guess is that the problem is with the view rendering or the url, but I do not manage to make it work.
Note that the view for www.myhomepage.com
belongs to one app and the view for content/cities
belongs to a different app.
This is the code I have:
settings.py
MIDDLEWARE_CLASSES = [
...
'django.middleware.locale.LocaleMiddleware',
...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.i18n',
],
},
},
]
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en-us'
USE_I18N = True
Main app:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += i18n_patterns('',
url(r'^content/', include('content.urls', namespace='content')),
)
views.py
from django.shortcuts import render
def home_view(request):
...
context = {
...
}
#print('request home', request)
return render(request, 'home_template.html', context)
By activating the print statement and loading www.myhomepage.com/en/content/cities
, the following is printed in the console: request home: <WSGIRequest: GET '/en/content/cities/'>
, even though this view belongs to the home_page.
Content app:
urls.py
from .views import countries_and_cities
urlpatterns = [
...
url(r'^cities/$', countries_and_cities),
...
]
views.py
from django.shortcuts import render
def countries_and_cities(request):
...
context = {
...
}
return render(request, 'cities_template.html', context)
I have also tried what it is suggested in the docs, without success.
urls.py
from main app:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
from content import views as content_views
content_patterns = ([
url(r'^cities/$', content_views.countries_and_cities, name='cities'),
], 'content')
urlpatterns += i18n_patterns('',
url(r'^content/', include(content_patterns, namespace='content')),
)
What am I doing wrong?
回答1:
I finally found the problem.
There was another url defined as:
urlpatterns += i18n_patterns(
...
url(r'', include('main.urls')),
...
)
This was causing that the problem, even if before going for internationalization it was working properly.
I have just changed it to:
urlpatterns += i18n_patterns(
...
url(r'^$', include('main.urls')),
...
)
And it is working properly.
来源:https://stackoverflow.com/questions/44502117/url-rendered-as-home-page-after-internationalization