I have a regular website where HTML5, CSS3, JQUERY and static images have been used.
I also have a Blog written in Django and I would like to integrate it in the website.
I am really new to Django so I was wondering which is the best approach to use.
Should I integrate the website code as part of the Django project or there are some other solutions?
thanks!
You have 2 ways of integrating your current site with Django.
1) You can write API with DjangoRestFramework and make requests with jQuery AJAX in order to get content from Django.
2) You can use your current HTML files as your Django project templates for rendering content from Django.
You can use a Django template. The template defines placeholders and various bits of basic logic (template tags) that regulate how the document should be displayed. Usually, templates are used for producing HTML, but Django templates are equally capable of generating any text-based format.
If you've used a templating engine like ''. They look somehow similar.
<html>
<head>
<title>Ordering notice</title>
</head>
<body>
<h1>Ordering notice</h1>
<p>Dear {{ person_name }},</p>
<p>Thanks for placing an order from {{ company }}. It's scheduled to ship on {{ s\
hip_date|date:"F j, Y" }}.</p>
<p>Here are the items you've ordered:</p>
<ul>
{% for item in item_list %}
<li>{{ item }}</li>{% end for %}
</ul>
{% if ordered_warranty %}
<p>Your warranty information will be included in the packaging.</p>
{% else %}
<p>
You didn't order a warranty, so you're on your own when the products inevitably stop working.
</p>
{% endif %}
<p>Sincerely,<br />{{ company }}</p>
</body>
</html>
check here for more details https://djangobook.com/django-templates/
Related
I'm a beginner in django-oscar and I try to manage a new view on the page.
I've already created two pages with django-oscar dashboard,
https://ibb.co/cM9r0v
and made new buttons in the templates:
Lib/site-packages/oscar/templates/oscar/partials/nav_primary.html
https://gist.github.com/Kalinar/076fc8144869c3b50fc0bc9e52f825e4
I have no idea how to make a good a href="???" to new pages in buttons ... can someone help?
Maybe there is better way to do it, can you explain it to me?
Oscar uses Django's flatpages app, so you can use their template tags to dynamically add in navigation links to the pages you create.
{% load flatpages %}
{% get_flatpages as flatpages %}
{% for page in flatpages %}
<li class="dropdown active">
<a href="{{ page.url }}" class="dropdown-toggle" {% if not expand_dropdown %} data-toggle="dropdown"{% endif %}>
{% trans page.title %}
</a>
</li>
{% endfor %}
You can find more information on the flatpages app in the Django flatpage documentation.
You already know the title and of course the id of the page, and each page is an instance of the model PagePromotion, just query the model for such name/id and use the attribute page_url of the returned instance to send it within a variable in the context of the view that renders the menu.
Then in the template:
href="{{ variable_containing_url }}"
I have installed django-page-cms successfully i think. Like other cms, it is also for creating new pages. But I already have html pages in my project. How to integrate with that?
They want me to put place holder in html page, like:
{% load pages_tags %}
but I think this will bring the content from the already created page in admin
Can anyone tell me how to integrate with my existing pages?
First you need to create page in admin console. Then add the placeholder in your template
like what tutorial saying
{% get_page "news" as news_page %}
{% for new in news_page.get_children %}
<li>
{{ new.publication_date }}
{% show_content new body %}
{% endfor %}
I am learning Django framework, I setup Django on my local host and successfully run my first app, and i downloaded some web app from git and then i tried to understand it but when i opened template html the format used their was completly different from original html format, so i just want to know what scripting, or language they are using to build html files, Here is a sample code
{% extends "admin/change_list.html" %}
{% load i18n %}
{% block object-tools-items %}
{% if not is_popup %}
<a href="{{ recoverlist_url }}" class="recoverlink btn">
<i class="icon-repeat"></i>
{% blocktrans with cl.opts.verbose_name_plural|escape as name %}Recover deleted {{ name }}{% endblocktrans %}</a>
{% endif %}
{{block.super}}
{% endblock %}
I am unable to get what {% %} doing here, Is it a scripting tag just like jsp or something else, If it is a scripting that what kind of script is it ? because normally html files starts with
<head>
tag.
Is it a scripting tag just like jsp or something else
Yes.
If it is a scripting that what kind of script is it ?
Django Template language
because normally html files starts with <head> tag.
It's not HTML. It's a template language that render and outputs HTML.
I'd suggest you go through the Django tutorial.
I read several posts about which bootstrap package use (mainly Crispy-form VS django-toolkit-integration)
But I'm pretty new to Django and I still don't understand what is the real need about these packages.
I mean, Twitter bootstrap is nothing more than css/js files.
So, I thought using bootstrap by linking my Django forms and field to HTML classes (using widgets for .py forms, and directly in .html templates for other fields)
So, what are the benefits of these packages? Is it just convenience or am I really missing something if I choose to not use it?
Thank you for help!
It is just a convenience for forms. It provides you with template tags. Django provides two methods as_p, as_ul and as_table but none of them works smoothly with bootstrap. So you use something like instead.
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% form|crispy %}
{% endblock %}
It also provides some convenient classes to configure in the Django Forms.
So, it is not necessary but it will save you some hassles to configure forms. You can start without them and when the problems come, you will find them useful.
As you yourself said, Twitter Bootstrap are only CSS and JS files that you will apply in your HTML structure and get a new face of its elements. The Bootstrap provides a number of ready-made elements and styles.
Many websites come ready structures and even site templates ready using Bootstrap, as: Bootsnipp, Bootswatch, Wrapbootstrap and many others. The big advantage I see is not wasting time with styles (I'm not disparaging the designers) and focus more on programming.
My final note is: give a chance to the Twitter Bootstrap, it is very useful in creating models and styles faster, more focused on the end product.
Some of this packages are made to be able to write your CSS in Less (the language), and compile them automatically. Also, some may add compression and concatenation of assets.
As Bootstrap is nothing more than a bunch of CSS and Javascript files (and I don't use Less) so far all I need is form integration, which is easily achieved by this:
NOTE: This is a very specific example of my own code, but you get the idea.
<div class="row">
<div class="span6 offset3">
<form id="form-history" class="form-horizontal text-center"
action="/somepath/" method="post">
<fieldset>
<legend><h3>History</h3></legend>
{% for field in form %}
<div class="row">
<div class="span3">{{ field.label }}</div>
<div class="span3">{{ field }}</div>
</div>
{% endfor %}
<div class="row pull-right">
<button class="btn btn-primary btn-large" type="submit">
<i class="icon-ok icon-white"></i> Find
</button>
</div>
</fieldset>
</form>
I have a web application written in Django that has one specific page I'd like to implement a mobile version of the template (and slightly different logic) for. I'd like to be able to implement it ala this sudo code:
def(myView)
do some stuff
if user-is-on-a-mobile-device:
do some stuff
return (my mobile template)
else:
do some stuff
return (my normal template)
I don't have a huge amount of time and I'm pretty early on in my coding learning curve :) - I found what looks to be a very powerful pluggable app called bloom for getting mobile device capablities - http://code.google.com/p/django-bloom/wiki/BloomDevice
However it seems to make a request via JSON to get lots of device specs I don't need, which seems a bit inefficient to me.
Does anyone have a suggest simpler method? My detection doesn't need to be 100%, just iPhone, iPod, android, and mainstream devices...
Does the http_user_agent string have some kind of mobile flag I can check for?
Update:
I just found: http://code.google.com/p/minidetector/
Which seems to do exactly what I want, I'm going to test now. Feel free to tell me i'm wrong!
best practice: use minidetector to add the extra info to the request, then use django's built in request context to pass it to your templates like so.
from django.shortcuts import render_to_response
from django.template import RequestContext
def my_view_on_mobile_and_desktop(request)
.....
render_to_response('regular_template.html',
{'my vars to template':vars},
context_instance=RequestContext(request))
then in your template you are able to introduce stuff like:
<html>
<head>
{% block head %}
<title>blah</title>
{% if request.mobile %}
<link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-mobile.css">
{% else %}
<link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-desktop.css">
{% endif %}
</head>
<body>
<div id="navigation">
{% include "_navigation.html" %}
</div>
{% if not request.mobile %}
<div id="sidebar">
<p> sidebar content not fit for mobile </p>
</div>
{% endif %>
<div id="content">
<article>
{% if not request.mobile %}
<aside>
<p> aside content </p>
</aside>
{% endif %}
<p> article content </p>
</aricle>
</div>
</body>
</html>
go for the fork of minidetecor called django-mobi, it includes documentation on how to use it.
https://pypi.python.org/pypi/django-mobi