Where does the smartselect URLS belong? - django

I'm trying to set op SmartSelect in my django project so I can chain dropdown menu's.
I do not understand how/where the urls belongs for the installation?
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^chaining/', include('smart_selects.urls')),
)
This is what their official installation guide says. Though this is either and old version of Django or i'm doing something wrong as I have not seen any URLS file written this way before and VScode does not recognize it.
What am I doing wrong?

As of django-3.1, url(…) [Django-doc] is
deprecated in favor of re_path(…) [Django-doc] and has been removed in django-4.0.
Furthermore a new syntax for paths has been introduced with path converters: you
use path(…) [Django-doc] for that.
So you can implement this as:
from django.urls import include, path
urlpatterns = [
path('admin/', include(admin.site.urls)),
path('chaining/', include('smart_selects.urls')),
]
That being said, if the documentation is that old, the project might no longer be "alive".

Related

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')),
]

how to set admin.site.urls as home page in django

some question about django 1.10:
I am going to make a website and the home page should be a login page created by django,when i tried to make the admin login page as my site's home page,it failed,can anybody help me ?
my urls.py:
urlpatterns = [ url(r'^$', include(admin.site.urls)), ]
or
urlpatterns = [ url(r'^$', admin.site.urls), ]
both works wrong.
thx
For djanog version 2 & later, the syntax url() has been replaced & simplified with path()
So, if you have django version >=2 , use this:
urlpatterns = [
path('', admin.site.urls),
]
Read the docs for details.
Generally speaking, setting admin page as the main page is not a good idea but still, whatever the use case is, try this:
url(r'^', admin.site.urls),
Tested it on Django 1.9 but I think it'll work on Django 1.10 as well.
Also, don't forget to restart the server after making these changes.

Why do my files not match a Django 1.8 installation even though Bash confirms I have one installed?

I set up the virtual environment variable for Django 1.8 as per the instructions in the Django tutorial (1.8) and the (almost) matching 1.7 tutorial on PythonAnywhere. When I go into Bash and follow the instructions to check the Django version it confirms that I have version 1.8. installed.
I am up to part 3 in the Django tutorial at this URL:
https://docs.djangoproject.com/en/1.8/intro/tutorial03/
In mysite/urls.py the tutorial tells me to write this:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
However, when I actually opened the file I was presented with this:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
The tutorial then has this to say:
Doesn’t match what you see? If you’re seeing admin.autodiscover()
before the definition of urlpatterns, you’re probably using a version
of Django that doesn’t match this tutorial version. You’ll want to
either switch to the older tutorial or the newer Django version.
As I said though, Bash confirms that I Do have Django 1.8 installed. What am I missing here? Why do I not have the correct files for Django 1.8 even though that's what I supposedly installed?
I tried to make the files match the tutorial files, but it only resulted in an error appearing on my public site.
The most likely cause is that you didn't activate the virtualenv when you ran startproject, so you got the default Django from PythonAnywhere instead of the one that you installed.

Django: international subroutes

It's written in official documentation here that i18n_patterns() is only allowed in your root URLconf.
This is a problem for me because I need those URLs working:
/en/products/
/fr/produits/
/sv/produkt/
/en/products/detail/[my product name]/
/en/produits/detail/[my product name]/
/sv/produkt/detalj/[my product name]/
/sv/produkt/detalj/[my product name]/
Heres my root urls.py:
urlpatterns += i18n_patterns(
url(_(r'^produits/detail/'),
include('produits.urls', namespace="produits")
),
url(_(r'^produits/'),
include('produits.urls', namespace="produits")
),
)
So, for the lastest translation works ok, but the first one doesn't. The translation is ok, but I want to transfer the last part ('detail/') to the app produits that should handle it transparently.
How do I do?
Here's how I've solved the problem. This is not a good solution but it's the only one working for now with Django 1.8.
I've removed my routes from my "produits" application, and added them to the main urls.py which means mixing application configuration with global configuration. I dont like it.
I've also added the name of the application before each route: produits_detail and produits_index. Thus I know the app those routes are for. Once again I dont like mixing "global setting" and specific settings, but it seems I have no choice. Anyway, few lines of code, and it works pretty well.
from django.contrib import admin
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _
from produits import views as p_views
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', include(admin.site.urls)),
]
urlpatterns += i18n_patterns(
url(_(r'^produits/detail/(?P<slug>[a-zA-Z0-9-_]+)/$'),
p_views.DetailView.as_view(), name='produits_detail'),
url(_(r'^produits/'),
p_views.IndexView.as_view(), name='produits_index'),
)

Difference between admin.site.root and admin.site.urls

In the The Django Book in chapter 6 about the Admin Site, they tell me to add the follwing URLpattern to urls.py:
urlpatterns = patterns('',
# ...
(r'^admin/', include(admin.site.urls)),
# ...
)
But to make it work on my system, I had to uncomment the following line:
(r'^admin/(.*)', admin.site.root),
Can somebody enlighten me on what the differences are?
Both Gabriel and Antti have it the wrong way round, unfortunately.
admin.site.root is the version 1.0 behaviour. If you have downloaded 1.0 or 1.0.2, that's what you should use.
However, there were some changes to the URL handling for Django's admin very recently, which are part of the yet-to-be-released 1.1. These are primarily to make it possible to use the reverse() function to look up admin URLs. So if you have a recent checkout of the code, you'll need to use admin.site.urls.
Your link is to the second edition of the Django Book, which is being updated for version 1.1 - and the docs which Gabriel refers to are also for the current checkout, which has the new version.
(Just for completeness, I'd note that versions of Django before newforms-admin was merged, prior to 1.0, used admin.urls, not admin.site.urls or admin.site.root.)
Please notice the following; I struggled because of (.*) being in the second entry below.
Works, but is deprecated:
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root)),
)
Incorrect, and partially works:
urlpatterns = patterns('',
(r'^admin/(.*)', include(admin.site.urls)),
)
Correct, and works well:
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
The Django Book speaks of version 0.9.6. Since then the admin has been rewritten. In Django 1.0 the whole admin is served by a single view (admin.site.root) which parses the rest of the URL internally.
Compare the admin directory of 0.96.3 with the corresponding directory from 1.0.2. There is no urls.py in the latter.
from the source code for the admin.site.root function:
root(self, request, url): Handles main URL routing for the admin app.
[...] method can be used as a
Django view function that presents a
full admin interface for the
collection of registered models.