Add part 4c, views using templates

parent ad233b72
...@@ -98,3 +98,6 @@ Edit `redurl/urls.py`. ...@@ -98,3 +98,6 @@ Edit `redurl/urls.py`.
Make views that do something, just reading the objects' data. Make views that do something, just reading the objects' data.
Make views using templates.
Edit `urlapp/views.py`.
Create `urlapp/templates/index.html` and `urlapp/templates/details.html`.
<h1>Details of URL {{ url.id }}</h1>
<ul>
<li>Owner: {{ url.user.first_name }} {{ url.user.last_name }}
({{ url.user.username }})</li>
<li>Target: <a href="{{ url.target }}" target="_blank"><code>{{ url.target }}</code></a></li>
<li>Followed: {{ url.used }} time(s).</li>
</ul>
<h1>List of URLs</h1>
{% if url_list %}
<ul>
{% for u in url_list %}
<li>
<a href="/url={{ u.id }}/">Show details</a>, or follow
<a href="/{{ u.id }}/" target="_blank"><code>{{ u.target }}</code></a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No URLs are available.</p>
{% endif %}
...@@ -6,13 +6,16 @@ from .models import Url ...@@ -6,13 +6,16 @@ from .models import Url
# Create your views here. # Create your views here.
def index(request): def index(request):
L = Url.objects.all() context = {
output = ', '.join([u.target for u in L]) 'url_list': Url.objects.all()
return HttpResponse(output) }
return render(request, 'index.html', context)
def redir(request, url_id): def redir(request, url_id):
return HttpResponse("You're following URL {}.".format(url_id)) return HttpResponse("You're following URL {}.".format(url_id))
def url(request, url_id): def url(request, url_id):
u = Url.objects.get(id=url_id) context = {
return HttpResponse("URL {} goes to {}.".format(url_id, u.target)) 'url': Url.objects.get(id=url_id)
}
return render(request, 'details.html', context)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment