Django Page not found (404) error with url config - django

I have a problem with urls config in my Django project, in my root url config i have this:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('main.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^profile/', include('accounts.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
And in my app url config i have this:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'main.view.home', name='main_home'),
url(r'^integrations/$', 'main.view.integrations', name='main_integrations'),
)
But when I go to render this url http://127.0.0.1:8000/integrations/ I obtain an error 404 Not Found.
Thanks by yours answers.

Yes, my app named 'main' and this is my view code:
def home(request):
user = request.user
if not user.is_authenticated():
data = {
'messages': get_messages(request),
}
return render_to_response('home.html', data, context_instance=RequestContext(request))
data = {
'messages': get_messages(request),
}
return render_to_response('home_user.html', data, context_instance=RequestContext(request))
def integrations(request):
return render_to_response('/main/integrations.html', context_instance=RequestContext(request))

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

Redirect to custom 404 html page in DjangoCMS

How can I redirect the user to my custom 404.html page if he enters a wrong path ?
views.py:
from django.shortcuts import render, render_to_response, redirect
from django.template import RequestContext
import threading
def url_redirect(request):
return render_to_response('blog.html', RequestContext(request))
def post_redirect(request):
return render_to_response('post.html', RequestContext(request))
def privacy_redirect(request):
return render_to_response('privacy.html', RequestContext(request))
def terms_conditions_redirect(request):
return render_to_response('terms.html', RequestContext(request))
def page_404(request):
return render_to_response('404.html', RequestContext(request))
urls.py:
from __future__ import print_function
from cms.sitemaps import CMSSitemap
from django.conf.urls import *
from django.conf.urls.i18n import i18n_patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^blog', "website.views.url_redirect", name="url-redirect"),
url(r'^post', "website.views.post_redirect", name="post-redirect"),
url(r'^privacy', "website.views.privacy_redirect", name="privacy-redirect"),
url(r'^terms', "website.views.terms_conditions_redirect", name="terms_conditions-redirect"),
url(r'^404', "website.views.page_404", name="page_404"),
url(r'^', include('cms.urls')),
)
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns
I also have TEMPLATE_DEBUG = True and DEBUG = False in my settings.py file. Any help, please ?
Also, can you guys tell me if those methods from views.py are ok or if they could be rewritten in a better way ?
Thanks
add 404.html near base.html (main folder templates)
and when there is an error 404 , Django itself take your 404 page
in settings.py
DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['yourweb.com']

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

django - invalid syntax (urls.py, line 7)

I'm doing a slight variation on my urls.py from the tutorial where I have the following -
mysite/urls.py -
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^TidalDEV/', include('TidalDEV.urls')),
)
TidalDEV/urls.py -
from django.conf.urls import patterns, url
from TidalDEV import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
url(r'^(?P<pk>[0-9]+)/$', views.tesxml, name='tesxml'),
)
And this is the view in views.py -
def tesxml(self, request, pk, format=None, renderer_context=None):
"""
returns an XML of a jobmst listing
"""
template_vars['jobmst'] = (queryset1, [pk])
template_vars['jobdtl'] = (queryset2, [pk])
template_vars['jobdep'] = (queryset3, [pk])
t = loader.get_template('TidalAPI/templates/xml_template.xml')
c = Context(template_vars)
return HttpResponse(t.render(c), mimetype="text/xml")
When I try to hit my url at http://localhost:8080/TidalDEV/10081/ I get invalid syntax. What is the problem here?
Essentially I need the view to populate a template XML file I built.
You are missing a comma after your index view in TidalDEV/urls.py

Django. Confusion about redirections

Here is my simplified code:
project's urls:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^to_app1/', include('app1.urls')),
url(r'^to_app2/', include('app2.urls')),
)
app1's urls:
from django.conf.urls import *
from app1 import views
urlpatterns = patterns('',
url(r'^$', views.app1Page, name='app1-page'),
url(r'^action/', views.app1_to_app2, name='app1-to-app2-page'),
)
app2's urls:
from django.conf.urls import *
from app2 import views
urlpatterns = patterns('',
url(r'^$', views.app2Page, name='app2-page'),
)
app1's view:
def app1Page(request):
return render(request, 'app1/app1page.html')
def app1_to_app2(request):
# some actions going on here #
return HttpResponseRedirect('/to_app2/')
app2's view:
def app2Page(request):
return render(request, 'app2/app2page.html')
So, I have a button on app1page.html which should sends data through POST function to to_app1/action/ url. In the end it should redirect me to app2page.html, but it does not. It gives me GET 127.0.0.1:8000/to_app2/ HTTP/1.0 200 OK, but I staying on the same page (app1page.html).
There is obviously something I missing, could someone point out it to me?