I am migrating my project from django 1.11.x to 2.0. I have everything going well till I got to urls. I happen to have an import like this
from cashondelivery.dashboard.app import application as cod_app
and I have my url pattern as
url(r'^dashboard/cod/', include(cod_app.urls)),
but I got the following error in my terminal
url(r'^dashboard/cod/', include(cod_app.urls)),
File ".../dev/lib/python3.6/site-packages/django/urls/conf.py", line 27, in include
'provide the namespace argument to include() instead.' % len(arg)
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.
I would really appreciate a fix.
cashondelivery->dashboard->app
import django
from django.conf.urls import url
from django.contrib.admin.views.decorators import staff_member_required
from oscar.core.application import Application
from . import views
class CashOnDeliveryDashboardApplication(Application):
name = None
default_permissions = ['is_staff', ]
list_view = views.TransactionListView
detail_view = views.TransactionDetailView
def get_urls(self):
urlpatterns = [
url(r'^transactions/$', self.list_view.as_view(),
name='cashondelivery-transaction-list'),
url(r'^transactions/(?P<pk>\d+)/$', self.detail_view.as_view(),
name='cashondelivery-transaction-detail'),
]
if django.VERSION[:2] < (1, 8):
from django.conf.urls import patterns
urlpatterns = patterns('', *urlpatterns)
return self.post_process_urls(urlpatterns)
application = CashOnDeliveryDashboardApplication()
You need to drop the include() and just pass the urls directly:
url(r'^dashboard/cod/', cod_app.urls),
The urls property returns a 3-tuple, not a list of urlpatterns, and support for passing this to include() was dropped in Django 2.
In django2 its path for normal url and re_path for url using regex.
path('dashboard/cod/', include(cod_app.urls)),
Related
I just have started my first project by using Django 2.0 in which I need to define a URL in a way as:
http://localhost:8000/navigator?search_term=arrow
But I couldn't know how to define a string parameter for a URL in Django 2.0
Here's what I have tried:
From ulrs.py:
from Django.URLs import path from. import views
urlpatterns = [
path('navigator/<str:search_term>', views.GhNavigator, name='navigator'),
]
Any help?
There is no need to define query params in URL. Below url is enough to work.
path('navigator/', views.GhNavigator, name='navigator')
Let you called URL http://localhost:8000/navigator/?search_term=arrow then you can get search_term by request.GET.get('search_term').
Request: GET
http://localhost:8000/navigator?search_term=arrow
urls.py
urlpatterns = [
path('navigator/', views.GhNavigator, name='navigator'),
]
views.py
search_term = request.GET.get('search_term', None)
urls.py file which is in 1.1 version of Django :-
urlpatterns = patterns('ecomstore.catalog.views',
(r'^category/(?P<category_slug>[-\w]+)/$','show_category',
{'template_name':'catalog/category.html'},'catalog_category'),
)
which I understood that first argument id prefix to all views. next argument is url which has four argument one is url string(regex),second is view , third is dict passing template name and fourth is location of category.
How to write it in Django 1.10
is following it correct way:-
from django.conf.urls import url
from ecommstore.catalog.views import *
urlpatterns = [
url(r'^category/(?P<category_slug>[-\w]+)/$','show_category',
{'template_name':'catalog/category.html'},'catalog_category'),
]
You're almost there. You've imported the view, but you're still passing in a string as the view instead of the view function itself:
urlpatterns = [
url(r'^category/(?P<category_slug>[-\w]+)/$', show_category,
{'template_name':'catalog/category.html'}, 'catalog_category'),
]
New python/Django user (and indeed new to SO):
When trying to migrate my Django project, I get an error:
RemovedInDjango110Warning: Support for string view arguments to url() is deprecated
and will be removed in Django 1.10 (got main.views.home). Pass the callable instead.
url(r'^$', 'main.views.home')
Apparently the second argument can't be a string anymore. I came to create this code as it is through a tutorial at pluralsight.com that is teaching how to use Django with a previous version (I'm currently working with 1.9). The teacher instructs us to create urlpatterns in urls.py from the views we create in apps. He teaches us to create a urlpattern such as the following:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', 'main.views.home')
]
to reference
def home(request):
return render(request, "main/home.html",
{'message': 'You\'ve met with a terrible fate, haven\'t you?'}) #this message calls HTML, not shown, not important for question
in the views.py of an app "main" that I created.
If this method is being deprecated, how do I pass the view argument not as a string? If I just remove the quotes, as shown in the documentation (https://docs.djangoproject.com/en/1.9/topics/http/urls/), I get an error:
NameError: name 'main' is not defined
I tried to "import" views or main using the code presented in this documentation:
from . import views
or
from . import main
which gave me:
ImportError: cannot import name 'views'
and
ImportError: cannot import name 'main'
I believe I've traced this down to an import error, and am currently researching that.
I have found the answer to my question. It was indeed an import error. For Django 1.10, you now have to import the app's view.py, and then pass the second argument of url() without quotes. Here is my code now in urls.py:
from django.conf.urls import url
from django.contrib import admin
import main.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', main.views.home)
]
I did not change anything in the app or view.py files.
Props to #Rik Poggi for illustrating how to import in his answer to this question:
Django - Import views from separate apps
You should be able to use the following:
from django.conf.urls import url
from django.contrib import admin
from main import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home)
]
I'm not absolutely certain what your directory structure looks like, but using a relative import such as from . import X is for when the files are in the same folder as each other.
You can use your functions by importing all of them to list and added each one of them to urlpatterns.
from django.conf.urls import url
from django.contrib import admin
from main.views import(
home,
function2,
function3,
)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/$', home),
url(r'function2/^$', function2),
url(r'^$', function3),
]
I'd like to redirect url pattern with variables from urls.py.
I refer other stackoverflow solution, but I don't know when url having a variable like following code.
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns(
url(
r'^permalink/(?P<id>\d+)/foo/$',
RedirectView.as_view(url='/permalink/(?P<id>\d+)/')
),
)
With this code, django will redirect /permalink/1/foo/ to /permalink/(?P<id>\d+)/, not the /permalink/1/.
Is there any solution without using views.py?
Of course I know solution using controller, but I wonder is there any simpler solution with using url pattern.
Passing url='/permalink/(?P<id>\d+)/' to RedirectView will not work, because the view does not substitute the named arguments in the url.
However, RedirectView lets you provide the pattern_name instead of the url to redirect to. The url is reversed using the same args and kwargs that were passed for the original view.
This will work in your case, because both url patterns have one named argument, id.
urlpatterns = [
url(r'^permalink/(?P<id>\d+)/foo/$',
RedirectView.as_view(pattern_name="target_view"),
name="original_view"),
url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]
If the target url pattern uses other arguments, then you can't use url or pattern_name. Instead, you can subclass RedirectView and override get_redirect_url.
from django.core.urlresolvers import reverse
from django.views.generic import RedirectView
class QuerystringRedirect(RedirectView):
"""
Used to redirect to remove GET parameters from url
e.g. /permalink/?id=10 to /permalink/10/
"""
def get_redirect_url(self):
if 'id' in self.request.GET:
return reverse('target_view', args=(self.request.GET['id'],))
else:
raise Http404()
It would be good practice to put QuerystringRedirect in your views module. You would then add the view to your url patterns with something like:
urlpatterns = [
url(r'^permalink/$', views.QuerystringRedirect.as_view(), name="original_view"),
url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]
I am trying to to redirect some of my Django views to https, using the SSLRedirect middleware.
I created the middleware, but I'm having trouble securing specific url paths as described in the middleware snippet. When I add {'SSL':True} to my view keywords, I get this syntax error: 'non-keyword arg after keyword arg'. My urls.py is
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import direct_to_template
from post.views import *
urlpatterns = patterns('',
url(r'^$', turk_post, name='post', {'SSL':True}),
)
Replace:
url(r'^$', turk_post, name='post', {'SSL':True}),
with:
url(r'^$', turk_post, name='post', kwargs={'SSL':True}),
The Django url is a function defined like this:
def url(regex, view, kwargs=None, name=None, prefix=''):
# et cetera
(hence your error as the function expects a keyword argument)