How to combine Django with Jade - python

I'm trying to combine Django with Jade, but I've had some problems.
I have model which is named About. This has a view like this:
def about(request):
return render_to_response('about.jade',{},RequestContext(request))
and in my urls I have:
url(r'about/', views.about),
But it provides an error that the Templates doesn't exist (and yes, it exists). Is it correct to write the url like this?
Any help would be appreciated!

If your getting the big Template does not exist page in your browser, this usually means that django cannot find where you have stored the your template file (irrespective of using jade).
If you ve created a djnago 1.6 project you need to add the following line to settings:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Then create a templates directory inside your app (not project) directory, and put your template there.

Related

How to serve complete htmlapp with django server using views.py

I have a folder which contains complete frontend built in oraclejet, you can get copy from here (http://www.oracle.com/technetwork/developer-tools/jet/downloads/index.html) download base distribution to get an idea. Folder contains index.html js and other files.
To serve this file with django I kept all files from this inside
"myapp/template/myapp" folder. contents from folder
admin
CONTRIBUTING.md
css
index.html
js
LICENSE.md
metadata
package.json
README.md
revnum
scss
THIRDPARTYLICENSE.txt
myproject - contains manage.py
I want to serve all files using django without using webserver or static mechanism provided by django, as I don't want to modify anything frontend files
Now, in Flask I simply did this, and it worked no issues.
#app.route("/demo/<path:path>", methods=['GET'])
def static_resource(path):
return send_from_directory('./demo/', path)
But how to achieve same thing in Django ?
urls.py
urlpatterns
re_path (r'^$', views.index , name='index'),
re_path (r'^(?P.*)$',views.test_files1 , name='name' )
views.py
def test_files(request , name):
fname = "/home/archit/django_src/mysite/myapp/templates/myapp/" + name
fsock = open(fname,"rb")
return HttpResponse(fsock)
I think there should be something simliar to "send_from_directory('./demo/', path)" in django as well
Please Don't suggest answer for with below solutions as answers already available on stackoverflow
How to serve web pages with apache server
Don't want to change even single line with default oraclejet code, so adding {{ % static }} in html files not solution for me

Filepath for Django Extending Template

Here is my template path
webapp/
|__templates/
|__frontpage/
| |__home.html
|__home_base.html
In home.html, it has:
{% extends "home_base.html" %}
This file structure will work. However, I want to put home_base.html inside frontpage/, as this makes more sense. However, Django will report home_base.html TEMPLATE DOES NOT EXIST, if home_base.html is moved to frontpage/.
The error says it cannot find the home_base.html file under templates/ folder. Since the home_base.html is moved to frontpage/, why doesn't it search for home_base.html inside frontpage/ first? Any configurations I am missing?
You need to do the following for template to be extended.
{% extends "frontpage/home_base.html" %}
Django does not have an idea where you have have moved your template. It will look for the template according to the templates path you have defined in your settings.
The template loader will look for template in the directories defined in the DIRS setting in the TEMPLATES settings.
From the DIRS setting documentation:
Directories where the engine should look for template source files, in
search order.
Also, if frontpage was an app and you had placed your template in the frontpage app instead of the templates folder, then you can set the APP_DIRS settings to be True. This will tell Django to find the templates in the individual apps also.
From the APP_DIRS setting documentation:
Whether the engine should look for template source files inside
installed applications.

Serving static files from root of Django development server

My goal is to have an Angular project being served from the root of my development server. The files will be completely static as far as Django is concerned, no Django template processing is needed. The angular project will then make resource calls to a Django project located at /api/ on the same development server, which will then return json results generated from a view for the Angular project to process.
I assumed it would be as easy as adding the following to my urls.py file.
url(r'^/', 'django.views.static.serve', {
'document_root':'/Users/kyle/Development/project/site/app',
}),
Or
+ static("^/$", document_root="/Users/kyle/Development/project/site/app")
To the end of the urlpatterns.
With /project/site/app being the directory with the Angularjs files.
However, both of these leave me with 404 errors.
I'm open to changing the structure of the project if a more obvious solution exists.
You need to serve both index.html and your static files on / which is done like this in Django 1.10:
from django.contrib.staticfiles.views import serve
from django.views.generic import RedirectView
urlpatterns = [
# / routes to index.html
url(r'^$', serve,
kwargs={'path': 'index.html'}),
# static files (*.css, *.js, *.jpg etc.) served on /
url(r'^(?!/static/.*)(?P<path>.*\..*)$',
RedirectView.as_view(url='/static/%(path)s')),
]
See this answer where I wrote a more complete explanation of such a configuration – especially if you want to use it for production.
It turned out that it was a combination of 2 things, as shavenwarthog said, it shouldn't have the slash. Also, it needed a regular expression to direct it to the file. The final line ended up being:
url(r'^(?P<path>.*)$', 'django.views.static.serve', {
'document_root':'/Users/kyle/Development/project/site/app',
}),
I can then access files like
http://localhost/beer.jpg
note that by default Django won't serve a directory listing. Do you still get a 404 if file /Users/kyle/Development/project/site/app/beer.jpg doesn't appear as http://localhost/beer.jpg ?
in urls.py URLs don't start with a slash; compare url(r'beer') with url(r'^/beer')
I suggest just going for the standard STATIC support. It's awkward, but lets you serve file simply during development, and switch to a 3rd party server (ie Nginx) for production:
https://docs.djangoproject.com/en/dev/howto/static-files/

No Module named Contact Error-Chapter 7 Exercise in Django Book

I've received the following error when trying to set up a contact subdirectory inside the mysite directory.
Here is how I structured the urls.py script:
from mysite.contact import views as contact_views
(r'^contact/$', contact_views.contact),
Do I need to change anything in the settings.py TEMPLATE_DIRS, so, I can call the contact_form.html template correctly?
Yes, template_dirs needs to contain the path to the templates folder.

How do I use generic views in Django?

I am trying to use Django's generic views to make a user registration page. I have the following code in my app's urls.py
from django.conf.urls.defaults import *
from models import Ticket
urlpatterns = patterns('',
(r'^new/$', 'django.views.generic.create_update.create_object', { 'model': User } ),
)
Now when I go to that url I get the following:
TemplateDoesNotExist at /new/
auth/user_form.html
I have tried making a template to match it but I keep getting that message. Any advice?
Also, I assumed that this would make the template for me. What do I actually put in the template file when I make it?
EDIT: Coming from rails I was mixing templates and views. I am still stuck but I think I need to make a function in my view. Something like:
def user_form:
#stuff
You will need to create the template file using the format <app_label>/<model_name>_form.html. Place this template into the template directory as defined in TEMPLATE_DIRS in your settings file.
In the template file itself, you will need to use the context as defined by the standard ModelForm.
{{ form.name.label_tag }} {{ form.name }}
i don't think you need to create a view function. you need to provide a template (auth/user_form.html in this case, is also configurable) in your templates directory. The template needs to contain the necessary fields to create the new user object.
see the docs for this.
ans also check out this similar discussion

Categories

Resources