NameError: name 'urlpatterns' is not defined using i18n_patterns - python

I have problems writing the urls for translation. According to this question I understand that it is because I have += so I need to put this = the bad thing is that I have to translate all my urls I can't leave any outside of i18n, what can I do to include all my urls there?
from . import views
from django.urls import path
from django.conf.urls.i18n import i18n_patterns
app_name='Clientes'
urlpatterns+= i18n_patterns(
path('',views.list_clientes,name='clientes_list'),
path('add',views.create_clientes.as_view(),name='clientes_add'),
path('edit/<int:pk>',views.edit_clientes.as_view(),name='clientes_edit'),
path('<int:pk>/',views.detail_clientes.as_view(),name='clientes_detail'),
path('delete/<int:pk>',views.eliminar_cliente.as_view(),name='clientes_delete'),
)

You did not define urlpatterns before. If you want to translate all paths, you use:
# đź–ź assignment
urlpatterns = i18n_patterns(
path('',views.list_clientes,name='clientes_list'),
path('add',views.create_clientes.as_view(),name='clientes_add'),
path('edit/<int:pk>',views.edit_clientes.as_view(),name='clientes_edit'),
path('<int:pk>/',views.detail_clientes.as_view(),name='clientes_detail'),
path('delete/<int:pk>',views.eliminar_cliente.as_view(),name='clientes_delete'),
)
In the linked answer, it first defines a list, and then extends it with +=, exactly because not all url patterns are translated there. But if all patterns should be translated, you assign the result of i18n_patterns(..) to the urlpatterns.

Related

How to use namespace in django + Problems after this

I looked at the official Django documentation(https://docs.djangoproject.com/en/3.2/topics/http/urls/#reversing-namespaced-urls) and the error code and wrote the following. How can I write namespaces to work?
views.py:
from django.urls import path, include
app_name = 'home'
urlpatterns = [
path('math/', include('math_note.urls', namespace='math-note'))
]
templates:
math note
Error when running runserver:
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
Question:
How can I use the namespace function?
Second question:
Thank you! Well connected via
<a href="{% url 'music:home-page' %}">(to app_name = music)
But when I call the form from the music app and load the url to generate, I get an error. The other URLs didn't have any issues, only this one.
Reverse for 'home-page' not found. 'home-page' is not a valid view function or pattern name.
#music/urls.py
from django.urls import path
from . import views
app_name = 'music'
urlpatterns = [
path('', views.music_home_page, name='home-page'),
path('new/', views.new_song, name='new'),
path('music_player/<int:id>/', views.music_player, name='music-player'),
]
Question:
I solved the previous problem and got the following problem. What is the cause and solution of this?
The namespace kwarg that include accepts is an instance namespace i.e. an instance of an application namespace, you cannot have instance namespaces without having an application namespace which as the error specifies you do by specifying app_name in the included module or by passing a 2-tuple containing the patterns and application namespace to include.
Hence you need to modify math_note.urls and add an app_name variable to it:
app_name = 'math_note'
urlpatterns = [
...
]
Keep your other urls (likely in home.urls?) as it is:
from django.urls import path, include
app_name = 'home'
urlpatterns = [
path('math/', include('math_note.urls', namespace='math-note'))
]
just remove namespace from the path.
including a url does not require to have namespace. having your namespace on your math-notes is enough.
views.py
from django.urls import path, include
app_name = 'home'
urlpatterns = [
path('math/', include('math_note.urls', name='math-note'))
]
Use name instead of namespace
app_name in application url will work as namespace. just remove namespace from include in root project directory.
your urlpatterns should look like:
urlpatterns = [
path('math/', include('math_note.urls'))
]
and define app_name='home' in your application urls.
something like:
`app_name='home'`
urlpatterns = [
path('list/', <your view>, name='app-list'),
]
second question answer.
I have set the navbar so I can't see the exact error code content. When I deleted the navbar, I saw the exact error content, and the cause of the error was the return link.

Django URL translation dependent on URL prefix language code

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/

NameError-- name 'logs' is not defined. Django-Python

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^logs/', include(logs.urls)),
]
I have written this code in main/urls.py file
code in logs/urls.py is below:-
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$/', views.index, name='index'),
]
and i am accessing http://localhost:8000/logs
Error:- NameError name 'logs' is not defined
You need to import the module you are actually referencing before adding it to your URLs file.
Assuming logs is in your import path:
import logs
Usually you would use a string with include so that you don’t have to import the module;
url(r'^logs/', include('logs.urls')
Also, you should remove the slash from the end of your regex for the index view. The dollar marks the end of the string, so r'^$/' will never match.
url(r'^$', views.index, name='index'),
Please check the docs. It should help!
include() function takes a list/tuple of url(), path(), re_path() and includes them to django routing system.
It also requires app's name: you can define it in include(("enter here your app's name", app.urls))
or in an app itself, in urls.py at a modular level. app_name = 'enter here your apps'name '
But only include() isn't enough, you have to define it in path() or smth that is used for routing (e.g re_path(), url() (deprecated in django 2.0))
For full details: include() function.
So, the full poiting to app's urls look so:
from django.conf.urls import url, include
from . import views
from . import app
urlpatterns = [
url(r'^', include(("app", app.urls)), name='index'),
]
I recommend you to use path() and re_path() if you use Django 2.0 >
I noticed that you use regex incorrectly little bit
url(r'^$/', views.index, name='index'),
You type $ before /, it's incorrect, $ means the end of expression and must be used in the end of expression.
Unfortunately I can't tell you all the details in the post. You should read Django docs. Go through django tutorial for start.
I'm sure it will help!

Django urls.py: url doesn't work properly

I'm developing a webpage with Django.
I want to add/define an url for the application.
At this moment the url (dsvd/) doen't work properly. It shows only table data but no css and background.
here is the code for the main urls.py file.
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'kleedkamer_overview.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('kleedkamer_overview.urls')),
url(r'^dsvd/', 'kleedkamer_overview.views.indeling'),
)
and here the code inside the application urls.py file
from django.conf.urls import patterns, url
from kleedkamer_overview import views
urlpatterns = patterns ('',
url(r'^$',views.indeling, name='indeling'),
)
Is there anyone who can help me find the problem?
Greetz.
It looks like line is giving you trouble:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
It seems like you are trying to map this URL to kleedkamer_overview.views.indeling but the URL regrex does not end with a '$'. Urls.py files that are mapped to other apps look like this:
url(r'^admin/', include(admin.site.urls))
Notice the include function call and that the regrex expression r'^admin/' does not end with a $
However mapping to a specific view is a bit different and it looks like this:
url(r'^$', 'kleedkamer_overview.views.home', name='home')
Notice that this time, where the include() function call would go you are instead telling Django which specific view you want to use and that the site regrex is r'^$' ending with a $.
Try changing this:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
to this:
url(r'^dsvd/$', 'kleedkamer_overview.views.indeling')
Edit:
I saw your comments and while the urls.py is not the source of your problem, what I said is still valid because the urls.py was malformed. You should not even have the offending line there at all because you have already included the urls.py from kleedkamer_overview , there is no need to include it twice. It isn't DRY and it is just bad practice in general. This is why this still works despite being malformed because sites are looked for in order, in this case first it looks for /admin then / and does not reach /dsvd because it was already caught by / and your malformed url mapping is NEVER reached.

