Mapping template folder in Django - python

I am not able to map html files from the directory, however i've followed all the instructions.
Following is my project urls.py:-
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
url(r'^articles/', include('article.urls')),
)
Views.py in app:-
from django.shortcuts import render_to_response
from article.models import Article
def articles(request):
return render_to_response('articles.html',{'articles':
Article.objects.all()})
def article(request, article_id=1):
return render_to_response('article.html',{'article':
Article.objects.get(id=article_id)})
Urls.py in app:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^all/$', 'artilce.views.articles'),
url(r'^get/(?P<article_id>\d+)/$', 'article.view.article'),
)
Settings.py shows exact location of template folder as:-
TEMPLATE_DIRS = (
'C:\Python27\Scripts\django_test\article\templates',
Kindly advise. It shows article on 404 page but couldn't map.

You need to use unix-style forward slashes for your path, even on Windows. See the doc page here: https://docs.djangoproject.com/en/1.6/ref/settings/#template-dirs

Related

Django runserver

When I run my django server this occurs:
Page not found (404)
Request Method:GETRequest URL:http://127.0.0.1:8000/myapp/
Using the URLconf defined in djproject.urls, Django tried these URL patterns, in this order:
admin/
The current path, myapp/, 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.
This is my djproject urls.py file:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('admin/', admin.site.urls), path('myapp/',
include('myapp.urls'))]
this is myapp urls.py:
from django.urls import path
from . import views
urlpatterns = [path('', views.home, name='my app-home')]
my app views.py:
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return (HttpResponse('<h1>Hello</h1>'), )
You have to remove the comma from the end of the line HttpResponse

Saved image cannot be displayed in Django filer

I'm having a problem viewing uploaded images in Django filer.
The images can be uploaded without any problems and will also be saved in the specified folder.
After that it will be shown as follows:
Screenshot filer page
When I then click on expand I get the following error:
Screenshot Error
My urls.py looks like this:
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import include, re_path, path
from django.views.i18n import JavaScriptCatalog
from django.contrib.sitemaps.views import sitemap
from cms.sitemaps import CMSSitemap
from djangocms_blog.sitemaps import BlogSitemap
urlpatterns = i18n_patterns(
re_path(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
)
urlpatterns += staticfiles_urlpatterns()
urlpatterns += i18n_patterns(
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('cms.urls')),
re_path(r'^sitemap\.xml$', sitemap, {'sitemaps': {'cmspages': CMSSitemap, 'blog': BlogSitemap,}}),
re_path(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')),
re_path(r'^filer/', include('filer.urls')),
)
admin.site.enable_nav_sidebar = False
I installed django-filer according to the documentation. But apparently it doesn't work.
How can I solve this problem?
Examine the error page closely, you can see it's a 404 error, meaning you're requesting a non existing path.
The current path .../example.jpg/, didn't match any of these.
Remove the trailing slash after example.jpg from your requesting url, then it should work properly.

Django: Page Not Found

I'm getting a 404 from Django and none of the previous posts on the subject (of which there are many) seem to have helped.
views.py
from django.views.generic.detail import DetailView, SingleObjectMixin
from app.models import MyModel
class MyDetails(DetailView, SingleObjectMixin):
template_name = "app/my_view.html"
model = MyModel
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from app.views import MainList, post_form_upload, MyDetails
urlpatterns = [
url(r'^$', MainList.as_view(), name="main_list"),
url(r'^add_something$', post_form_upload, name="add_something"),
url(r'^my_details/(?P<pk>\d+)$', MyDetails.as_view(), name="my_details"),
]
app/urls.py
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
(r'^$', include('app.urls')),
url(r'^admin/', include(admin.site.urls)),
]
when I enter the URL: http://localhost:8000/my_details, I get the following error:
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
^$ [name='main_list']
^add_something$ [name='add_something']
^my_details/(?P<pk>\d+)$ [name='my_details']
The current URL, my_details, didn't match any of these.
The other two URLs (/ and /add_something) work fine.
First of all, not sure how you are not running into this issue, but in your app/urls.py
(r'^$', include('app.urls')),
should be
(r'^/', include('app.urls')),
$ indicates the end of regex pattern, and anything inside include() would not be included.
Secondly, None of your URL patterns match my_details/ They only match my_details/<id>
A little more on the documentation of URL regex, etc..

Django - URL routing issues (cannot import name 'urls')

I am following the Django tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/, and am trying to get the index view to show up. I've tried the code specified on the page verbatim but keep on getting errors.
polls/urls.py:
from django.conf.urls import patterns, urls
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and finally, the index method in views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h1>Hello world!</h1>");
I'm not sure what I'm doing wrong. I keep getting an error that says "cannot import name 'urls'." any help would be appreciated!
The problem is in your import statement - there is no urls function in django.conf.urls package.
Replace:
from django.conf.urls import patterns, urls
with:
from django.conf.urls import patterns, url

Created pages/subpages in Django

I am currently learning Django and I am trying to create a few pages and subpages on my site.
project
urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'home/', include('home.urls', namespace = 'home')),
url(r'about/', include('about.views', namespace = 'About_page')),
)
I first create a home page:
urls.py:
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
views.py:
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response
def index(request):
return render(request,'homepage_template/home.html')
Next I tried to create an about page but I get this error:
Exception Type: ImproperlyConfigured
Exception Value:
The included urlconf <module 'about.views' from '/home/bradford/Development/Django/pub_pic/about/views.pyc'> doesn't have any patterns in it
This is what my about app looks like:
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response
def index(request):
return render(request,'About_template/about.html')
I don't have a urls.py but I thought the line
url(r'about/', include('about.views', namespace = 'About_page')),
would directly include the about.views. However I was wrong because this never called my index() function in about/views.py
I later changed the url() in pub_pic/urls.py to:
url(r'about/', include('about.views.index', namespace = 'About_page')),
But I got this error:
Exception Value:
No module named index
I think this caused due to the fact that only about.views is a module, not index()
I'm not quite sure how to create pages or subpages and best practices for a well structured project. Could someone give me some suggestions please? Thank you!
In your project urls.py, you cannot include an application's view files in urlpatterns.
url(r'about/', include('about.urls', namespace = 'About_page')), )
And in about/urls.py
from django.conf.urls import patterns, url
from about import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),)
If there is only one url in about/urls.py, then you can directly put it in urls.py of project.
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'home/', include('home.urls', namespace = 'home')),
url(r'^about/$', 'about.views.index', name="About_page_index"),
)

Categories

Resources