How to solve geodjango url conflict for geojson data? - django

I have the urls
url(r'^geojson.data/$', geojson_provinces, name='geojson_provinces'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Account,properties=('species','listvalues',)), name='data'),
url(r'^extdata.data/$', existingData_APFT, name='existingData_APFT'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Existing_Data_APFT,properties=('species','listvalues',)), name='extdata'),
FRom the two urls Im displaying different geojson data on map. But When I run
http://localhost:8000/extdata.data/
The geojson data of (geojson.data) is getting dispalyed. I read from the docs that django checks the order but can it be done order independent?
urls.py
from django.conf.urls import url,include,patterns
from django.contrib.gis import admin
from pft.views import*
from django.conf import settings
from django.conf.urls.static import static
from pft.models import Account,Existing_Data_APFT
from django.views.generic import TemplateView
from djgeojson.views import GeoJSONLayerView
urlpatterns =patterns('',
url(r'^admin/', admin.site.urls),
#url(r'',include('pft.urls')),
url(r'^$', 'django.contrib.auth.views.login'),
url(r'^logout/$', logout_page),
url(r'^accounts/login/$', 'django.contrib.auth.views.login'), # If user is not login it will redirect to login page
url(r'^register/$', register),
url(r'^register/success/$', register_success),
url(r'^home/$', home),
url(r'^$',geojson),
url(r'^index/',index),
url(r'^submit', submit),
url(r'^pft',pft),
url(r'^geojson.data/$', geojson_provinces, name='geojson_provinces'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Account,properties=('species','listvalues',)), name='data'),
url(r'^extdata.data/$', existingData_APFT, name='existingData_APFT'),
url(r'^data.data/$', GeoJSONLayerView.as_view(model=Existing_Data_APFT,properties=('species','listvalues',)), name='extdata'),
url(r'^potpft.data/$', TemplateView.as_view(template_name='pot_pft.html'), name='extract_raster_points'),
url(r'^datapotg.geojson/$', GeoJSONLayerView.as_view(model=Account,properties=('species','rastvalues',)), name='datapotg'),
url(r'^csv',add_multiple_accounts,name='add_multiple_accounts'),
url(r'^traits',add_traits,name='add_traits'),
(r'^', include('pft.urls')),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Related

Preview custom 500 error page in Wagtail?

I've made custom 500 and 404 error pages in Wagtail. I can preview the 404 page by typing in a false url. I'm just wondering how I can preview the 500 page?
The custom page has links to static images that I need to check are working.
My urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from wagtail.contrib.sitemaps.views import sitemap
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
admin.autodiscover()
urlpatterns = [
url(r'django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^sitemap\.xml$', sitemap),
url(r'', include('puput.urls')),
url(r'', include(wagtail_urls)),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if 'debug_toolbar' in settings.INSTALLED_APPS:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
The answer at https://stackoverflow.com/a/24660486/823020 has most of these details. You can make a view that raises a 500 error.
You can add a views.py to any app. In that file (taken directly from the linked answer):
from django.http import HttpResponseServerError
def my_test_500_view(request):
# Return an "Internal Server Error" 500 response code.
return HttpResponseServerError()
Supplement this in your urls.py with:
from django.conf import settings
from django.urls import path
# or for Django 1.x do
# from django.urls import url
from myapp import views
urlpatterns = [
# original content here
]
if settings.DEBUG:
urlpatterns += [
path('test_500/', views.my_test_500_view, name="test_500"),
# or for Django 1.x do
# url(r'^test_500/$', views.my_test_500_view, name="test_500"),
]
If it's not directly related to any Wagtail pages, then a utils Django app can work well for generic shared code.
Just add url with TemplateView to your urls.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
url(r"^500/", TemplateView.as_view(template_name='500.html')),
]

Getting error : 'admin' is not a registered namespace, on trying to access the password reset view

