Skip to content

Instantly share code, notes, and snippets.

@JuniorLima
Last active August 29, 2015 14:08
Show Gist options
  • Save JuniorLima/6d0ee417bc642349dec2 to your computer and use it in GitHub Desktop.
Save JuniorLima/6d0ee417bc642349dec2 to your computer and use it in GitHub Desktop.
Buscas no Django
{% load staticfiles %}
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<img src="{% static "images/sitelogo.png" %}" alt="Logo" />
{% block content %}{% endblock %}
</body>
</html>
from django.db import models
class Reporter(models.Model):
full_name = models.CharField(max_length=70)
def __str__(self): # __unicode__ on Python 2
return self.full_name
class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter)
def __str__(self): # __unicode__ on Python 2
return self.headline
# Buscar todos os reporteres
Reporter.objects.all()
# Buscar todos os artigos
Article.objects.all()
# Arquivos do ano
#view
def year_archive(request, year):
# Pega todos os arquivos do ano
a_list = Article.objects.filter(pub_date__year=year)
context = {'year': year, 'article_list': a_list}
return render(request, 'news/year_archive.html', context)
#url
(r'^articles/(\d{4})/$', views.year_archive),
# template
{% extends "base.html" %}
{% block title %}Articles for {{ year }}{% endblock %}
{% block content %}
<h1>Articles for {{ year }}</h1>
{% for article in article_list %}
<p>{{ article.headline }}</p>
<p>By {{ article.reporter.full_name }}</p>
<p>Published {{ article.pub_date|date:"F j, Y" }}</p>
{% endfor %}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment