Reach main urls.py and use another app url - django

I have use 2 apps in my project named post and gallery. In url localhost:8000/en, I am using post.urls specifically "homepage". In homepage, when I want go to {% url "gallery:gallery_detail" "photos" %} it gives me an error. Because even though it takes a keyword "photos" it can not find any pattern specifically "gallery_detail".
My question is how can I reach this anathor app's url ?
My main urls.py:
from django.conf.urls import url,include
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^ckeditor/', include("ckeditor_uploader.urls")),
]
urlpatterns += i18n_patterns(
url(r'^gallery/', include("gallery.urls",namespace="gallery")),
url(r'^', include("post.urls", namespace="post")),
)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My post.urls:
from django.conf.urls import url
from post.views import HomePageDetailView, AboutDetailView, \
CategoryDetailView, SponsorshipDetailView, CommonDetailView, NewsEntryDetailView
urlpatterns = [
url(r'^$', HomePageDetailView.as_view(), name="homepage"),
url(r'^about/(?P<slug>[-_\w]+)/$', AboutDetailView.as_view(),
name="about_detail"),
url(r'^category/(?P<slug>[-_\w]+)/$', CategoryDetailView.as_view(),
name="category_detail"),
url(r'^sponsorship/(?P<slug>[-_\w]+)/$', SponsorshipDetailView.as_view(),
name="sponsorship_detail"),
url(r'^news/(?P<slug>[-_\w]+)/$', NewsEntryDetailView.as_view(),
name="news_detail"),
url(r'^(?P<slug>[-_\w]+)/$', CommonDetailView.as_view(),
name="common_detail"),
]
My gallery.urls:
from django.conf.urls import url
from gallery.views import GalleryDetailView
urlpatterns = [
url(r'^(?P<slug>[-_\w]+)/$', GalleryDetailView.as_view(),
name="galery_detail"),
]

Related

DetailView Django dont work error page not found

When I click on the article I get
How can I fix it?
I tried url, path and other but I have this error
urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('index', include('mainpage.urls')),
path('blog', include('news.urls')),
path('games', include('games.urls')),
path('fortnite', include('fortnite.urls')),
path('contacts', include('contact.urls')),
path('admin', admin.site.urls),
]
news/urls.py
from django.urls import path,include,re_path
from django.conf.urls import url
from . import views
from django.views.generic import ListView, DetailView
from news.models import Articles
urlpatterns = [
path('', ListView.as_view(queryset=Articles.objects.all().order_by('-date')[:20],template_name='news.html')),
path('<int:pk>/', DetailView.as_view(model=Articles, template_name='news/post.html'))
]
First of all please add '/' at end of all your path strings , change it like this
path('index/', include('mainpage.urls')),
path('blog/', include('news.urls')),
path('games/', include('games.urls')),
path('fortnite/', include('fortnite.urls')),
path('contacts/', include('contact.urls')),
path('admin/', admin.site.urls),
]
check if it works or not

Add internationalization to Django URL with prefix of the language coming after the added prefix

I know how to add a general prefix to all urls thanks to this answer https://stackoverflow.com/a/54364454/2219080. But I want to have urls where we have the added prefix always coming before the language prefix (added by i18n_patterns ).
from django.urls import include, path, re_path
from django.views.generic import TemplateView
from django.conf.urls.i18n import i18n_patterns
urlpatterns = [
path("grappelli/", include("grappelli.urls")), # grappelli URLS
path("admin/doc/", include("django.contrib.admindocs.urls")),
path("admin/", admin.site.urls),
path("pages/", include("django.contrib.flatpages.urls")),
re_path(r"^api-auth/", include("rest_framework.urls")),
re_path(r"^accounts/", include("allauth.urls")),
re_path(r"^indicators/", include("indicators.urls", namespace="indicators")),
path("", TemplateView.as_view(template_name="landing.html"), name="landing"),
]
if settings.URL_PREFIX:
urlpatterns = [path(r"{}".format(settings.URL_PREFIX), include(urlpatterns))]
E.g: Having this code above, I would like to have an url https://example.com/mycuteprefix/en/indicators/ instead of the usual https://example.com/en/mycuteprefix/indicators/.
Whenever I apply internationalization to the first urlpatterns I get an error. For example if I try this one bellow:
urlpatterns = [
path("grappelli/", include("grappelli.urls")), # grappelli URLS
path("admin/doc/", include("django.contrib.admindocs.urls")),
path("admin/", admin.site.urls),
path("pages/", include("django.contrib.flatpages.urls")),
re_path(r"^api-auth/", include("rest_framework.urls")),
re_path(r"^accounts/", include("allauth.urls")),
re_path(r"^indicators/", include("indicators.urls", namespace="indicators")),
]
urlpatterns += i18n_patterns(
path("", TemplateView.as_view(template_name="landing.html"), name="landing"),
)
if settings.URL_PREFIX:
urlpatterns = [path(r"{}".format(settings.URL_PREFIX), include(urlpatterns))]
I have this error:
urlpatterns = [path(r"{}".format(settings.URL_PREFIX), include(urlpatterns))]
File "/home/username/.virtualenvs/roject/lib/python3.5/site-packages/django/urls/conf.py", line 52, in include
'Using i18n_patterns in an included URLconf is not allowed.'
django.core.exceptions.ImproperlyConfigured: Using i18n_patterns in an included URLconf is not allowed.
How to achieve that ?

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

In django 1.10, how do I handle my urls now that patterns is deprecated?

from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r"^$", home),
url(r"^storefront/", storefront),
url(r"^sell/", get_entry),
.
ImportError: cannot import name patterns
The above is a snippet of my urls.py, is fixing this just a matter of changing my import statement or will I literally need to rewrite my entire urls.py now that the patterns module has been deprecated?
In django 1.10 the urls can be defined in the following way:-
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
urlpatterns = i18n_patterns(
url("^admin/", include(admin.site.urls)),
)
if settings.USE_MODELTRANSLATION:
urlpatterns += [
url('^i18n/$', set_language, name='set_language'),
]
urlpatterns += [
url("^", include("your_app.urls")),
]
So you dont have to change all your urls. Just place correctly i.e if you are useing I18N place them with admin in urlpatterns = i18n_patterns section else in another section as in example above replace the name with your_app.urls.

How to solve geodjango url conflict for geojson data?

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)