I created a password reset view following this tutorial.
I used the urls for class based views since django uses CBVs in the version 2.1 and above. It seemed to work fine, but today I am getting the above stated error. I have commented the admin url, on uncommenting it the password_reset view works, but through the django registration and uses it's own template regardless of the templates I have created. Why am I getting this problem suddenly when it worked fine earlier?
urls.py
from django.conf.urls import url,include
from django.contrib import admin
from NewApp import views
from django.conf import settings
from django.conf.urls.static import serve
from django.urls import path
urlpatterns = [
url(r'^$', views.user_login , name='user_login'),
url(r'^NewApp/', include('NewApp.urls', namespace="NewApp")),
path('', include('django.contrib.auth.urls')),
]
NewApp/urls.py
from django.conf.urls import url,include
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
app_name = 'NewApp'
urlpatterns = [
path('password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
Apart from these, I have only used templates which are just copied from the link I have mentioned.
views for password reset need to access the admin by default. So you have to include the admin site to urls.py:
from django.urls import path
from django.contrib import admin
urlpatterns = [
...
path('admin/', admin.site.urls),
...
]

Django Rest Framework URL Mapping for multiple apps

I have a django-rest project called main and under it I have created an app called users. So, my project has the files :-
main/main/urls.py
and
main/users/urls.py
In users/urls.py I have
from django.conf.urls import url, include
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
and in the main/main/urls.py I have
from django.conf.urls import url
from django.contrib import admin
from users import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users.urls),
]
However, I keep getting the error NameError: name 'users' is not defined. What is the correct way to set up urls when I have multiple apps? I would like to have a urls.py file for each app that is independent of the project. And in the root urls.py would include routing to different apps.
You import url not user, can try it
from users import urls as users_url
# ^^^^^^^^
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users_url),
# ^^^^^^^
]
but better:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', include('users.url')),
# ^^^^^^^
]
more details including-other-urlconfs

TypeError: 'function' object has no attribute 'getitem' django url part

hi I started to learn django newly. I want to upload a photo to my website. I did the other parts but in urls part I got error like this. What should I do?
from django.contrib import admin
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from bildirge.views import contact, home
from bildirge.urls import *
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
from bildirge.views import ProfileImageView, ProfileDetailView, ProfileImageIndexView
urlpatterns = patterns[
url(r'^contact/', contact),
url(r'^home/', home),
url(r'^$', ProfileImageIndexView.as_view(), name='home'),
url(r'^upload/', ProfileImageView.as_view(), name='profile_image_upload'),
url(
r'^uploaded/(?P<pk>\d+)/$', ProfileDetailView.as_view(),
name='profile_image'),
url(r'^admin/', include(admin.site.urls))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And error is
TypeError: 'function' object has no attribute 'getitem'
thanks in advance
You've got a stray patterns before the opening of the list of URLs. Remove that.

what's wrong with my urls.py in this example?

I just installed userena, and had the example working from the tutorial, but as soon as I added in a single line in URLS.py, I'm getting an error. In the example below, I added the line mapping the home function from views.py
Now the issue I'm having is that when I go to 127.0.0.1/8000, I get TypeError: string is not callable, but then oddly, if I go to accounts/signup or accounts/signin, I am getting the template that should be appearing if i go to 127.0.0.1/8000.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from accounts import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Here is my accounts/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
def home(request):
return render('homepage.html')
You need to remove the quotes in the url and import that view
from accounts.views import home
urlpatterns = patterns('',
url(r"^$", home),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
You can steel use the strings in the url() but you must use the format 'app.views.viewname'
urlpatterns = patterns('',
url(r"^$", 'accounts.views.home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Or name the module in the first argument as string to patterns()
urlpatterns = patterns('accounts.views',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
the issue was I forgot to include the request in the return render.
The correct answer is that render is being called incorrectly. Actually, the views.py file would raise a SyntaxError, but we'll let that slide :)
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'homepage.html')