NoReverseMatch " " is not a registered namespace - django

Hello guys I've been struggling to move my project in another location in anaconda so after finally installing everything and setting up the project I am getting some errors that I don't understand. First of all I had my code on a sub-folder inside my app called api there I had my views, serializers, urls. And I included the urls but nothing seemed to happen. I moved all the api files to the app folder and I deleted the api folder. Now I'm getting this error NoReverseMatch at /op_data/objects/ ('api-op-data' is not a registered namespace). Even after deleting this url I keep getting the same error. This is my code:
urls.py
from django.urls import path, re_path
from django.views.generic import TemplateView
from django.conf.urls import url, include
from django.contrib import admin
from djgeojson import views
from djgeojson.views import GeoJSONLayerView
from django.conf.urls.static import static
import MMA
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from MMA import views
from rest_framework_jwt import views
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/auth/token/$', obtain_jwt_token, name='api-auth-token'),
url(r'^api/', include(('MMA.urls', 'api-op-data'), namespace='api-op-data')),
]
urls.py
from django.conf.urls import url
from django.contrib import admin
from .views import OP_Data_RudView, OP_Data_ApiView, UserCreateAPIView, UserLoginAPIView, WoType_ApiView, WoType_RudView, UserObjects_ApiView, UserObjects_RudView
app_name = 'MMA'
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/register/$', UserCreateAPIView.as_view(), name='register'),
url(r'^users/login/$', UserLoginAPIView.as_view(), name='login'),
url(r'^op_data/$', OP_Data_ApiView.as_view(), name='post-listcreate'),
url(r'^op_data/(?P<pk>\d+)/$', OP_Data_RudView.as_view(), name='post-rud'),
url(r'^op_data/wo_type/$', WoType_ApiView.as_view(), name='post-listcreate'),
url(r'^op_data/wo_type/(?P<pk>\d+)/$', WoType_RudView.as_view(), name='post-rud'),
url(r'^op_data/objects/$', UserObjects_ApiView.as_view(), name='post-listcreate'),
url(r'^op_data/objects/(?P<pk>\d+)/$', UserObjects_RudView.as_view(), name='post-rud'),
]
error log:
NoReverseMatch at /op_data/objects/
'api-op-data' is not a registered namespace
Request Method: GET
Request URL: http://---.--.-.---:7000/op_data/objects/
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:
'api-op-data' is not a registered namespace
Exception Location: C:\Users\Administrator.HR-JUGOR\Anaconda3\envs\MMA\lib\site-packages\django\urls\base.py in reverse, line 86
Python Executable: C:\Users\Administrator.HR-JUGOR\Anaconda3\envs\MMA\python.exe
Python Version: 3.6.5
Python Path:
['C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\Mobile',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\python36.zip',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\DLLs',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages\\win32',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages\\win32\\lib',
'C:\\Users\\Administrator.HR-JUGOR\\Anaconda3\\envs\\MMA\\lib\\site-packages\\Pythonwin']
Server time: Wed, 24 Jul 2019 09:49:27 +0000

/op_data/objects/ isn't the configured url for post-listcreate, its api/op_data/objects/.
In this case the namespace argument seems to be redundant, you already declared the namespace as '^api/' before the include(), declaring it explicitly as 'api-op-data' later the same line seems unnecessary based on the code provided, though I would need to see how you were accessing these urls from your views and templates to know for sure.
You also don't need to redeclare your admin url in MMA.urls, and I question your decision to include the api/auth/token url in the root url config, it seems like it belongs in MMA.urls with the other urls in the api namespace

Related

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

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.

Page Not Found 404 Django & Python

I am having the following error
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in Decoder.urls, Django tried these URL patterns, in this order:
form.html [name='form1']
hl7 [name='hl7']
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Its my first time writing code using Django
`from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('hl7rest.urls')),
]`
and this other file
from . import views
from django.urls import path
urlpatterns = [
path('form.html', views.render_form_View, name='form1'),
path('hl7', views.hl7_web_view ,name='hl7'),
]
Your paths don' t match the request.
You can create a TemplateView subclass for render your template:
Views
from django.views.generic.base import TemplateView
class HomePageView(TemplateView):
template_name = "display_form.html"
Url patterns
urlpatterns = [
path('', HomePageView.as_view(), name='home')
]

Improperly Configured urls.py during Django deployment (Django 2.1)

This is my first time deploying Django. My app runs fine locally, but when I deploy, I get this error:
ImproperlyConfigured at /admin/
The included URLconf module 'search.urls' from '/home/imeaytbc/myproject/search/urls.py' 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.
My urls.py file is exactly the same as the one run on my computer:
from django.urls import path
from . import views
app_name = 'search'
urlpatterns = [
path('', views.query_search, name='query_search'),
path('article/<int:ArticleID>/', views.article_detail, name='article_detail')
]
Is there anything I need to change in regards to deployment? All the changes I made to my files regarding deployment are about static and media file directories. What else do I need to change for deployment? As far as I am aware, I have uploaded all files to the hosting server and the app shouldn't be missing any file.
EDIT: added main urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('search/', include('search.urls')),]

Why does this URL work in Django? It is not in urls.py

I've never worked with Django before. I'm taking over a Django project that was started by another programmer, who is now long gone. There is some magic happening in the code that I do not understand. For instance, in this file:
urls.py
I see this:
from django.conf.urls import url, include
from django.contrib import admin
from django.core.urlresolvers import reverse_lazy
from django.views.generic.base import RedirectView
from django.conf import settings
from core import views as core_views
from sugarlab.search.views import validate_collections, create_document, delete_interest, rename_interest, add_url, my_interests
from sugarlab.search.views import content, score, terms
from django.contrib.auth.views import logout as django_logout
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
url(r'^admin/logout/$', django_logout,
{'next_page': '/'}),
url(r'^admin/', admin.site.urls),
url(r'^accounts/logout/$', django_logout,
{'next_page': '/'}),
url(r'^accounts/', include('allauth.urls')),
url(r'^unsecured/$', core_views.home),
The confusing part is these two lines:
from django.conf import settings
url(r'^accounts/', include('allauth.urls')),
"allauths" is some configuration set inside of this file:
settings/common.py
The data looks like this:
'allauth',
'allauth.account',
'allauth.socialaccount',
'django.contrib.auth',
'django.contrib.sites',
# Social/3rd party Authentication apps
'allauth.socialaccount.providers.linkedin_oauth2',
'captcha'
Somehow this is a URL that actually works:
/accounts/signup/
This file is completely blank:
settings/__init__.py
So I've two questions:
how does "import settings" manage to magically import allauths?
how does /accounts/signup/ map to an actual view? I don't see anything in urls.py, nor in settings, that would make me think that /accounts/signup/ is a valid url.
how does /accounts/signup/ map to an actual view? I don't see anything in urls.py, nor in settings, that would make me think that /accounts/signup/ is a valid url.
url(r'^accounts/', include('allauth.urls')),
there is another urls file inside the app called allauth if it's installed by "pip" you can find it in the following directory "lib/python*/site-package/allauth"
= the python version you are using for example 2.7 or 3.5
ps allauth is a well known 3rd party app you can quick google search django allauth and you'll find it
how does "import settings" manage to magically import allauths?
it doesnt import settings is used for something else for example setting static file url like that
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

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