LoginSignup
0
0

More than 5 years have passed since last update.

Django Webアプリ作成(9) リンクの表示有無

Last updated at Posted at 2019-03-01

環境
OS : macOS Mojave
Anaconda : python3.6.7
Django==2.1.5

ここまで

前回まででアプリは、ほぼほぼ完成しています。
今回は、管理者ユーザーのみに編集リンクを表示するようにします。

htmlの編集

base.htmlの

base.html
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>

部分を以下のように編集

base.html
{% if user.is_authenticated %}
    <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
{% endif %}

また、post_detail.htmlの

post_detail.html
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>

部分を以下のように編集

post_detail.html
{% if user.is_authenticated %}
     <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
{% endif %}

これで一度実行してみましょう!
もしまだ、アイコンが見えている場合は一度管理者ページ(/admin)に行きログアウトしてみましょう。
ログイン状態でないと投稿・編集ボタンが見えないと思います。

終わりに

以上で長く書いてきた基本的なDjangoの使い方は終わりです。
お疲れ様でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0