URL conflict in django1.8 and DRF 2.4.8 - django

Project level urls.py
urlpatterns += [
url(r'^machines/api/', include('core.urls')),
url(r'', include('apps.api.urls')),
url(r'^machines', include('apps.machines.urls')),]
App level urls.py
urlpatterns = [
url(r'^user/edit/(?P<pk>[0-9]+)/$', core_view.ProfileEdit.as_view()),
url(r'^group/', core_view.GroupList.as_view()),
url(r'^groups/add/', core_view.GroupCreate.as_view()),]
when i hit
http://localhost:8000/machines/api/groups/add
it is calling GroupList view instead of GroupCreate.
I am not getting any reason why this is happening?

Current url list triggered GroupList on each url started with group/. You should add $ at the end of GroupList pattern to limit url triggered by GroupList only with group:
url(r'^group/$', core_view.GroupList.as_view()),
url(r'^groups/add/', core_view.GroupCreate.as_view()),]

Related

How to have only redoc url in drf-spectacular

I'm using drf-spectacular for my django project:
https://github.com/tfranzel/drf-spectacular
On urls.py I have this:
urlpatterns = [
# YOUR PATTERNS
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
# Optional UI:
path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]
I just want to have 'api/schema/redoc/' so I removed the other two URLs.
After removing 'api/schema/' I get the following error:
Reverse for 'schema' not found. 'schema' is not a valid view function or pattern name.
How can I remove that URL like this but resolve the error:
urlpatterns = [
path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]

Django REST Framework tutorial sends Page Not Found on part 1

I'm new to Django and API developing, so I started following Django REST Framework tutorial, and I have an error in the first part of it.
When I go to "127.0.0.1:2000/" on my browser, the Api Root page appears (I hosted it on port 2000), so it seems to be working, but when I try to go to "127.0.0.1:2000/snippets/", I get a Page Not Found error.
I imagine this is a very simple thing I'm just overlooking, but I'm kind of stuck right now, and would appreciate the help. Should my "tutorial/urls.py" include the snippets in any way? I followed the tutorial from the beginning, and re-viewed it, so I don't think so, but its a hypothesis.
According to the tutorial, my tutorial\urls.py (tutorial being the project name) looks like this:
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
And the app's (snippets\urls.py) like this:
urlpatterns = [
path('snippets/', views.snippet_list),
path('snippets/<int:pk>/', views.snippet_detail),
]
Did you add your URLs from the app you created to the main URLs .py?

Url not found in server

I have written a function on paypal when user would cancel payment then it would redirect to paypal-cancel page which i have defied in my app level url.py. But problem is it is showing error that
Not Found
The requested URL /paypal-cancel was not found on this server.
BuT I have defined this function in url.
web/urls.py
urlpatterns = [
url(r'^paypal-payment$', ApiPaymentPayPalView.as_view(), name='paypal-payment'),
url(r'paypal-cancel', PaypalCancelView.as_view(), name='paypal-cancel'),
]
whatever i do r'paypal-cancel' or r'^paypal-cancel$' nothing is working
project/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('web.urls')),
]
So,where am i doing wrong? I think i have given wrong regex probably!

Including only selected urls in django

I have a third party app with the following url configuration:
urlpatterns = [
url(r'^webhook/(?P<token>[-_:a-zA-Z0-9]+)', TelegramView.as_view(), name='webhook'),
url(r'^bots', BotViewSet, name='api'),
]
Now I want to include only the second url (with namespace 'api') because I have overridden the first one in my own app (It will be handled by my own views). How do I use include so that it only includes the second url?

Django and service workers - serve "sw.js" at application's root url

So I'm building a Django progressive web app with offline support using service workers.
According to google's documentation, the sw.js file should be at the root of the app's url:
You need to do this because the scope of a service worker (the set of
urls that the ServiceWorker will load for) is defined by the directory
where it resides.
At the moment, I'm serving all static assets from http://example.com/static/ folder. But I need to serve this specific file at a url like: http://example.com/sw.js.
Any idea how I can achieve this? I could make a specific nginx rule to do this redirection, but I don't know if it's the cleanest way of doing this. Maybe this setting should reside in urls.py?
Note: I've seen this question which suggests using the static() method from django.conf.urls.static. But django's docs say that the static method is only for development use so not good for me.
Note (2): I guess I could change the STATIC_URL setting, but I'm happy with my files being served from /static directory. I only want this one file to be at url's root.
You can serve javascript as a view, not just html. Put this in your projects urls.py
url(r'^service-worker.js', cache_control(max_age=2592000)(TemplateView.as_view(
template_name="service-worker.js",
content_type='application/javascript',
)), name='service-worker.js'),
Then put your service-worker.js in your templates directory.
Bonus is that you can now also use template tags like static in your javascript file.
Django 2.2
project structure
myproj/
|-app/
| |-templates/
| |-app/
| -sw.js
|-myproj/
-urls.py
urls.py (project)
from django.views.generic import TemplateView
urlpatterns = [
...
path('sw.js', (TemplateView.as_view(template_name="app/sw.js",
content_type='application/javascript', )), name='sw.js'),
]
In Django 1.11 urls.py should look:
from django.views.generic import TemplateView
urlpatterns = [
url(r'^sw.js', (TemplateView.as_view(template_name="sw.js", content_type='application/javascript', )), name='sw.js'),
]
I was getting all the time the error DOMException: The script resource is behind a redirect, which is disallowed.
I spent hours trying to figure out the solution.
Apart from adding at urls.py:
from django.views.generic import TemplateView
urlpatterns = [
...,
url(r'^service-worker.js', (TemplateView.as_view(template_name="service-worker.js", content_type='application/javascript', )), name='service-worker.js'),
]
There was also another step needed.
Instead of
<script>
if ('serviceWorker' in navigator) {
console.log("Will the service worker register?");
navigator.serviceWorker.register('service-worker.js')
.then(function(reg){
console.log("Yes, it did.");
}).catch(function(err) {
console.log("No it didn't. This happened:", err)
console.log("err.message:", err.message)
});
}
</script>
I used:
<script>
if ('serviceWorker' in navigator) {
console.log("Will the service worker register?");
navigator.serviceWorker.register("{% url 'service-worker.js' %}") //note that I am using the url template here
.then(function(reg){
console.log("Yes, it did.");
}).catch(function(err) {
console.log("No it didn't. This happened:", err)
console.log("err.message:", err.message)
});
}
</script>