Skip to content

Conversation

@jeffcjohnson
Copy link
Contributor

@jeffcjohnson jeffcjohnson commented Mar 16, 2017

Description

This change allows using the rest_framework/base.html as-is for all pages on a website and should have no impact on existing code.

Use case

I typically start new projects with cookiecutter-django.

It includes many pages for registration, login, change password, etc. All of their templates use a "content" block. In order to make all of those pages immediately compatible with DRF and styled the same, simply add these two files to a new project:

templates/base.html

{% extends "rest_framework/api.html" %}

{% block content %}{% endblock %}

templates/rest_framework/api.html

{% extends "rest_framework/base.html" %}
{% load staticfiles i18n %}

{% block title %}{% if name %}{{ name }} – {% endif %}My Project{% endblock %}

{% block branding %}
  <a class="navbar-brand" rel="nofollow" href="/">
    My Project
  </a>
{% endblock %}

{% block userlinks %}
  <li class="nav-item">
    <a class="nav-link" href="{% url 'api-root' %}">{% trans "API" %}</a>
  </li>
  {% if request.user.is_authenticated %}
    <li class="nav-item">
      <a class="nav-link" href="{% url 'users:detail' request.user.username  %}">{% trans "My Profile" %}</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="{% url 'account_logout' %}">{% trans "Sign Out" %}</a>
    </li>
  {% else %}
    <li class="nav-item">
      <a id="sign-up-link" class="nav-link" href="{% url 'account_signup' %}">{% trans "Sign Up" %}</a>
    </li>
    <li class="nav-item">
      <a id="log-in-link" class="nav-link" href="{% url 'account_login' %}">{% trans "Sign In" %}</a>
    </li>
  {% endif %}
{% endblock %}

Breadcrumbs

Also, the "breadcrumbs" div is kept outside the content block with a default blank space because its top margin of 70px is needed for the fixed header and also maintains the look and feel. Maybe that should instead be put inside the content block so it can be controlled by the application and a more generic solution could be found for the 70px margin?

@lovelydinosaur lovelydinosaur merged commit 73ad88e into encode:master Mar 17, 2017
@lovelydinosaur
Copy link
Contributor

Thanks for this! 😄

Maybe that should instead be put inside the content block so it can be controlled by the application and a more generic solution could be found for the 70px margin?

Welcome to consider PRs towards this, tho not something I'd personally prioritize otherwise.

@jeffcjohnson
Copy link
Contributor Author

Actually for the breadcrumbs, in future could just wrap another block around that which lets the application override if desired. If I get a chance later today I'll try to submit a PR for that.

@jeffcjohnson
Copy link
Contributor Author

Whoops, that breadcrumbs block is already there, so... problem solved :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants