Django-filer problem when retrieve uploaded image in Django admin interface or any http|s request [duplicate] - django

I am using Django-Filer in my admin on a Django web project which is hosted in PythonAnywhere. I have gone through the installation instructions but I am having trouble accessing the canonical urls the Filer makes for each file. It seems I am being directed to an extended url that Filer.urls is not recognizing (the non-canonical part starts at /filer-public/; this is a directory that is being created and storing my files on the separate PythonAnywhere file directory).
Is there an error in my urls syntax? An error in my views.canonical? I am unsure of why plugging in the exact canonical url redirects me to this extended version of the url.
Python: 3.7
Django: 2.2
Canonical URL:
/filer/sharing/1560887480/39/
ERROR / DEBUGGING SCREEN
Page not found (404)
Request Method: GET
Request URL: http://www.mywebsite.com/filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg
Using the URLconf defined in mywebsitesite.urls, Django tried these URL patterns, in this order:
admin/
^filer/ sharing/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$ [name='canonical']
The current path, filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg, didn't match any of these.
APP URLS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/urls.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from django.conf.urls import url
from . import settings as filer_settings
from . import views
urlpatterns = [
url(
filer_settings.FILER_CANONICAL_URL + r'(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$', # flake8: noqa
views.canonical,
name='canonical'
),
]
APP VIEWS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/VIEWS.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
from .models import File
def canonical(request, uploaded_at, file_id):
"""
Redirect to the current url of a public file
"""
filer_file = get_object_or_404(File, pk=file_id, is_public=True)
if (not filer_file.file or int(uploaded_at) != filer_file.canonical_time):
raise Http404('No %s matches the given query.' % File._meta.object_name)
return redirect(filer_file.url)
BASE URLS: /home/mywebsite/mywebsite/urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
from django.views.generic import TemplateView
from quotes.views import Register
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
path('', include('pages.urls')),
url(r'^filer/', include('filer.urls')),
]
BASE SETTINGS: /home/mywebsite/mywebsite/settings.py
FILER_CANONICAL_URL = 'sharing/'

I was able to get the canonical url to work by configuring the static and media root in my urls and settings files, as shown below.
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
url(r'^filer/', include('filer.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
MEDIA_ROOT = os.environ.get('FILER_MEDIA_ROOT', os.path.join(BASE_DIR, 'media'))
MEDIA_URL = '/home/mywebsite/media/'

I was able to solve this by not including the filer.urls but rather specifying the url pattern directly in my projects urls.py file.
Django 3.1.7
django-filer 2.1.2
/myproject/myproject/urls.py:
...
from filer import views as filer_views
from .settings import FILER_CANONICAL_URL
...
urlpatterns = [
url(r'^filer/'+FILER_CANONICAL_URL+r'/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$',
filer_views.canonical,
name='canonical'),
...,
]
/myproject/myproject/settings.py
...
FILER_CANONICAL_URL = 'somefolder/'
However I'm still not sure where the extra space was coming from in the first place - the filer urls.py and settings.py look fine as far as I can tell.

Related

Routing does not work in Django: page not found (GET)

I've tried multiple methods of writing views but I don't think it is a problem here. App is installed in settings.py
It displays error every time.
project structure:
structure
views.py (app folder)
from django.http import HttpResponse
from django.shortcuts import render
def home_view(request):
return HttpResponse('Hello World')
url.py in apps folder
from django.urls import path
from . import views
urlpatterns = [
path('home_view/', views.home_view)
]
apps.py in app folder
from django.apps import AppConfig
class AppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app'
urls.py in store folder
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/home_view/', include('app.url')),
path('admin/', admin.site.urls),
]
error message:
error
As a django web development expert I saw some small corrections to be done:
In store urls.py file its app/ and app.urls
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/', include('app.urls')),
path('admin/', admin.site.urls),
]
Then change the app url.py file name to standard name urls.py.
also don't forget to add your app to installed_apps variable in settings.py file:
INSTALLED_APPS=[
'app',
'django.contin.auth',
#and other already specified apps
]
Rest all files have no bugs!!
According to this the correct path for HttpResponse is:
http://localhost:8000/app/home_view/
OR
http://127.0.0.1:8000/app/home_view/
In your urls.py file
Change
path('app/home_view/', include('app.url')),
To
path('', include('app.url')),
Then
On your browser go to:
127.0.0.1:8000/home_view/
The main thing it must be app.urls not app.url. change your file to url.py to urls.py, its recommended.
If you have defined path('app/home_view/', include('app.urls')) in urls.py of your store folder, then it goes to your urls.py which is in app.
In your app's urls.py you have written path('home_view/',views.home_view).
It means if you type 127.0.0.1:8000/app/home_view/home_view/ then it will render your HttpResponse that is Hello world.

How to fix 'The included URLconf 'gp.urls' does not appear to have any patterns in it'

