<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Les notes de Golgotha &#187; python</title>
	<atom:link href="https://blog.developpez.com/cs-blog/ptag/python/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/cs-blog</link>
	<description>Quelques articles sur le développement.</description>
	<lastBuildDate>Thu, 16 May 2013 04:53:41 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Django &#8211; Afficher les données d&#8217;une table</title>
		<link>https://blog.developpez.com/cs-blog/p11976/framework/django-afficher-les-donnees-dune-table</link>
		<comments>https://blog.developpez.com/cs-blog/p11976/framework/django-afficher-les-donnees-dune-table#comments</comments>
		<pubDate>Thu, 16 May 2013 04:53:41 +0000</pubDate>
		<dc:creator><![CDATA[Golgotha]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/cs-blog/?p=160</guid>
		<description><![CDATA[Dans l&#8217;article précédent, nous avons créée deux notes, voyons comment les afficher sur notre page web. Pour ce faire, nous allons utiliser une boucle for dans le template. Avant tout, il faut sélectionner les notes dans la contrôleur et les &#8230; <a href="https://blog.developpez.com/cs-blog/p11976/framework/django-afficher-les-donnees-dune-table">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify">Dans l&rsquo;article précédent, nous avons créée deux notes, voyons comment les afficher sur notre page web. Pour ce faire, nous allons utiliser une boucle for dans le template.</p>
<p>Avant tout, il faut sélectionner les notes dans la contrôleur et les envoyer au template :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">shortcuts</span> <span style="color: #ff7700;font-weight:bold;">import</span> render_to_response <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> myapp.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> Note <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> home<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notes <span style="color: #66cc66;">=</span> Note.<span style="color: black;">objects</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'home.html'</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'notes'</span>: notes<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></div></div>
<p>Les deux éléments importants à retenir :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">notes <span style="color: #66cc66;">=</span> Note.<span style="color: black;">objects</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>Je récupère tout les enregistrements <strong>Note</strong> (équivalent à select * from myapp_note)</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: black;">&#123;</span><span style="color: #483d8b;">'notes'</span>: notes<span style="color: black;">&#125;</span></div></div>
<p>la variable notes est transmit sous le nom &lsquo;notes&rsquo; à mon template (la page HTML).</p>
<p>Comme vous le voyez, nous n&rsquo;utilisons pas de SQL pour récupérer les enregistrements en base de données. Ici tout est objet.<br />
</br><br />
Voyons maintenant comment afficher les notes dans mon fichier html :</p>
<div class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% extends &quot;base.html&quot; %} <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% block content %} <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Todo :<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span>&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% for n in notes %} <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>{{ n.libelle }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endfor %} <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endblock %}</div></div>
<p><strong>Django</strong> nous permet d’itérer sur l&rsquo;objet notes avec une boucle <strong>{% for n in notes %}</strong>. l&rsquo;affichage de l&rsquo;attribut se fait avec <strong>{{ objet.attribut }}</strong>.</p>
<p>Et voici le résultat :</p>
<p><img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-9.png" alt="affichage de notes" /></p>
<p>Dans le prochain article, nous verrons comment ajouter, supprimer et modifier un objet avec <strong>Django</strong>.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django &#8211; le site d&#8217;administration</title>
		<link>https://blog.developpez.com/cs-blog/p11974/framework/django-le-site-dadministration</link>
		<comments>https://blog.developpez.com/cs-blog/p11974/framework/django-le-site-dadministration#comments</comments>
		<pubDate>Wed, 15 May 2013 18:48:53 +0000</pubDate>
		<dc:creator><![CDATA[Golgotha]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/cs-blog/?p=153</guid>
		<description><![CDATA[Cinquième article consacré à Django. Django est livré avec un site d&#8217;administration de données, un peux comme phpMyAdmin. Il permet d&#8217;ajouter/créer et supprimer des objets en base de données. Cela est très utile quand vous débuter avec votre site web, &#8230; <a href="https://blog.developpez.com/cs-blog/p11974/framework/django-le-site-dadministration">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Cinquième article consacré à <strong>Django</strong>.</p>
<p style="text-align: justify">
<strong>Django</strong> est livré avec un site d&rsquo;administration de données, un peux comme <strong>phpMyAdmin</strong>. Il permet d&rsquo;ajouter/créer et supprimer des objets en base de données. Cela est très utile quand vous débuter avec votre site web, et que tout n&rsquo;est pas encore terminé. Voyons comment il fonctionne.</p>
<p style="text-align: justify">
Pour commencer, il faut déclarer l&rsquo;objet <strong>Note</strong> (créée dans l&rsquo;article précédent) dans l&rsquo;administration, sinon il n&rsquo;apparaitra pas. Pour cela, créer un nouveau fichier <strong>admin.py</strong> dans le répertoire <strong>/mysite/myapp</strong>
</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> myapp.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> Note <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span> <span style="color: #ff7700;font-weight:bold;">import</span> admin <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; admin.<span style="color: #dc143c;">site</span>.<span style="color: black;">register</span><span style="color: black;">&#40;</span>Note<span style="color: black;">&#41;</span></div></div>
<p>Ensuite, il faut paramétrer différent fichier pour que l&rsquo;administration soit en place :<br />
Dans <strong>settings.py</strong>, dé-commentez les lignes relatives à l&rsquo;administration :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INSTALLED_APPS <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.auth'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.contenttypes'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.sessions'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.sites'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.messages'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.staticfiles'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'myapp'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Uncomment the next line to enable the admin: </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #483d8b;">'django.contrib.admin'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Uncomment the next line to enable admin documentation: </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #483d8b;">'django.contrib.admindocs'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#41;</span></div></div>
<p>Ainsi que dans le fichier des urls :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> patterns<span style="color: #66cc66;">,</span> include<span style="color: #66cc66;">,</span> url <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span> <span style="color: #ff7700;font-weight:bold;">import</span> admin <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; admin.<span style="color: black;">autodiscover</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; urlpatterns <span style="color: #66cc66;">=</span> patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^$'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'mysite.myapp.views.home'</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'home'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^admin/'</span><span style="color: #66cc66;">,</span> include<span style="color: black;">&#40;</span>admin.<span style="color: #dc143c;">site</span>.<span style="color: black;">urls</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#41;</span></div></div>
<p>Resynchronisez la base de données avec la commande <strong>syncdb</strong>.</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; manage.py syncdb <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating tables ... <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table django_admin_log <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Installing custom SQL ... <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Installing indexes ... <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; No fixtures found.</div></div>
<p>Redémarrer votre serveur, puis rendez-vous à la page <strong>http://127.0.0.1:8000/admin/</strong>.<br />
</br><br />
Connecter vous avec le compte que vous avez crée précédemment en ligne de commande. (avec <strong>syncdb</strong>) Si tout ce passe bien vous arriverez à cette page :</p>
<p><img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-6.png" alt="admin django" /></p>
<p>Nous pouvons directement créer des Notes ici :<br />
<img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-7.png" alt="add notes" /><br />
<em>Cliquez sur Notes, puis Add Note.</em></p>
<p>Créer deux notes.</p>
<p><img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-8.png" alt="creation notes" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django &#8211; le modèle de données</title>
		<link>https://blog.developpez.com/cs-blog/p11964/framework/django-le-modele-de-donnees</link>
		<comments>https://blog.developpez.com/cs-blog/p11964/framework/django-le-modele-de-donnees#comments</comments>
		<pubDate>Sat, 11 May 2013 05:33:10 +0000</pubDate>
		<dc:creator><![CDATA[Golgotha]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/cs-blog/?p=148</guid>
		<description><![CDATA[Bienvenu dans le quatrième article consacré à Django. Nous allons voir une des forces de Django : l&#8217;abstraction de la base de données. Django utilise un fichier modele.py, qui défini clairement les objets du projet. Tout comme le Python, Django &#8230; <a href="https://blog.developpez.com/cs-blog/p11964/framework/django-le-modele-de-donnees">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Bienvenu dans le quatrième article consacré à <strong>Django</strong>.</p>
<p style="text-align: justify">
Nous allons voir une des forces de <strong>Django</strong> : l&rsquo;abstraction de la base de données. <strong>Django</strong> utilise un fichier <strong>modele.py</strong>, qui défini clairement les objets du projet. Tout comme le Python, <strong>Django</strong> est très orienté objet. Ici nous ne verrons pas de requêtes sql, de connexion à la base de données, ou de <em>commit</em> Cela pourrait paraître comme une limitation mais <strong>Django</strong> à été pensé et construit pour cela, vous avez donc une multitude d&rsquo;outils pour aller aussi loin que vous le voulez.
</p>
<p>Pour illustrer cela, nous allons créer une liste de note puis l&rsquo;afficher.<br />
<em>Je considère que vous avez une base de donnée MySQL installée sur votre machine.</em></p>
<p>Premièrement, il faut définir la base de données dans le fichier <strong>settings.py</strong> :</p>
<p><span id="more-148"></span></p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DATABASES <span style="color: #66cc66;">=</span> <span style="color: black;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'default'</span>: <span style="color: black;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'ENGINE'</span>: &nbsp; <span style="color: #483d8b;">'mysql'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'NAME'</span>: &nbsp; &nbsp; <span style="color: #483d8b;">'django_db'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'USER'</span>: &nbsp; &nbsp; <span style="color: #483d8b;">'root'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'PASSWORD'</span>: <span style="color: #483d8b;">'root'</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'HOST'</span>: <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'PORT'</span>: <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#125;</span></div></div>
<p style="text-align: justify">
Donc ici j&rsquo;ai créer un schéma qui se nomme <strong>django_db</strong> sur ma base de donnée MySQL.<br />
Ces données sont utilisé pour que <strong>Django</strong> dialogue avec votre base de données, comme vous pouvez le voir, votre projet pourra aussi bien tournée sur MySQL que sqlite3 ou encore oracle sans changer une seule ligne de code !
</p>
<p>Maintenant que le paramétrage est fait, ouvrer le fichier <strong>models.py</strong> et copier le code suivant :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">class</span> Note<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; libelle <span style="color: #66cc66;">=</span> models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length<span style="color: #66cc66;">=</span><span style="color: #ff4500;">200</span><span style="color: black;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status <span style="color: #66cc66;">=</span> &nbsp;models.<span style="color: black;">IntegerField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">libelle</span></div></div>
<p>Il faut déclarer notre application dans le fichier de configuration :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INSTALLED_APPS <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.auth'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.contenttypes'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.sessions'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.sites'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.messages'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'django.contrib.staticfiles'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'myapp'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Uncomment the next line to enable the admin: </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># 'django.contrib.admin', </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Uncomment the next line to enable admin documentation: </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># 'django.contrib.admindocs', </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#41;</span></div></div>
<p style="text-align: justify">
Je déclare donc une nouvelle class Note avec deux attributs : <strong>libelle</strong> qui sera le texte de la note et le status pour savoir si c&rsquo;est fait ou pas. la fonction __unicode__ nous servira pour plus tard, c&rsquo;est le texte qui sera renvoyé pour l&rsquo;objet.</p>
<p>Nous arrivons à une phase typique de <strong>Django</strong> : le liens avec la base de données à été défini ainsi que notre modèle, <strong>Django</strong> est donc capable de créer la base de données.<br />
<br />
Taper la commande suivante :</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; manage.py syncdb</div></div>
<p><em>Penser à installer le support de mysql pour python. (http://www.codegood.com/archives/129)</em></p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d:\DEV\mysite&amp;gt<span style="color: #66cc66;">;</span>manage.<span style="color: black;">py</span> syncdb<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating tables ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">Creating</span> table auth_permission<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table auth_group_permissions<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table auth_group<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table auth_user_user_permissions<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table auth_user_groups<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table auth_user<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table auth_message<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table django_content_type<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table django_session<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table django_site<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creating table myapp_note<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; You just installed Django<span style="color: #483d8b;">'s auth system, which means you don'</span>t have <span style="color: #008000;">any</span> superuse<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs defined.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">Would</span> you like to create one now? <span style="color: black;">&#40;</span>yes/no<span style="color: black;">&#41;</span>: yes<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Username <span style="color: black;">&#40;</span>Leave blank to use <span style="color: #483d8b;">'administrateur'</span><span style="color: black;">&#41;</span>: root<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; E-mail address: cedric.<span style="color: black;">salaun</span><span style="color: #66cc66;">@</span>gmail.<span style="color: black;">com</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Password:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: black;">&#40;</span>again<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Superuser created successfully.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">Installing</span> custom SQL ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">Installing</span> indexes ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">No</span> fixtures found.</div></div>
<p style="text-align: justify">
Comme vous pouvez le constater, <strong>Django</strong> met en place plusieurs tables que nous n&rsquo;avons pas défini, ce sont des tables &laquo;&nbsp;système&nbsp;&raquo; qui sont là pour nous facilité la vie, par exemple pour la gestion des utilisateurs, la gestions des droits etc&#8230; La seule ligne qui nous intéresse vraiment ici c&rsquo;est <strong>Creating table myapp_note</strong> Pour la création de notre table Note.</p>
<p style="text-align: justify">
Nous avons vu dans cette article, comment mettre en place une base de données avec <strong>Django</strong>, nous verrons dans le prochaine article comment manipuler les tables et ajouter des données avec l&rsquo;administration de Django.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django &#8211; Introduction aux templates</title>
		<link>https://blog.developpez.com/cs-blog/p11961/framework/django-introduction-aux-templates</link>
		<comments>https://blog.developpez.com/cs-blog/p11961/framework/django-introduction-aux-templates#comments</comments>
		<pubDate>Fri, 10 May 2013 05:47:01 +0000</pubDate>
		<dc:creator><![CDATA[Golgotha]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/cs-blog/?p=109</guid>
		<description><![CDATA[Bienvenu dans ce troisième article consacré à Django. Nous allons maintenant voir comment Django intègre les pages HTML. Pour cela nous allons modifier le fichier settings.py pour lui donner le répertoire ou serons placés les fichiers HTML, ce que Django &#8230; <a href="https://blog.developpez.com/cs-blog/p11961/framework/django-introduction-aux-templates">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h2>Bienvenu dans ce troisième article consacré à <strong>Django</strong>.</h2>
<p>Nous allons maintenant voir comment <strong>Django</strong> intègre les pages HTML.</p>
<p style="text-align: justify">
Pour cela nous allons modifier le fichier <strong>settings.py</strong> pour lui donner le répertoire ou serons placés les fichiers HTML, ce que <strong>Django</strong> nomme les templates. Commencer par ajouter le répertoire templates dans <strong>mysite/</strong> puis modifier le fichier <strong>settings.py</strong> :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TEMPLATE_DIRS <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;C:/mysite/templates&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#41;</span></div></div>
<p><em>Le chemin vers le dossier templates doit être absolut.</em></p>
<p><span id="more-109"></span></p>
<p>Dans le nouveau répertoire templates, créer votre premier template : <strong>home.html</strong></p>
<div class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hello Template !</div></div>
<p>Ensuite, il faut indiquer dans notre fichier <strong>view.py</strong>, que nous allons passer par un template :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">shortcuts</span> <span style="color: #ff7700;font-weight:bold;">import</span> render_to_response <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> home<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'home.html'</span><span style="color: black;">&#41;</span></div></div>
<p style="text-align: justify">
Notez le changement ici on utilise <strong>render_to_response</strong>, et non plus <strong>HttpResponse</strong>, puisque nous passons maintenant par un template (home.html) alors qu&rsquo;avec <strong>HttpResponse</strong>, c&rsquo;est directement du HTTP qui était renvoyé. <strong>render_to_response</strong> nous permet d&rsquo;injecter un certain nombre d&rsquo;objet dans une page HTML pour être ensuite rendue au client dans ça forme définitive.</p>
<p>Relancer votre serveur puis recharger la page web :</p>
<p><img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-3.png" alt="django" /></p>
<h2>Template et héritage</h2>
<p style="text-align: justify">
Maintenant que vous savez utiliser un template, regardons ce que <strong>Django</strong> nous propose pour nous faciliter la vie. Il y a bien longtemps, dans une galaxie lointaine, très lointaine.. Nous utilisions <a href="http://cyberzoide.developpez.com/html/frame.php3" title="frames et framset">les frames et frameset</a> pour ne pas avoir de redondance de code entre les pages web, puisque souvent le menu, le header et le footer sont les mêmes sur toutes les pages. <strong>Django</strong> nous propose un système d&rsquo;héritage au niveau des pages web, voyons ça de plus près ensemble.</p>
<p>Créez une page web dans le dossier de template, que nous appellerons <strong>base.html</strong> :</p>
<div class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>mon site web<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Mon premier site web<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% block content %} {% endblock %}</div></div>
<p>Ici nous voyons pour la première fois le langage de template propre à <strong>Django</strong>.<br />
<strong>{% block content %}{% endblock %}</strong> est une déclaration de block, qui nous permettra de venir insérer à cette endroit du contenu.</p>
<p>Il faut maintenant modifier notre page <strong>home.html</strong> pour se servir de ce fichier :</p>
<div class="codecolorer-container html4strict railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% extends &quot;base.html&quot; %} <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% block content %} <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Première page du site <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endblock %}</div></div>
<p>Pour dire à <strong>Django</strong> que cette page va hériter de la page <strong>base.html</strong>, nous lui indiquons : <strong>{% extends &laquo;&nbsp;base.html&nbsp;&raquo; %}</strong></p>
<p style="text-align: justify">
Ensuite, il suffit de reprendre le ou les blocks déclaré dans la page mère, ici <strong>{% block content %}</strong>. Quand <strong>Django</strong> va rendre cette page, il va aller chercher <strong>base.html</strong>, puis y inclure les différents block. L’exemple n&rsquo;est pas très parlant mais il faut imaginer que le fichier <strong>base.html</strong> est le squelette complet de notre site, cela permet de ne pas dupliquer le code, et aussi d&rsquo;avoir un seule endroit à modifier pour changer l&rsquo;aspect générale du site web.</p>
<p>Le résultat de notre page web :</p>
<p><img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-4.png" alt="site web django" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django &#8211; Premiers pas</title>
		<link>https://blog.developpez.com/cs-blog/p11960/framework/django-premier-pas</link>
		<comments>https://blog.developpez.com/cs-blog/p11960/framework/django-premier-pas#comments</comments>
		<pubDate>Thu, 09 May 2013 08:55:18 +0000</pubDate>
		<dc:creator><![CDATA[Golgotha]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/cs-blog/?p=91</guid>
		<description><![CDATA[Deuxième article sur Django, Hello world ! Nous allons commencer par écrire un premier projet qui affichera &#171;&#160;Hello world&#160;&#187; pour nous assurer que Django est prêt. Commencer par ouvrir une invite de commande, puis taper la commande suivante : django-admin.py &#8230; <a href="https://blog.developpez.com/cs-blog/p11960/framework/django-premier-pas">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h1>Deuxième article sur Django, Hello world !</h1>
<p style="text-align: justify">
Nous allons commencer par écrire un premier projet qui affichera &laquo;&nbsp;Hello world&nbsp;&raquo; pour nous assurer que Django est prêt. Commencer par ouvrir une invite de commande, puis taper la commande suivante :</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">django-admin.py startproject mysite</div></div>
<p>placer vous dans le répertoire nouvellement crée : <strong>mysite</strong>.<br />
Vous devriez retrouver les éléments suivant :</p>
<ul>
<li>manage.py
<li>settings.py
<li>urls.py
<li>__init__.py
</ul>
<p><span id="more-91"></span></p>
<ul>
<li>Le dossier /<strong>mysite</strong> contient tout les éléments de votre projet.
<li><strong>manage.py</strong> est l&rsquo;utilitaire vous permetra d&rsquo;interagire avec Django.
<li><strong>settings.py</strong> est le fichier de configuration de votre projet.
<li><strong>urls.py</strong> est la table des url de votre projet.</ul>
<p style="text-align: justify">
A ce stade, nous avons créé un projet Django, maintenant nous allons créer une application. Il est possible de décomposer sont site web en plusieurs applications, ceci permet de faire des choses réutilisables par la suite dans d&rsquo;autre projet.</p>
<p>Taper la commande suivante :</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python manage.py startapp myapp</div></div>
<p>Django va créer pour nous le squelette de l&rsquo;application dans le répertoire /myapp.</p>
<ul>
<li>models.py
<li>tests.py
<li>views.py
<li>__init__.py
</ul>
<p>Le fichier <strong>models.py</strong> contiendra le modèle de donnée de notra application<br />
le fichier <strong>views.py</strong> est un fichier particulier qui servira à écrire notre code métier.</p>
<p>Nous allons maintenant démarrer le serveur d&rsquo;application fournis avec Django, taper la commande :</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python manage.py runserver</div></div>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Validating models...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 errors found<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Django version 1.3.1, using settings 'mysite.settings'<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Development server is running at http://127.0.0.1:8000/<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Quit the server with CTRL-BREAK.</div></div>
<p>Vous pouvez ouvrir un navigateur et aller à l&rsquo;url indiqué : http://127.0.0.1:8000/</p>
<p><img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-1.png" alt="premier lancement de Django" /></p>
<p>Maintenant que tout est en place, nous allons faire notre première page.<br />
Commencer par editer le fichier <strong>mysite/urls.py</strong> :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">urlpatterns <span style="color: #66cc66;">=</span> patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^$'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'mysite.myapp.views.home'</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'home'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#41;</span></div></div>
<p>Ici nous voulons que l&rsquo;index du site [http://127.0.0.1:8000/] appèle l&rsquo;action <strong>home</strong> du fichier <strong>view.py</strong><br />
Éditez ensuite le fichier <strong>mysite/myapp/views.py</strong> pour ajouter cette action :</p>
<div class="codecolorer-container python railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponse<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> home<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, world.&quot;</span><span style="color: black;">&#41;</span></div></div>
<p>Maintenant, relancer la page web :</p>
<p><img src="http://salaun-cedric.developpez.com/tutoriels/web/introduction-django/images/Django-2.png" alt="Django" /></p>
<p>Si vous avez cette page, vous pouvez passer à la suite <img src="https://blog.developpez.com/cs-blog/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django &#8211; installation</title>
		<link>https://blog.developpez.com/cs-blog/p11958/framework/django-installation</link>
		<comments>https://blog.developpez.com/cs-blog/p11958/framework/django-installation#comments</comments>
		<pubDate>Wed, 08 May 2013 19:26:59 +0000</pubDate>
		<dc:creator><![CDATA[Golgotha]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/cs-blog/?p=81</guid>
		<description><![CDATA[Je débute une petite série d&#8217;article traitant de Django que j’espère vous sera utile. Partie I &#8211; l&#8217;installation Sous windows Au préalable, il faut installer python 2.7 que vous trouverez ici. Vous aurez besoin de modifier votre variable PATH, en &#8230; <a href="https://blog.developpez.com/cs-blog/p11958/framework/django-installation">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je débute une petite série d&rsquo;article traitant de <strong>Django</strong> que j’espère vous sera utile.</p>
<p style="text-align: center">
<img align="center" src="https://djangoproject.com/s/img/logos/django-logo-negative.png" alt="django logo" width="50%">
</p>
<h1>Partie I &#8211; l&rsquo;installation</h1>
<h2>Sous windows</h2>
<p>Au préalable, il faut installer <strong>python 2.7</strong> que <a href="http://www.python.org" title="ici" target="_blank">vous trouverez ici</a>.</p>
<p>Vous aurez besoin de modifier votre <strong>variable PATH</strong>, en y ajoutant : </p>
<blockquote><p>C:\Python27\;C:\Python27\Scripts; </p></blockquote>
<p>(si vous avez installé python dans C:\Python27)</p>
<p><span id="more-81"></span></p>
<p>Pour modifier la variable PATH de windowss :</p>
<ul>
<li>Clique droits sur &laquo;&nbsp;Ordinateur&nbsp;&raquo; (sur le bureau ou dans le menu directement)
<li>Dans le menu cliquez sur &laquo;&nbsp;Proprietés&nbsp;&raquo;
<li>Allez dans &laquo;&nbsp;paramètre système avancé&nbsp;&raquo;
<li>Cliquez sur le bouton &laquo;&nbsp;varaible d&rsquo;environnement&nbsp;&raquo;
<li>Dans la liste variable système, cliquez sur la variable &laquo;&nbsp;path&nbsp;&raquo;
<li>Cliquez sur modifier
<li>Ajouter le chemin que vous voulez à la suite de la liste
<li>Fermer toutes les fenètre avec le bouton &laquo;&nbsp;ok&nbsp;&raquo;
</ul>
<p style="text-align: justify">
Une fois que python est bien installé allez sur la page de téléchargement de Django puis télécharger Django ( à l&rsquo;heure ou j&rsquo;écris, le fichier se nomme Django-1.4.1.tar.gz ). Pour décompresser ce fichier je vous conseil d&rsquo;utiliser 7zip, En effet le format tar.gz est propre à unix, 7zip est tout à fait capable de décompresser correctement ce genre d&rsquo;archive. La premier décompression vous donnera un fichier .tar, refaite une décompression de ce fichier pour avoir le dossier décompressé de Django.
</p>
<p style="text-align: justify">
Si vous vous retrouvez comme moi avec un chemin long vers le dossier de Django (Django-1.4.1.tar\dist\Django-1.4.1\Django-1.4.1) n&rsquo;hésiter pas à copier le dernier dossier et à le placer dans C:\ directement. Normalement vous devriez vous retrouver avec C:\Django-1.4.1\django.
</p>
<p>Lancer la commande suivante :</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python setup.py install</div></div>
<p>Il ne reste plus qu&rsquo;a modifier la variable PATH, comme vous l&rsquo;avez fait pour python en ajoutant : C:\Django-1.4.1\django\bin.</p>
<p>Pour vérifier que Django est opérationel, taper la commande :</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">django-admin.py --version<br />
1.4.1</div></div>
<p>Il faut relancer la console à chaque fois que vous modifier la variable PATH</p>
<h2>Sous Ubuntu</h2>
<p>Sur ubuntu l&rsquo;installation se fait en une ligne :</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo apt-get install python-django</div></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
