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'),
]
Related
I am learning django and I am trying to add a new url to '/'
here is my urls.py:
from django.contrib import admin
from django.urls import path
from blog import views as blog_views
urlpatterns = [
path(r'^$', blog_views.index),
path('admin/', admin.site.urls),
]
and here is the index method from blog/views:
def index(request):
return HttpResponse("Hey There")
But when I go to '/' route, I get a 404 response. It seems that it fails at import from blog import views as blog_views in urls.py.
What is going wrong?
Here is my project structure:
Here is the error I get:
In Django 2.x there is a path function instead of django's 1.x url function
the path function doesn't accept Regular Expressions it only accepts normal text
So to make a url for the home page with path function you only need to write your url path like this :
urlpatterns = [
path('', blog_views.index), # http://localhost/
path('admin/', admin.site.urls),
]
read more about Django 2.x urls here:
https://docs.djangoproject.com/en/dev/ref/urls/
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)),
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)
This is just an academic question. I don't intend this for production but can you put a view function above your url routes in your url.py file? Something like:
from django.conf.urls import patterns, include, url
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("Hello World!")
urlpatterns = patterns('',
url(r'^$', 'hello_world'),
)
Yes, the view can be in your urls file, although I wouldn't recommend it as a way to organise your Django project.
However, in the url() you should use the view itself, not a string.
urlpatterns = [
url(r'^$', hello_world),
]
Providing string view arguments e.g. 'myproject.urls.hello_world' is deprecated in Django 1.8 and removed in Django 1.10.
The view argument can be any callable, you can even use a lambda function (again, I wouldn't recommend it).
urlpatterns = [
url(r'^$', lambda req: HttpResponse("Hello World"),
]
I've got to test URLs that look like this:
http://127.0.0.1:8000/payment/pay/513623c9/
but I'm not sure how to say this to the reverse() function. This is my urls.py
from django.conf.urls import patterns, url
from payment import views
from payment.msn import *
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^msnstats/', MsnStats.finish_transaction, name='finish_transaction'),
url(r'^pay/(?P<payment_id>\w+)/$', views.pay, name='pay')
)
And this is what the relevant part of my views.py looks like:
def pay(request, payment_id):
try:
plan=PaymentPlan.objects.get(payment_id=payment_id)
The payment_id is generated for each plan, so I first create a plan, get its payment_id from the database, and somehow call it. I'm just not sure how to use reverse.
from django.core.urlresolvers import reverse
url = reverse('pay', args=[plan.payment_id])
or
url = reverse('pay', kwargs={'payment_id': plan.payment_id})
Both versions are valid.
UPDATE: If you include the payment.urls with namespace argument then you have to add this namespace to the url name in the reverse() call:
project/urls.py:
url(r'^payment/', include('payment.urls', namespace='payment')),
payment/tests.py:
response = self.client.get(reverse('payment:pay', args=[plan.payment_id]))