Django on GAE - name 'url' is not defined - django

I've just uploaded my django web app to GAE, but I'm getting an error when I compile it with "dev_appserver.py":
Exception Type: NameError
Exception Value: name 'url' is not defined
Here my urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns(
'',
url(r'^landing/$', 'webpage_dev.dashboard.views.landing'),
)
I'm importing url from django.conf.urls.default. Why appear this error?
Thanks a lot

from django.conf.urls.defaults import patterns, url

Related

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')
]

I have an error in django views.py file, httpresponse

This is my views.py file, in which i have create a small function.
from django.shortcuts import render, HttpResponse
>
> # Create your views here.
>
>
> def Index(request):
> return HttpResponse("Danial")
This is my urls.py file
from django.contrib import admin
from django.urls import path
from api.views import Index
urlpatterns = [
path('admin/', admin.site.urls),
path('', Index),
]
This is Error
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/It%20is%20working...!
Using the URLconf defined in firstProject.urls, Django tried these URL patterns, in this order:
admin/
The current path, It is working...!, 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.
Not sure what can cause the problem but I see some mistakes in your code:
Commonly used is to name methods with first lowercase charracter - first charracter uppervase is used ot name classes, so rename your method to index().
urlpatterns in urls.py is a list so you shouldn't have that comma behind last member of list.
And just question: your app name is api? That is also sort of keyword and I wouldn't use it as an app name. If your app has other name, this can cause the problem so replace 'api' with your app name - with proper path in your folder structure.

NoReverseMatch " " is not a registered namespace

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

404 Error while accessing API with tastypie

Just started with TastyPie to expose the data. Trying to tie together resources using tastypie.Api for urls.py.
But I get this error when I try to access them through localhost:api/**resource.
my api.py:
from tastypie.resources import ModelResource
from idg.models.molecule_dictionary import MoleculeDictionary
class MoleculeDictionaryResource(ModelResource):
class Meta:
queryset = MoleculeDictionary.objects.all()
# resource_name = 'moleculedictionary'
my url.py:
from django.conf.urls import url, include, patterns
from idg.api import MoleculeDictionaryResource
from django.contrib import admin
from tastypie.api import Api
from . import views
dictionary_resource = MoleculeDictionaryResource()
# private_api = Api(api_name='private')
# private_api.register(MoleculeDictionaryResource(), canonical=True)
urlpatterns = [
url(r'^$', views.index, name='index'),
# url(r'^exports/', include('data_exports.urls', namespace='data_exports')),
url(r'^api/', include(dictionary_resource.urls)),
]
Error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/api/moleculedictionary/?format=json
Using the URLconf defined in django_root.urls, Django tried these URL patterns, in this order:
^__debug__/
xadmin/
^idg/
^comments/
^admin/
The current URL, api/moleculedictionary/, 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.
Any suggestions ? I am not sure where I made a mistake. I have followed the tutorial (https://django-tastypie.readthedocs.org/en/latest/tutorial.html)
Uncomment line
resource_name = 'moleculedictionary'
in Your api.py file.
Try this:
private_api = Api(api_name='v1')
private_api.register(MoleculeDictionaryResource(), canonical=True)
url(r'^api/', include(private_api.urls)),
Then you should be able to access it with /api/v1/

Urls.py says my view is not defined (class based view)

This is my urls.py for my project:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^CMS/', include('CMSApp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
and this is my urls.py for my app (CMSApp):
from django.conf.urls import patterns, url
urlpatterns = patterns(
'CMSApp.views',
url(r'^users$', user_list.as_view()),
url(r'^users/(?P<pk>[0-9]+)$', user_detail.as_view()),
)
When I go to
CMS/users
it gives me a name error saying that
name 'user_list' is not defined
Any idea why?
When I do
from CMSApp import views
urlpatterns = patterns(
'',
url(r'^users$', views.user_list.as_view()),
)
it works but I'm just wondering why the former does not work?
It appears that you're using Django 1.8 for your project; the behavior you're trying to use was removed in 1.8, as documented here: https://docs.djangoproject.com/en/1.8/releases/1.8/#django-conf-urls-patterns
You have to import
from CMSApp.views import user_list
Otherwise django won't know user_list is defined.
If you just use user_list without importing it explictly, python will consider it is a local variable and return NameError.
Once user_list is defined in views.py, you have to explicitly tell python to search for it there.