Building URL from name - django

I've got a url of 'accounts:produce_setup' (i.e. namespace/app is 'accounts' and name of url is 'product_setup'). I'd like to build the full url associated with this view and pass it as context into a template.
How would I go about doing this? Would I use build_absolute_uri()?
Thanks!

you should include your app in project_name/urls.py and bind your application to special URL pattern like this:
from account.urls import urlpatterns as account_urlpatterns
urlpatterns = [
url(r'^account/', include(account_urlpatterns, namespace='account')),
url(r'^admin/', admin.site.urls),
]
and after this in your account/urls.py you can implement your urlpatterns and set your special name for each url like this:
from django.conf.urls import url
from .views import produce_setup_view
urlpatterns = [
url(r'^produce_setup/$', produce_setup_view, name='produce_setup')),
]
at the end now you can use them in your template and views or any python file in your django project like this:
.py in django project:
from django.urls import reverse
url_string = reverse('account:produce_setup')
print(url_string)
>>> '/account/produce_setup/'
in template:
Good Luck :)

Actually, you don't need to. You can simply use {% url "accounts:product_setup" %} in template. For more details check here. And if you want to build the url is views(maybe for other reasons) you can use reverse.

Related

How do you implement Django namespaces

I have started my journey with Django and I am thoroughly loving it. I do have a question about namespaces which I hope someone will be able to help me with and explain why it works like that.
I understand namespaces are used to make sure that if you have two pages with the same name that the url and reverse function points to the right page. I have been trying to implement namespaces in a test app I am writing but have not been able to do it.
Here is what I have so far (This is without namespaces as I haven't been able to get it to work.
Extract from app urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.index, name = "index"),
]
project urls.py
from django.contrib import admin
from django.urls import path, include, re_path
import gallery
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('index/', include('gallery.urls')),
]
And lastly, this is my view.py file in my app folder
from django.shortcuts import render
from django.urls import reverse
# Create your views here.
def index(request):
return render(request,"gallery/index.html")
Any help will be appreciated
URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It’s a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed. In other words, since multiple instances of a single application will share named URLs, namespaces provide a way to tell these named URLs apart. See URL namespaces
In your case:
project urls.py
from django.contrib import admin
from django.urls import path, include, re_path
import gallery
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls', namespace='your-namespace')),
path('index/', include('gallery.urls',namespace='your-namespace')),
]
Extract from app urls.py
from django.urls import path, re_path
from . import views
app_name = 'app'
urlpatterns = [
path('', views.index, name = "index"),
]
in the template:
{% url 'app:index' %}

Replacing default forms in django-postman via urlpatterns and views

django-postman docs say that you can replace the default forms in views with this:
urlpatterns = patterns('postman.views',
# ...
url(r'^write/(?:(?P<recipients>[^/#]+)/)?$',
WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)),
)
But what is patterns? Where do I import that from, and where would this code go? In the project's urls.py?
My project level urls.py currently includes django-postman, as recommended in the docs, like this:
urlpatterns = [
...
url("r'messages/', include('postman.urls', namespace='postman'),
]
So the custom url pattern should be overwriting the default that will already be included in urls.py.
Yes, this is a code for urls.py. However, it's quite outdated.
The modern version would look like:
from django.urls import re_path
urlpatterns = [
re_path(r'^write/(?:(?P<recipients>[^/#]+)/)?$',
WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)),
]
Edit
I guess you're including postman urls in your root urls.py, then you can do something like this to overwrite one of them:
urlpatterns = [
...
re_path(r'^messages/write/(?:(?P<recipients>[^/#]+)/)?$',
WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)),
path('messages/', include('postman.urls')),
]

Python - Django add multiple urlpatterns for multiple views of template

I'm very very new to Python 3 and Django and I get to the following problem: I use a standard Template and now how to set it up when there is 1 view. But I don't get the code right for multiple views. I currently run the page locally
At the moment I have tried to change different orders within urlpatterns, and they do work when only 1 url in in there, but I can't get the second one in
views.py
from django.shortcuts import render, render_to_response
# Create your views here.
def index(request):
return render_to_response('index.html')
def store(request):
return render_to_response('store.html')
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from myapp import views as views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^store/$', views.store, name='store'),
url(r'^admin/', admin.site.urls)
]
urlpatterns += staticfiles_urlpatterns()
I would like the url pattern that lets me go to the index view and the store view
EDIT:
Full code is shared via: https://github.com/lotwij/DjangoTemplate
The error in the comments shows you are going to http:/127.0.0.1:8000/store.html, but your URL pattern url(r'^store/$', ...) does not include the .html, so you should go to http:/127.0.0.1:8000/store/.
The Django URL system uncouples the URL from the name of the template (sometimes the view doesn't even render a template!). You could change the regex to r'^store.html$ if you really want .html in the URL, but I find the URL without the extension is cleaner.

How to register DRF router url patterns in django 2

My DRF routers specify a namespace so that I can reverse my urls:
urls.py:
router = DefaultRouter()
router.register('widget/', MyWidgetViewSet, base_name='widgets')
urlpatterns =+ [
url(r'/path/to/API/', include(router.urls, namespace='widget-api'),
]
Which, when upgrading to django 2, gives:
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.
Django 2 now requires app_name if the namespace kwarg is specified when using include. What's the right way to specify app_name when the url patterns are constructed by a DRF url router? I don't think the documentation is up-to-date for django 2 on this subject.
You need to put app_name = 'x' in your application's url.py file. This is a little buried in the documentation:
https://docs.djangoproject.com/en/2.0/topics/http/urls/#id5
For example, if in /project/project/urls.py you have:
path('', include('app.urls', namespace='app'))
Then in the corresponding url file (in /project/app/urls.py) you need to specify the app_name parameter with:
app_name = 'app' #the weird code
urlpatterns = [
path('', views.index, name = 'index'), #this can be anything
]
It's just necessary to use '{basename}-list' in reverse function.
In your case, it's going to be: reverse('widgets-list')
You need to include the router.urls as a tuple and add the app name to the tuple instead of only include router.urls
According to your example you should try with something like:
router = DefaultRouter()
router.register('widget/', MyWidgetViewSet, base_name='widgets')
urlpatterns =+ [
url(r'/path/to/API/', include((router.urls, 'my_app_name'), namespace='widget-api'),
]
The recommended approach is
from django.conf.urls import url, include
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'widget/', MyWidgetViewSet)
urlpatterns = [
url(r'^path/to/API/', include('rest_framework.urls', namespace='widget-api'))
]
See http://www.tomchristie.com/rest-framework-2-docs/tutorial/quickstart#urls

Django url dispatcher uses strings to specify function, why?

The Django documentation shows examples like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
]
However, I have seen some code that looks like this:
from django.conf.urls import url
urlpatterns = [
url(r'^articles/2003/$', 'myapp.views.special_case_2003'),
]
Where special_case_2003 is the name of a function in myapp/views.py.
What is the difference between these two approaches?
urlpatterns = [
url(r'^articles/2003/$', 'myapp.views.special_case_2003'),
]
Code like this is out of date. Providing the view as a string like this is deprecated in Django 1.8, and does not work in Django 1.10+. In Django 1.10+, you must use the callable.