How to solve “Page not found (404)” error in Django?

You are getting the 404 because you haven’t defined a url pattern for http://127.0.0.1:8000/ yet. You should be able to view the admin site at http://127.0.0.1:8000/admin/ and your food posts at http://127.0.0.1:8000/foodPosts/. To add a url pattern for the homepage, uncomment the following entry in your urls.py, and replace homefood.views.home with the path to the … Read more

Django templates folders

Did you set TEMPLATE_DIRS in your settings.py? Check and make sure it is set up correctly with absolute paths. This is how I make sure it is properly set: settings.py PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) TEMPLATE_DIRS = ( # Put strings here, like “/home/html/django_templates” or “C:/www/django/templates”. # Always use forward slashes, even on Windows. # Don’t forget … Read more

Django 404 error-page not found

You are getting the 404 because you haven’t defined a url pattern for http://127.0.0.1:8000/ yet. You should be able to view the admin site at http://127.0.0.1:8000/admin/ and your food posts at http://127.0.0.1:8000/foodPosts/. To add a url pattern for the homepage, uncomment the following entry in your urls.py, and replace homefood.views.home with the path to the … Read more

How to get URL of current page, including parameters, in a template?

Write a custom context processor. e.g. def get_current_path(request): return { ‘current_path’: request.get_full_path() } add a path to that function in your TEMPLATE_CONTEXT_PROCESSORS settings variable, and use it in your template like so: {{ current_path }} If you want to have the full request object in every request, you can use the built-in django.core.context_processors.request context processor, … Read more

How can I list urlpatterns (endpoints) on Django?

If you want a list of all the urls in your project, first you need to install django-extensions You can simply install using command. pip install django-extensions For more information related to package goto django-extensions After that, add django_extensions in INSTALLED_APPS in your settings.py file like this: INSTALLED_APPS = ( … ‘django_extensions’, … ) urls.py … Read more

Django URL Redirect

You can try the Class Based View called RedirectView from django.views.generic.base import RedirectView urlpatterns = patterns(”, url(r’^$’, ‘macmonster.views.home’), #url(r’^macmon_home$’, ‘macmonster.views.home’), url(r’^macmon_output/$’, ‘macmonster.views.output’), url(r’^macmon_about/$’, ‘macmonster.views.about’), url(r’^.*$’, RedirectView.as_view(url=”<url_to_home_view>”, permanent=False), name=”index”) ) Notice how as url in the <url_to_home_view> you need to actually specify the url. permanent=False will return HTTP 302, while permanent=True will return HTTP 301. Alternatively … Read more