any method to mix these urls in django - python

the usl :
(r'^account/', include('account.urls')),
(r'^account/', include('django_authopenid.urls')),
i want to use these url , and i dont want to mix it by my hand one by one , it is not very easy ,
has a method to do this in django .
thanks

Your own code can work properly in many cases, but if not you can do
this:
Create your own urls.py file:
from account.urls import urlpatterns as accounts_urlpatterns
from django_authopenid.urls import urlpatterns as authopenid_urlpatterns
urlpatterns = patterns("")
urlpatterns += accounts_urlpatterns
urlpatterns += authopenid_urlpatterns

What you do will work, with the caveat that any pattern that would match an entry in both would be found by the first one you list.
For instance, the url /account/openid/foo/ might match a pattern in django_authopenid.urls that is r'^openid/(.*)/$', but that would never hit if there was a match in account.urls that also matched, such as r'^(.*)/foo/$'.

Related

NameError: name 'urlpatterns' is not defined using i18n_patterns

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.

URL Pattern with Primary Key not working properly

Need help with the regex in urls. I'm building a different app, not the one shown in the lecture above. To relate to my case with the lecture, School is Clients, and Students is categories.
In urls.py file, from url_patterns :
url(r'^(?P<pk>[-\w]+)/$', views.DetailClientList.as_view(), name='Detail_Client_List'),
This work correctly, with the address being http://127.0.0.1:8000/App1/cli1/, where cli1 is a Clients table primary key (one of the records).
But when I put the line below in url patterns (instead of the above)
url(r'^<str:pk>/$', views.DetailClientList.as_view(), name='Detail_Client_List')
I get the following error (same error for int:pk):
Page not found (404)
Request Method:GET
Request URL:http://127.0.0.1:8000/App1/cli1/
The resulting URL is the same in both cases described above. So where am I going wrong here. I'm guessing its an issue with the url pattern regex (although resulting URL is the same?).
Please help. TIA!
try with re_path instead of 'url'
from django.urls import re_path, path
1. re_path
re_path(r'^<str:pk>/$', views.DetailClientList.as_view(), name='Detail_Client_List')
2. path
path('<str:pk>/', views.DetailClientList.as_view(), name='Detail_Client_List')
You might misunderstood the url(...)--(Doc) with path(...)--(Doc)
So,
from django.conf.urls import url
urlpatterns = [
url(
r'^(?P<pk>[-\w]+)/$',
views.DetailClientList.as_view(),
name='Detail_Client_List'
),
]
is identical with the following,
from django.urls import path
urlpatterns = [
path(
'<str:pk>',
views.DetailClientList.as_view(),
name='Detail_Client_List'
)
]

Django URL complicated patterns and paths

I'm trying to generated nested url paths. So far I could only generate 1 level urls. When I try second level urls it doesn't get called anymore, it still only shows the first level url although the address bar on the browser does direct to the second level url. Is this possible or do I need to create a new app?
urls.py
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView, TemplateView
from dashboard.models import IPARate,PCPRate
from dashboard import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^medicare/', ListView.as_view(queryset=IPARate.objects.all().order_by("id"), template_name='dashboard/medicare.html')),
url(r'^medicare/medicarepcp/$', ListView.as_view(queryset=PCPRate.objects.all().order_by("id"), template_name='dashboard/medicarepcp.html')),
url(r'^medicare/', views.medicaid, name='medicare'),
url(r'^medicare/medicarepcp/$', views.medicarepcp, name='medicarepcp'),
]
You need to add a dollar (end of line character) to the r'^medicare/ regex, so that it matches /medicare/ but not other URLs like medicare/medicarepcp/.
url(r'^medicare/$', ListView.as_view(queryset=IPARate.objects.all().order_by("id"), template_name='dashboard/medicare.html')),
url(r'^medicare/medicarepcp/$', ListView.as_view(queryset=PCPRate.objects.all().order_by("id"), template_name='dashboard/medicarepcp.html')),
The third and the fourth regexes are the same as the first and the second respectively. Django will always match the first two URL patterns, so you need to change the third and fourth URL patterns to something else.
The order of URLs is important, as Django will react to the first URL pattern that matches the request. A good habit is to put all of your "sub" URLs first, then the top level of that section last, so it works as a fall-back if no others match (i.e. put your '^medicare/$' last).

Django, how to pass just the last segment of url to view

I'm attempting to pass a string from the end of a url to a view function, but I'm getting the entire url passed to render_form() i.e. if I input "myurl.me/prepend/identifier" I get "prepend/identifier/" when I just want "identifier"
These are my urls.py files, the top one is located in an app directory, the bottom one is in the project dir:
urlpatterns = patterns('',
url(r'^(?P<identifier>.+)$', views.render_form),
)
--
urlpatterns = patterns('',
url(r'^prepend/', include('the_ones_above.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I think it might have somehting to do with the split over 2 urlpatterns in 2 separate files, any ideas what im doing wrong?
Thanks!
edit: the view looks like: def render_form(request, identifier=None):
oh, and this is the latest django (from git)
2nd edit: removed the rather superfluous 1st url, behavior is still the same
I will suggest you to simply write you url like.
url(r'^(?P<identifier>[\w]+)/$', views.render_form),
[\w] will tell you that you can pass word character (A-Za-z0-9_).

How to define url which accept everykind of strings in django

In django, I defined url like that
(r'^checkstring/(?P<string>\w+)/$',views.check_str,name='check str')
But, When i enter string inputs like ibrahim.yilmaz, ibrahi!m or ibrahim#ibrahim.com, it returns http 404.
So how can i write the url which accept everykind of string?
any help will be appreciated.
İbrahim
Django uses regular expressions to match incoming requests. In python a dot (.) matches any character except a newline. See docs for more information and try:
(r'^checkstring/(?P<string>.+)/$',views.check_str,name='check str')
Also keep in mind that this will accept any character (including the forward slash) which may not be desirable for you. Be sure to test to make sure everything works as you would expect.
In Django >= 2.0, you can implement in the following way.
from django.urls import path
urlpatterns = [
...
path('polls/<string>/$','polls.views.detail')
...
]
For Django 2.0
import re_path in urls.py file
like this:from django.urls import re_path
then in the urlpatterns write the following code:
urlpatterns = [ re_path('prefixs/.*', your_ViewClass_name.as_view()), ]

Categories