Django: The included urlconf core.urls doesn't have any patterns in it

I'm having some weird issues with class-based-views and reverse_lazy.
Following error shows up when calling the website:
ImproperlyConfigured at /dashboard/student/
The included urlconf core.urls doesn't have any patterns in it
My views.py:
class DashStudentMain(TemplateView):
model_class = None
template_name = 'learn/dashboard/snip_student_1.html'
tab_list = {
("Main", reverse_lazy('dash_student_main_url')),
#("History", reverse_lazy('dash_student_main_url'))
}
active_tab = "Main"
My core.urls:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.views.generic import RedirectView
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns(
'',
url(r'^$', 'core.views.home', name='home_url'),
url(r'^home', 'core.views.home'),
url(r'^dashboard/', include('tc_learn.dashboard.urls')),
...
)
My tc_learn.dashboard.urls:
from django.conf.urls.defaults import patterns, url
from .views import DashStudentMain, DashStudentHistory
urlpatterns = patterns(
# Student + Tabs
url(r"^", DashStudentMain.as_view()),
url(r"^student/$", DashStudentMain.as_view(), name="dash_student_main_url"),
url(r"^student/history/$", DashStudentHistory.as_view(), name="dash_student_history_url"),
I've
restarted the server, to make sure urls were loaded properly
commented out ("Main", reverse_lazy('dash_student_main_url')) to make sure that the urls.py syntax is fine
deleted the line url(r"^", DashStudentMain.as_view()), since it's not used anyway, but without it /dashboard/student doesn't work at all..
Any idea what I might be missing? Thanks!
EDIT:
It looks like the issue is coming from the tab_list object.
When I directly assign the object via tab_list = reverse_lazy('dash_student_main_url'), the code works fine. When I use it inside a list, it's showing that error. Does anyone know of work-around for this scenario?
Change this code:
tab_list = {
("Main", reverse_lazy('dash_student_main_url')),
#("History", reverse_lazy('dash_student_main_url'))
}
to:
tab_list = [
("Main", reverse_lazy('dash_student_main_url')),
#("History", reverse_lazy('dash_student_main_url'))
]
Contrary to a name you gave the variable, you were not creating a list, but a set. Elements were evaluated immediately at the time of set creation, because sets need to know more about values they contain. Changing it to a proper list will allow the elements to be evaluated lazily, as intended.
In tc_learn.dashboard.urls: you are missing the first argument (empty prefix in your case). Change it to:
urlpatterns = patterns(
'',
url(r"^", DashStudentMain.as_view()),
url(r"^student/$", DashStudentMain.as_view(), name="dash_student_main_url"),
url(r"^student/history/$", DashStudentHistory.as_view(), name="dash_student_history_url"),
)
Also, the first regex should be r"^$" if you want it to represent an empty one
And see if it works. Let me know!

Categories