I am trying to internationalize a Django dictionary application using the Django translation system. I have successfully translated most of the content on the site but I am having problems translating URL patterns, with the exception of the admin page.
My goal is to change the language of both content and URL based on prefix language code in the URL. For example:
www.example.com/en/dictionary/define // (content in English)
www.example.com/it/dizionario/definisci // (content in Italian)
www.example.com/fr/dictionnaire/définis // (content in French)
...
I checked the Django documentation on this (https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#translating-url-patterns) but found it quite vague and could not really understand it.
project-level urls.py:
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import gettext_lazy as _
from dictionary import views as dictionary_views
urlpatterns = [
path('admin/', admin.site.urls),
]
dictionary_patterns = ([
path('', dictionary_views.index, name='index'),
path(_('define/'), dictionary_views.define, name='define'),
path(_('define/<slug:headword_slug>'), dictionary_views.headword, name='headword'),
], 'dictionary')
urlpatterns += i18n_patterns(
path(_('dictionary/'), include(dictionary_patterns, namespace='dictionary')),
)
As for app-specific urls.py, I have no clue on what to do there. Should I import the dictionary_patterns variable from above and add it to the app's urlpatterns list? Should I delete the app's urls.py file altogether?
Can anybody help?
Just try to add import include in the urlpatterns like
In the project/urls.py
from django.conf.urls import include
path('en/dictionary/',include('app URL here with extension.url'),
path('it/dizionario/',include('app URL here with extension.url'),
path('fr/dictionnaire/',include('app URL here with extension.url'),
In the Appname/urls.py
urlpatterns = [
url(r'^define$/',views.name of view,name=''),
url(r'^definisci/$',views.name of view,name=''),
url(r'^définis/$',views.name of view,name=''),
Check this out:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Related
I'm playing with Django python tutorial and notice a very odd url behavior.
My project name is 'mysite', and in its urls.py, it by default has
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
]
This shows that little rocket base page when I visit localhost:8000.
Then I editted like following,
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('student/', include('student.urls'))
]
to indicate a new url for my own app, this time the base page localhost:8000 returns a 404. I'm very curious why my new line will affect the base page? BTW my django version is 3.0.6.
hey guys i have been learning Django for about 3 week now and i am just curious is there anyway to set custom url for main page(the first page shown when starting the server) because i try to do it like the code below but still it give me error if anyone know please help if not hmm...maybe that how Django operate i think haha. Thanks
// Django api
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/',include("useraccount.urls")),
]
//useraccount
from django.urls import path
from . import views
urlpatterns = [
path('login',views.login,name="login"),
]
if you want a url for home page you can do something like this in your main url
path('', HomeView.as_view(), name='home'),
So I am following a Django tutorial and I have reached the stage where I add my own URL patterns to the urls.py file.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url((r'^webapp/', include('webapp.urls'))),
url(r'^admin/', admin.site.urls),
]
the problem is that when I run the server Django doesnt even search for the pattern. I'm not sure what I'm doing wrong here
webapp.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Make sure the directory structure is:
app
app
urls.py
webapp
urls.py
You write about a file named webapp.urls.py that is unusable by django
You pass wrong arguments for url().
Wrong:
url((r'^webapp/', include('webapp.urls')))
Right:
url(r'^webapp/', include('webapp.urls'))
I migrated my Django project to an Ubuntu distant server. I'm using mod_wsgi in order to runserver and I have some questions about default page.
I'm really new in this domain and I apologize if my question is bad or useless ..
When I want to connect to my Django application, I have to write something like that :
http://172.XX.XX.XXX/Home/login/
If I write just :
http://172.XX.XX.XXX
I get :
Page not found
Using the URLconf defined in Etat_civil.urls, Django tried these URL patterns, in this order:
^admin/
^BirthCertificate/
^Identity/
^Accueil/
^Home/
^captcha/
^Mairie/
The current URL, , didn't match any of these.
My question is :
How I can define redirected url in order to write http://172.XX.XX.XXX in my browser and go directly to http://172.XX.XX.XXX/Home/login/ for example ?
This is urls.py file from my project :
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from BirthCertificate import views
from Identity import views
from Accueil import views
from log import views
from Mairie import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
Because I just want for example, write my IP adress in order to access to my Django application and not write all the time a complete url.
If you need some files (apache2 files, ...) please tell me which one I have to post there.
Include this in views.py file of your any app:
from django.shortcuts import redirect
def some_view(request):
return redirect('/Home/login/')
Suppose the view is in log app then,
Include this in your urls.py:
from log.views import some_view
urlpatterns = [
url(r'^$', some_view,name='index'),
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
One method is to use RedirectView by making the following changes to your urls.py:
from django.views.generic.base import RedirectView
urlpatterns = [
url(r'^$', RedirectView.as_view(url='/Home'), name='home'),
...
]
You can either do this in Django or Configure from your webserver. Django has redirects app go through https://docs.djangoproject.com/en/1.10/ref/contrib/redirects/ for more info. You just add the source and redirect urls in the redirects section of your django admin.
I have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.