I'm learning python and Django coming from PHP. This is all really exciting, and I would love to use Bootstrap with Django to create sexy web pages.
As I understand it (I'm following the Django tutorial on their website), Django uses "apps" which can be included in your settings.py file. I did a quick search and found several bootstrap-themed apps, but have no knowledge on how to pick the right one. Is there a standard app most people use? All I need are the bootstrap.css and bootstrap.js files.
I'm sure I could manually place them in my root, but I'd enjoy an "all inclusive" setup within my Django install.
Re-reading your question, it seems that you're searching for a way to install Twitter Bootstrap as a Django app. While there are some apps out there that facilitate using Twitter Bootstrap with Django, you don't really need to use any of them.
You can simply include the Twitter Bootstrap CSS and JS at either the project or app level and refer to them in your Django templates.
To include Twitter Bootstrap in a Django app, your best bet is to:
Use Static Files
In your settings.py, add the path to Bootstrap (which you should download and place in your Django app under a folder named static:
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/path/to/my_project/my_app/static/',
)
Also, make sure your STATIC_URL prefix is set:
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
Now, download Twitter Bootstrap and place it in the path there:
/path/to/my_project/my_app/static/bootstrap/
Include Twitter Bootstrap in your templates
I would link to Twitter Bootstrap documentation, but there isn't any, really. Your best bet is to take a look at the source of their starter template. Using the Django templating system is a bit beyond the scope of this question, but I'll give you this hint: Anywhere in the starter template where you find a link to a .css or .js, replace it with your STATIC_URL.
So:
<link href="../assets/css/bootstrap.css" rel="stylesheet">
becomes
<link href="{{ STATIC_URL }}/bootstrap/css/bootstrap.css" rel="stylesheet">
I use the starter template as my base.html and include {% block content %} blocks in base.html that can be replaced by the actual content in my templates, which {% extend base.html %}.
Or, use a 3rd party app to guide you
You might investigate is the Django Bootstrap Toolkit, which I have not used myself. I would suggest doing it yourself manually first, however, as a way to explore the project and to really understand what is going on. It's not too hard at all!
Welcome to the world of Python/Django. Like you, after years of doing web development in PHP, I've migrated to creating dynamic websites and robust web applications using this language/framework pair.
One of the really nice features of django is their automatic admin interface. I use an app called Django-Admin-Tools and django-admintools-bootstrap which transforms the default interface into a slick looking admin. https://bitbucket.org/salvator/django-admintools-bootstrap
The easiest way to install django apps or other python modules is by using pip. I would read up on using virtualenv http://readthedocs.org/projects/virtualenv/ to manage your django projects. When you set up a python virtual environment, it allows you to install django apps and python modules in a separate python install on your system.
Then, installing additional django apps is a breeze - pip install django-admin-tools - django grappelli - south - are all some of my favorites.
What if you simply install twitter-bootstrap using pip:
follow the instructions:
https://pypi.python.org/pypi/django-twitter-bootstrap/
for the simple configuration.
None of the mentioned answers worked for me, the simplest way of all I will say is directly add CDNs in your base.html which will be used to extend and you can add it in standalone/static pages as well
Read here for the bootstrap CDN and integration.
Related
Is django compressor default on django install?
I have been having an issue with whitenoise and django. I keep getting this error:
ValueError: Missing staticfiles manifest entry for 'inline.bundle.js'
now all the research I have done points that it may be a whitenoise (im using heroku) and a issue with django compressor. But I do not remember installing such a tech. Is it by default? One of the work arounds suggested is to replace django compressor with another. But how?
Actively researching this, if anyone has had this issue before would appreciate a work around.
This problem belong to angular2/4/5. If you are using angular2/4/5 then it will come because you forget to upload inline script in index.html of python's project.
please add this in index.html file of python's project templates
<script type="text/javascript" src="{{ANGULAR_STATIC}}inline.bundle.js"></script>
In setings.py ANGULAR_STATIC = 'http://localhost:4200/'
You can change url according to you.
I'm trying to combine Sphinx on my Django development server. I know i could better use apache. But I can't use apache due to the fact that the project will be managed by someone else and the project needs to work as simple as possible. Without too many external libraries etc.
So i tried django-docs package and django.static.serve in my url. The HTML pages work, only the look of the pages is just plain html so the CSS isn't included. The documentation on django-docs is really bad and i can't seem to get it to work with the static files Sphinx created. I can't use sphinxdocs as well since it needs haystacks which will add to my external libraries.
I added django-docs to my installed apps and added this to my settings. With projectpath being the path to where my conf.py is located. I'm not sure if the location is right though. But the documentation isn't really clear at what i should fill in on the project path part. And i added the urls in my urlspatterns
DOCS_ROOT = os.path.join(PROJECT_PATH, '../docs/_build/html')
DOCS_ACCESS = 'staff'
url(r'^docs/', include('docs.urls')),
In sphinx doc the static files are located in the docs/_build/html/_static
I found the problem If anyone else has the issue. I had some other folders called static inside my docs folder. When i removed them it worked. So it was just me being a bit stupid.
I'm using the django-disqus package (https://github.com/arthurk/django-disqus) on my web site which works great in dev. However my production server is running on https and when I view the page I get an error and the disqus comments are not loaded. The error shown in the console states that the code is blocked as it is trying to run insecure content from ...discus.com/count.js and ...discus.com/embed.js and the content should be loaded over HTTPS.
Does anyone know how I can solve this? I've had a look at the docs http://django-disqus.readthedocs.org/en/latest/ but cant find this mentioned.
Edit 2 (July 15, 2014):
The newest release of django-disqus has been uploaded to pypi and can be installed through pip or easy_install. This includes templates that use protocol relative urls, so it may now be easily used on sites served from HTTP or HTTPS.
Edit (July 4, 2014):
I've contacted the repository owner and gotten access. The latest merge into master supports protocol relative urls. It can be installed directly from github using pip:
pip install git+https://github.com/arthurk/django-disqus
Original Answer:
Just taking a quick look at the code for django-disqus on github
https://github.com/arthurk/django-disqus/blob/master/disqus/templates/disqus/show_comments.html
It looks like the http protocol is being hard-coded. There is already an open issue on github to support this, however it doesn't look like the code has been touched for a while, which means the author probably isn't interested (also, there are 10 open pull requests, which isn't a great sign either).
https://github.com/arthurk/django-disqus/issues/18
Your easiest solution is this:
In your settings.py file, make sure your template configuration is something like this (I do this on all my projects, so I can easily override the templates in some 3rd party apps, by providing an alternative template in my site:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (os.path.join('path', 'to', 'mydjangoroot', 'myprojectroot', 'templates'),)
Find the django-disqus package on your machine and copy the 'templates' folder, into the same folder as your settings.py file (if you do something like what I've displayed above)
If you just want to use HTTPS, it's a simple matter of opening the template files modifying the protocol used from HTTP to HTTPS.
Why this works
When do you something like call one of the functions that renders a template, (like django.shortcuts.render).
Django uses a series of template loader classes to figure out where exactly your template is located.
The filesystem.Loader, that I have specified first, will first use any directories I have listed in the tuple assigned to TEMPLATE_DIRS.
Second, if the template wasn't found the app_directories.Loader, it will search any of the installed apps, under a subdirectory called 'templates' for a path matching the template requests. This is usually the behavior I prefer, so, as I mentioned above, I can override the templates for a 3rd party app directly from within my project.
I just started working at a place as a front end developer where I need to build Django templates. I never worked with these before, does anyone know where I can download a sample template so I can look through the code structure?
I won't be doing any application development using the Django framework, only taking the variables the developer gives me and incorporating the in the html/css templates I build.
There are lots of Open Source django apps that you could look at for inspiration. One example is Zinnia which is a blogging application - there are dozens of templates in this project, see this directory. There are many other open source django projects on http://github.com and http://bitbucket.org
You should also take a look at the official template documentation, there are lots of snippets there that are very useful.
Here's a nice little tutorial.
http://www.webmonkey.com/2010/02/use_templates_in_django/
Also, Check out the Django Docs.
http://docs.djangoproject.com/en/1.3/ref/templates/builtins/
I am in a team developing a web-based university portal, which will be based on Django. We are still in the exploratory stages, and I am trying to find the best way to lay the project/development environment out.
My initial idea is to develop the system as a Django "app", which contains sub-applications to separate out the different parts of the system. The reason I intended to make these "sub" applications is that they would not have any use outside the parent application whatsoever, so there would be little point in distributing them separately. We envisage that the portal will be installed in multiple locations (at different universities, for example) so the main app can be dropped into a number of Django projects to install it. We therefore have a different repository for each location's project, which is really just a settings.py file defining the installed portal applications, and a urls.py routing the urls to it.
I have started to write some initial code, though, and I've come up against a problem. Some of the code that handles user authentication and profiles seems to be without a home. It doesn't conceptually belong in the portal application as it doesn't relate to the portal's functionality. It also, however, can't go in the project repository - as I would then be duplicating the code over each location's repository. If I then discovered a bug in this code, for example, I would have to manually replicate the fix over all of the location's project files.
My idea for a fix is to make all the project repos a fork of a "master" location project, so that I can pull any changes from that master. I think this is messy though, and it means that I have one more repository to look after.
I'm looking for a better way to achieve this project. Can anyone recommend a solution or a similar example I can take a look at? The problem seems to be that I am developing a Django project rather than just a Django application.
The best way that I have found to go about this is to create applications and then a project to glue them together. Most of my projects have similar apps which are included in each. Emails, notes, action reminders, user auth, etc. My preferred layout is like so:
project/
settings.py
urls.py
views.py
...
apps/
emails/
urls.py
views.py
...
notes/
urls.py
views.py
...
...
apps:
Each of the "apps" stands on its own, and other than a settings.py, does not rely on the project itself (though it can rely on other apps). One of the apps, is the user authentication and management. It has all of the URLs for accomplishing its tasks in apps/auth/urls.py. All of its templates are in apps/auth/templates/auth/. All of its functionality is self-contained, so that when I need to tweak something, I know where to go.
project:
The project/ contains all of the glue required to put these individual apps together into the final project. In my case, I made use heavy of settings.INSTALLED_APPS in project/ to discern which views from the apps were available to me. This way, if I take apps.notes out of my INSTALLED_APPS, everything still works wonderfully, just with no notes.
Maintenance:
This layout/methodology/plan also has long-term positive ramifications. You can re-use any of the apps later on, with almost no work. You can test the system from the bottom up, ensuring that each of the apps works as intended before being integrated into the whole, helping you find/fix bugs quicker. You can implement a new feature without rolling it out to existing instances of the application (if it isn't in INSTALLED_APPS, they can't see it).
I'm sure there are better documented ways of laying out a project, and more widely used ways, but this is the one which has worked best for me so far.
You should take a look at :
Django generic relations
Django reusable apps best practices if you want to re-use
GIT or any other CVS (git is great for maintaining + deployment)
Fabric if you need automated deployments/updates
I usually use this project structure :
/djangoproject
/apps
/main # the main code
/static # each sub app can serve statics
/app1
/static # each sub app can serve statics
/app2...
/scripts # manage.py, wsgi, apache.conf, fabfile.py...
/core # your libraries ...
settings.py
local_settings.py
Each app in /apps have an urls.py thats autoincluded in the main urls.py. And each app can be a git submodule (or svn external)
Also, using git, you can work on different parallels branches (master/dev/customerA/customerB...) and merge updates.
Creating real reusable is not so easy with django.
You can extract the common functionality into a separate module and make your apps depend on it:
my_portal
auth_module
profiles_module
application1 (depends on auth_module)
application2 (depends on auth_module and profiles_module)
I think the fact that a 'classical' Django project appear to 'contain' the apps it's using prevent you from seeing the picture - in fact, it's not necessary. For a project where you're going to have some sort of pluggable modules I'd suggest organizing the apps as eggs and using zc.buildout+djangorecipe to manage everything.
This way you'll be able to keep your modules in a flat one-level structure. Eggs have the ability to specify dependencies, so if you install application1 (see above), auth_module will be installed automatically.
Also it'll be easy to have different configurations deployed to different servers. Suppose, you have server1 which has application1 installed and server2 which has both application1 and application2 installed - you can just have two configs:
server1.cfg:
[buildout]
extends = base_deployment.cfg
eggs += application1
server2.cfg:
[buildout]
extends = base_seployment.cfg
eggs += application1
application2
djangorecipe also allows you to specify different settings files for each buildout config so you'll be able to add the necessary bits to the main project's urls and installed apps settings.
Not to mention, you can also have a separate config for development configuration (with debug=True and Django Debug Toolbar installed, for example).