when i tried to write the first urlpattern and the first view, i got this error, so i can be able to access to the authentification template, i have no idea what can be the source of this error
# my gp/urls.py
from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html')
when i try to run the server this is the error i get
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'gp.urls' does not appear to have any patterns in it. If you see valid
patterns in the file then the issue is probably caused by a circular
import.
this is my program tree
gp
apps
conge
organisation
personnel
stage
gp
pycache
init.py
settings.py
urls.py
views.py
wsgi.py
static
templates
gp
index.html
db.sqlte3
manage.py
# my gp/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html', {})
what i edited is:
instead of from django.conf.urls import url i wrote from
django.urls import path
added {} in render function (its optional but just in case :) )
As stated in here: https://code.djangoproject.com/ticket/30728#no1
Maybe try to use latest Python version. Python 3.9 able to work

How to fix this error - Not Found: /firstapp/ "GET /firstapp/ HTTP/1.1" 404 2065"

I have created a project named firstproject and added a new project firstapp in which urls.py of firstapp is invoked to call view.py which displays a string in webpage. But the webpage is showing error.
I have created a project named firstproject and added a new project firstapp in which urls.py of firstapp is invoked to call view.py which displays a string in webpage. But the webpage is showing error.
firstapp
urls.py
from django.conf.urls import url
from . import views
urlpatterns={
url('r^$',views.index,name='index')
}
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<H2> Hey Everyone welcome to Django Tutorial! </H2>")
# Create your views here.
firstproject
urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$/', include('firstapp.urls')),
]
I expect the output to be "Hey Everyone welcome to Django Tutorial!"
In your firstapp urls.py there is a mistake use [] instead of {}
second mistake in is your code url regex ('r^$') is wrong
change it to (r'^$')
in firstapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='index')
]

django 'admin' is not a registered namespace in pybbm-forum app on subdomain

I have django 1.10.7, python2.7, installed django-hosts, pybbm app.
Pybbm forum on subdomain forum.example.com.
When i'l trying open topic on url forum.example.com/topic/1/, that already have created, i get error.
NoReverseMatch at /topic/1/
u'admin' is not a registered namespace
my hosts.py
# -*- coding: utf-8 -*-
from django_hosts import patterns, host
from django.conf import settings
host_patterns = patterns('',
host(r'example.com', settings.ROOT_URLCONF, name='www'),
host(r'forum', 'forums.urls', name='forum'),
)
my forums/urls.py, where i included pybb urls
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'^', include('pybb.urls', namespace='pybb')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Can you help how to better configure pybbm forum app with my django project on subdomain?
Looks like you have no admin url in your 'forums/urls.py' and the template that renders '/topic/1/' has some django url link pointing to admin routes (e.g. Foo) that doesn't even exist. Can you show the template code?

Django URL mapping - NameError: name X is not defined

[A similar question was asked, but not marked as answered, here. I considered continuing that thread but the website told me I'm only supposed to post an answer, so it seems I have to start a new topic.] I'm trying to follow this tutorial and I'm having problems with the URL mapping. Specifically with the part described as "So best practice is to create an “url.py” per application and to include it in our main projects url.py file". The relevant, I hope, part of the folder structure, which arose by following steps of the tutorial to the letter (if possible; usage of the 'patterns' module was impossible for example) and using Django 1.10 is the following:
myproject/
myapp/
urls.py
views.py
myproject/
urls.py
The myproject/urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include(myapp.urls)),
]
The myapp/urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^hello/', myapp.views.hello),
]
The myapp/views.py is as follows:
from django.shortcuts import render
def hello(request):
return render(request, "hello.html", {})
However, running 'python manage.py runserver' results in the following error:
url(r'^myapp/', include(myapp.urls)),
NameError: name 'myapp' is not defined
INSTALLED_APPS in settings.py contains 'myapp'.
I'd be greatful for any tips on how to deal with the NameError! [Or any tips whatsoever that anyone might consider to be helpful!]
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which means that the import is not required.
url(r'^myapp/', include('myapp.urls')),
Since you have move the hello URL pattern into myapp/urls.py, you can remove from myapp.views import hello from myproject/urls.py.
Once you've made that change, you will get another NameError in myapp/urls.py. In this case, a common approach is to use a relative import for the app's views.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
Make sure you have imported following modules to urls.py.
from django.conf.urls import url
from django.contrib import admin
in django 2.0
use these
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
path('',views.index, name="index"),
path('admin/', admin.site.urls),
]
your app URL has to be a string
so, here is how the code should look like.
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
also, note that from python 2 upward the regular expression is not needed.
change URL to path
from django.conf.URLs import include path
from Django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
path('^admin/', include(admin.site.urls)),
path('^myapp/', include('myapp.urls')),
]
In Django 2.1.7 here is the default urls .py file
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
so we need to add this line as well
from django.conf.urls import url
I have followed #Alasdair answers
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which
means that the import is not required.
Unfortunately, it didn't work out(I still got the name X is not defined error). Here is how I do it.
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from article import urls as article_users
from article import urls as user_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('api/article/', include(article_users)),
path('api/user/', include(user_urls)),
]
Before using the URL command be sure to first import the url from the module Urls. Then try using the runserver.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path