Django Warning regex beginning with a '/' when running a server - python-3.x

When I run a server in console I am getting warnings:
?: (urls.W002) Your URL pattern '^/(?P<slug>[\w-]+)/$' [name='detail'] has a regex beginning with a '/'. Remove this slash as it is unnecessary.
I did some research and found that you can silence checks with the SILENCED_SYSTEM_CHECKS setting.
In urls.py url patterns are defined as follows:
urlpatterns = [
url(r'^$', PostListAPIView.as_view(), name='list'),
url(r'^/create/$', PostCreateAPIView.as_view(), name='create'),
url(r'^/(?P<slug>[\w-]+)/$', PostDetailAPIView.as_view(), name='detail'),
url(r'^/(?P<slug>[\w-]+)/edit/$',
PostUpdateAPIView.as_view(), name='update'),
url(r'^/(?P<slug>[\w-]+)/delete/$',
PostDeleteAPIView.as_view(), name='delete'),
When '/' is removed after "r'^" I have an error from server: Page Not Found (404) error, Django tried these URL patterns, in this order:
....
When '/' is added it is working perfectly.
But I guess there should other workarounds to fix this problem. Any suggestions?

Related

How to redirect to URL with trailing slash in warp?

I am using warp to serve a directory of static files. Unfortunately the relative links used in those static files can only be resolved, when I add a trailing slash to my path.
This is the code I use to serve the directory:
let route = warp::path("segment")
.and(warp::path("another"))
.and(warp::fs::dir("../path/to/static/files/folder"));
warp::serve(route).run(([0, 0, 0, 0], 3030)).await;
So now
0.0.0.0:3030/segment/another/ works fine
0.0.0.0:3030/segment/another does not work
In principle I would not mind this and just always use the URLs with trailing slashes, however when I "Add to Homescreen" the page from Safari on iOS (as a PWA) the trailing slash is ommited automatically.
So in order to work around this, I tried to create a redirect, and then add the trailing slash like this:
let redirect = warp::path("segment").and(warp::path("another")).and(warp::path::end())
.map(|| {
warp::redirect(Uri::from_static("0.0.0.0:3030/segment/another/"))
});
however this redirect filter only matches 0.0.0.0:3030/segment/another/. When I omit the warp::path::end() the redirect works for 0.0.0.0:3030/segment/another but now everything (for example 0.0.0.0:3030/segment/another/styles.css) gets redirected to 0.0.0.0:3030/segment/another/.
Is there a way to only redirect when the path does not end with a slash or a file extension (e.g. .html, .cssetc.)?
It might be that my overall aproach is incorrect here, too.

Django website saying "Page not found (404)"

I just deployed a django website that I made. This is my first time of doing something like this.. still a newbie. The problem is that whenever I try to upload something to the website, I get the following error while DEBUG is True
Page not found (404)
Request Method: POST
Request URL: https://majestylink.com/admin/music/music/add/
Raised by: django.contrib.admin.options.add_view
Using the URLconf defined in majestylink.urls, Django tried these URL patterns, in this order:
[name='index']
advertise/ [name='advertise']
about-us/ [name='about-us']
promote/ [name='promote']
privacy-policy/ [name='privacy-policy']
s/ [name='search']
admin/
poem/
video/
music/ [name='index']
music/ <slug:slug>/ [name='detail']
ckeditor/
^media\/(?P<path>.*)$
The current path, music/music/add/, didn't match any of these.
The code below is my music app url..
from django.urls import path
from . import views
app_name = 'music'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<slug:slug>/', views.detail, name='detail'),
]
And this one is my project url..
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('home.urls')),
path('admin/', admin.site.urls),
path('poem/', include('poems.urls')),
path('video/', include('video.urls')),
path('music/', include('music.urls', namespace='music')),
path('ckeditor/', include('ckeditor_uploader.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What else am I supposed to do in order to be able to publish something on the website? Am still new so I have a limited knowledge on how these things works
Everything works fine on local server, I get any error while testing it on local machine.
I made a slight change while deploying the website which is changing the database from the default sqlite3 to MySQL, will this be the cause? If yes, how can I resolve it. All migrations were run successfully
Your URL points to a admin/music/music/add path. I'm guessing you meant admin/music/add. You may want to share the template that triggers the add command.

Snap framework root ("/") route not being handed?

I have added routes to my app in the following format:
addRoutes
[ ("/", redirect "/docs/home")
, ... more routes
]
But for some reason the root handler is being ignored altogether. What is happening here?
It turns out that if you have an index.tpl file then the SnapFramework will ignore any "/" mappings and go straight to that instead. To fix the problem I just ran:
git rm snaplets/heist/templates/index.tpl
And then reloaded my templates and the route on root started working.
(I could not find that anywhere in the docs so I decided to post that here)
Edit: I later discovered (with help) that the problem was that I was adding my routes AFTER I ran heistInit. If i added my routes before heistInit then there was no problem.

How do I set up static serving in Express with an arbitrary start path?

If I want to set up the directory .../whatever/stuff to be served statically, but referenced as http://example.com/mystuff, I tried doing this:
app.configure(function() {
app.use('/mystuff', _express.static(__dirname + "/whatever/stuff"));
app.use('/mystuff', _express.directory(__dirname + "/whatever/stuff"));
});
This mostly works, but if I reference a subdirectory of mystuff without a trailing slash, say http://example.com/mystuff/subdir, it redirects to the wrong place (http://example.com/subdir/), resulting in a 404. This is especially problematic with directory listings, since the directory middleware doesn't put a trailing slash on links to subdirectories.
Is there something I can do to get around this? (and is my syntax above correct?)
try this:
app.use('/mystuff*', ..);

Cannot serve static files with express routing and no trailing slash

I want a route called 'main' which will serve static files:
app.use('/main',express.static(__dirname+'/public'));
However when i do:
http://my.site.dev/main
The CSS and JS files won't download because it is trying to get them from
http://my.site.dev/css/styles.css
It should be getting the files from:
http://my.site.dev/main/css/styles.css
However, if I access my site with a trailing slash:
http://my.site.dev/main/
All files come through fine
Any ideas why not having a trailing slash messes up resources like CSS and JS from coming in?
This is an http problem, not just an Express-related challenge. The problem is discussed at:
Relative URLs and trailing slashes
http://www.bizcoder.com/the-mystery-of-the-trailing-slash-and-the-relative-url
If your URL is /main and your relative URL is css/style.css, it will resolve to /css/style.css; but if your URL is /main/, the relative URL resolves to /main/css/style.css.
My strategy for dealing with this is to redirect to add the trailing slash. Like this in Express:
app.all(/^\/main$/, function(req, res) { res.redirect('/main/'); });
app.use('/main/',express.static(__dirname+'/public'));
Or:
app.enable('strict routing');
app.all('/main', function(req, res) { res.redirect('/main/'); });
app.use('/main/',express.static(__dirname+'/public'));
How are the JS/CSS files requested in the HTML? If you're using strings like css/styles.css, then it will try to get them from the current directory. The directory for /main is / (just like /main.html would be), while the one for /main/ is /main/. A quick fix would be to use /main/css/styles.css in your HTML.

Resources