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.
Related
Problem
When trying to request my robots.txt file at website.com/robots.txt, I always receive a 404 error.
Files
config > urls.py
from django.conf import settings
from django.contrib import admin
from django.urls import path, include, re_path
from django.views.generic import TemplateView
from django.conf.urls.static import static
from config import views
from django.conf.urls import handler404, handler500, handler403, handler400
handler404 = views.handler404
handler500 = views.handler500
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', include('pages.urls')),
path('plants/', include('plants.urls')),
path('robots.txt',TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
]
config > views.py
from django.http import JsonResponse, Http404, HttpResponse
from django.shortcuts import render
def handler404(request, exception=None):
return render(request, '404.html', status=404)
def handler500(request):
return render(request, '500.html', status=500)
templates > robots.txt
User-Agent: *
Disallow: /admin/
Disallow: /accounts/
Parts of my folder structure that may be helpful to know
project
|
|---config
| |
| |---urls.py
| |---views.py
|
|---templates
|
|---pages
| |---about.html
| |---contact.html
|
|---404.html
|---robots.txt
I've also tried using only exception instead of exception=None inside of my handler404.
I've tried to move the robots.txt views and urls into my pages app with the robots.txt within the pages template folder.
I've tried removing the content_type=text/plain from the url pattern.
I've read through several different tutorials and I'm not sure what I'm doing wrong.
Some of your secondary urls.py files contain too general pattern which catches "robots.txt" thus URL dispatcher never reaches expected pattern declaration in the end of pattern list.
To fix the issue try moving robots pattern higher in the pattern list and/or review included urls.py files and make their patterns more specific.
Also please consider making "static" robots.txt file truly STATIC in terms of Django. Such files are not supposed to be served by Django backend on prod and do not need template engine involved.
I am new to Django and learning this python framework. I have created a virtual environment called Virtual_Django_Project and installed Django in it. Moving ahead I created folder called Django_project and added an app called blog you can see my files here File Directories. Error >Page not found at /blog/
blog.views.py Code
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse('Home')
blog.urls.py Code
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name ='blog-home'),
]
Main Django_Project.urls Code
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('blog.urls')),
]
Can anyone tell me why my blog page is not opening and showing the error page not found?
PS: Server is running in cmd.
Ok so I guess, based on your screenshot it seems to look like only your admin page is properly registered.
It is very simple, just go to settings.py in project and make sure you added your apps in installed apps.
INSTALLED_APPS = [
...
"app name"
then clearly you have added urls.py in app but you have to add in project urls.py too... If you forgot it add it too...
urlpatterns = [
path('blog/', include('app name.urls', namespace='app name')),
namespace is not a must to add.
I guess that should be the error...
There is no path found in your blog app. It seems that simply you have not specified "blog" in your INSTALLED_APPS. Pretty common mistake after creating a new app in a Django's project. Add it so Django would be able to find that view.
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?
By follow Angular2 example, I create my first app
under my project folder. I can run: "npm start" to make the application running
For example I can access the app from http://localhost:3000
I have a new requirement which I need to run the application under a context path, which mean I need to access the application through http://localhost:3000/myapp
The question is how to deploy or run Angular2 application in a sub folder path?
try to add in the index.html file. However, it seems Angular cannot resolve the path to find the template URL and resource JS and TS files.
Can anyone help to make sure I can run the app under a context path "myapp"?
Add base tag to index.html:
<base href="/myapp">
or in your bootstrap file (main.js)
import { provide } from '#angular/core';
import { APP_BASE_HREF } from '#angular/common';
bootstrap(AppComponent, [
provide(APP_BASE_HREF, {useValue: '/myapp'}),
]);
I'm using bower to grab the .css and .js files for my website.
The directory structure looks like this:
server.py
bower_components/
templates/
All of the HTML templates (and some partials templates that I use with AngularJS) are located in templates. Stuff installed by bower is located in bower_components. How can I define template_path, static_path and static_url_prefix settings so that I can link into files in those directories?
If I use relative paths like:
href="bower_components/bootstrap/dist/css/bootstrap.css"
I will get 404 error. No handlers can be found so the error is thrown. It seems that Tornado forces me to use static_url function on my templates? Do I really have to use it? It looks very ugly when mixed with AngularJS. I have tried setting static_path to os.path.dirname(file) and tried using static_url but that gives me exception:
Exception: You must define the 'static_path' setting in your application to use static_url
How should I configure this? I can't believe I have wasted hours trying to figure this out already... :(
Did you try to use the StaticFileHandler? You can use something like this:
import os
import tornado.web
import tornado.ioloop
if __name__ == '__main__':
app = tornado.web.Application([
(r"/(.*)", tornado.web.StaticFileHandler, {"path": os.path.dirname(os.path.abspath(__file__))}),
])
app.listen(8888)
tornado.ioloop.IOLoop.instance().start()
You must combine staticfilehandler with some path variables you put in the settings variable.
http://tornado.readthedocs.org/en/latest/web.html
It's pretty easy. You can name as many settings variables as you want.
It turned out I needed to use tornado.web.StaticFileHandler class with path mapped to it. This is how I configured my tornado.web.Application:
class TornadoApp(tornado.web.Application):
def __init__(self):
# static file regex patterns matched with the actual folders here
static_handlers = {
'/bower_components/(.*)': 'bower_components',
'/templates/partials/(.*)': 'templates/partials'
}
handlers = [
('/', IndexHandler),
('/products', ProductHandler),
]
# append the handlers with content from static_handlers dictionary
for r, p in static_handlers.items():
handlers.append((r, tornado.web.StaticFileHandler, {'path': p}))
settings = {
'template_path': "templates",
'debug': True
}
tornado.web.Application.__init__(self, handlers, **settings)