|
|
|
@ -48,16 +48,6 @@
|
|
|
|
|
<h1 class='semi-opaque'>Entendendo Django</h1> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<h2>Agenda</h2> |
|
|
|
|
|
|
|
|
|
<ul> |
|
|
|
|
<li>Entendendo Projeto</li> |
|
|
|
|
<li>Entendendo Apps</li> |
|
|
|
|
<li>Adicionando Apps no Projeto</li> |
|
|
|
|
</ul> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<section> |
|
|
|
|
<h2>Projeto</h2> |
|
|
|
@ -374,7 +364,9 @@ fruit = Product.objects.get(name='fruit')
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
all_fruits = Order.objects.filter(products__name__like='fruit') |
|
|
|
|
all_fruits_in_orders = Order.objects \ |
|
|
|
|
.filter(products__name__like='fruit') \ |
|
|
|
|
.distinct() |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
@ -423,7 +415,7 @@ all_expensive_fruits = Order.objects.filter(
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<h2>Queryes com OR</h2> |
|
|
|
|
<h2>Queries com OR</h2> |
|
|
|
|
|
|
|
|
|
<p>Para queries com OR, deve-ser usar o objeto <code>Q</code>.</p> |
|
|
|
|
|
|
|
|
@ -638,6 +630,75 @@ def free(value):
|
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<section> |
|
|
|
|
<h2>URLs/Redirects</h2> |
|
|
|
|
|
|
|
|
|
<p>Redirects servem para indicar que o browser deve se |
|
|
|
|
direcionar à outra URL.</p> |
|
|
|
|
|
|
|
|
|
<p>Para referenciar URLs de outras apps (ou mesmo da |
|
|
|
|
mesma app), deve-ser usar o método |
|
|
|
|
<code>reverse</code>.</p> |
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
from django.core.urlresolvers import reverse |
|
|
|
|
from django.shortcuts import redirect |
|
|
|
|
|
|
|
|
|
def view(request): |
|
|
|
|
# do stuff |
|
|
|
|
return redirect(reverse('app:func')) |
|
|
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<section> |
|
|
|
|
<h2>Testes</h2> |
|
|
|
|
|
|
|
|
|
<p>Bem parecido com o módulo <code>unittest</code> do Python |
|
|
|
|
padrão.</p> |
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
import random |
|
|
|
|
|
|
|
|
|
from django.test import TestCase |
|
|
|
|
|
|
|
|
|
class RandomTest(TestCase): |
|
|
|
|
def test_random(self): |
|
|
|
|
"""Check if random numbers return random numbers.""" |
|
|
|
|
self.assertTrue(random.randint(255) != 0) |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<p>Testes podem acessar qualquer coisa (desde que importados):</p> |
|
|
|
|
|
|
|
|
|
<ul> |
|
|
|
|
<li>Modelos;</li> |
|
|
|
|
<li>Views;</li> |
|
|
|
|
<li>Requisições inteiras.</li> |
|
|
|
|
</ul> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
from django.test import Client |
|
|
|
|
from django.test import TestCase |
|
|
|
|
|
|
|
|
|
class RequestTest(TestCase): |
|
|
|
|
def test_retrieve_page(self): |
|
|
|
|
client = Client() |
|
|
|
|
response = client.get('/') |
|
|
|
|
self.assertTrue('success!' in response.content) |
|
|
|
|
self.assertEquals(response.status_code, 200) |
|
|
|
|
self.assertTrue('variable' in response.context) |
|
|
|
|
self.assertEqual(response.context['variable'], 'success') |
|
|
|
|
</code></pre> |
|
|
|
|
</section> |
|
|
|
|
</section> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|