【django】社内SNSアプリを作ってみる⑦(DetailViewやいいねの実装)【pythonメモ】

pythonLOGO

templatesフォルダにdetail.htmlを作成する

{% extends 'base.html' %}

{% block customecss %}
{% endblock customecss %}

{% block header %}
<div class="alert alert-primary" role="alert">
個別投稿
</div>
{% endblock header %}

{% block content %}
<div class='container'>

  <div class="alert alert-success" role="alert">
    <p>タイトル:{{object.title}}</p>
    <p>投稿者:{{object.auther}}</p>
    <a href="#" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">Primary link</a>
    <a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">Link</a>
  </div>


</div>
{% endblock content %}
from django.urls import path
from .views import signupfunc, loginfunc, listfunc, logoutfunc, detailfunc

urlpatterns = [
    path('signup/', signupfunc, name='signup'),
    path('login/', loginfunc, name='login'),
    path('list/', listfunc, name='list'),
    path('logout/', logoutfunc, name='logout'),
    path('detail/<int:pk>', detailfunc, name="detail")
]
from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect
from .models import BoardModel
from django.contrib.auth.decorators import login_required

# Create your views here.

def signupfunc(request):
  if request.method =='POST':
    username2 = request.POST['username']
    password2 = request.POST['password']
    try:
      User.objects.get(username=username2)
      return render(request, 'signup.html', {'error':'このユーザーは登録されています'})
    except:
      user = User.objects.create_user(username, '', password2)
      return render(request, 'signup.html', {'some':100})
  return render(request, 'signup.html', {'some':100})

def loginfunc(request):
  if request.method =='POST':
    username2 = request.POST['username']
    password2 = request.POST['password']
    user = authenticate(request, username=username2, password=password2)
    if user is not None:
      login(request, user)
      return redirect('list')
    else:
      return redirect('login')
  return render(request, 'login.html')

@login_required
def listfunc(request):
  object_list = BoardModel.objects.all()
  return render(request, 'list.html', {'object_list':object_list})

def logoutfunc(request):
    logout(request)
    return redirect('login')

def detailfunc(request, pk):
  object = BoardModel.objects.get(pk=pk)
  return render(request, 'detail.html', {'object':object})
{% extends 'base.html' %}

{% block customecss %}
{% endblock customecss %}

{% block header %}
<div class="alert alert-primary" role="alert">
  社内SNS
</div>
{% endblock header %}

{% block content %}
<div class='container'>
  {% for item in object_list %}
  <div class="alert alert-success" role="alert">
    <p>タイトル:<a href="{% url 'detail' item.pk %}">{{item.title}}</a></p>
    <p>投稿者:{{item.auther}}</p>
    <a href="#" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">Primary link</a>
    <a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">Link</a>
  </div>
  {% endfor %}
  <a href="{% url 'logout' %}">ログアウト</a>
</div>
{% endblock content %}

detail.htmlを作り込む

{% extends 'base.html' %}

{% block customecss %}
{% endblock customecss %}

{% block header %}
<div class="alert alert-primary" role="alert">
個別投稿
</div>
{% endblock header %}

{% block content %}
<div class='container'>

  <div class="alert alert-success" role="alert">
    <p>タイトル:{{object.title}}</p>
    <p>投稿者:{{object.auther}}</p>
    <p>内容:{{object.content}}</p>
    <p><img src="{{object.images.url}}" width=300></p>

    <a href="{% url 'good' object.pk %}" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">いいねする</a>
    <a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">既読にする</a>
  </div>


</div>
{% endblock content %}
http://127.0.0.1:8000/detail/3

ここまでなんとか出来ました。

「いいね」の実装

from django.urls import path
from .views import signupfunc, loginfunc, listfunc, logoutfunc, detailfunc, goodfunc

urlpatterns = [
    path('signup/', signupfunc, name='signup'),
    path('login/', loginfunc, name='login'),
    path('list/', listfunc, name='list'),
    path('logout/', logoutfunc, name='logout'),
    path('detail/<int:pk>', detailfunc, name="detail"),
    path('good/<int:pk>', goodfunc, name='good'),
]
from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect
from .models import BoardModel
from django.contrib.auth.decorators import login_required

# Create your views here.

def signupfunc(request):
  if request.method =='POST':
    username2 = request.POST['username']
    password2 = request.POST['password']
    try:
      User.objects.get(username=username2)
      return render(request, 'signup.html', {'error':'このユーザーは登録されています'})
    except:
      user = User.objects.create_user(username, '', password2)
      return render(request, 'signup.html', {'some':100})
  return render(request, 'signup.html', {'some':100})

def loginfunc(request):
  if request.method =='POST':
    username2 = request.POST['username']
    password2 = request.POST['password']
    user = authenticate(request, username=username2, password=password2)
    if user is not None:
      login(request, user)
      return redirect('list')
    else:
      return redirect('login')
  return render(request, 'login.html')

@login_required
def listfunc(request):
  object_list = BoardModel.objects.all()
  return render(request, 'list.html', {'object_list':object_list})

def logoutfunc(request):
    logout(request)
    return redirect('login')

def detailfunc(request, pk):
  object = BoardModel.objects.get(pk=pk)
  return render(request, 'detail.html', {'object':object})

def goodfunc(request, pk):
  post = BoardModel.objects.get(pk=pk)
  post.good = post.good + 1
  post.save()
  return redirect('list')
{% extends 'base.html' %}

{% block customecss %}
{% endblock customecss %}

{% block header %}
<div class="alert alert-primary" role="alert">
  社内SNS
</div>
{% endblock header %}

{% block content %}
<div class='container'>
  {% for item in object_list %}
  <div class="alert alert-success" role="alert">
    <p>タイトル:<a href="{% url 'detail' item.pk %}">{{item.title}}</a></p>
    <p>投稿者:{{item.auther}}</p>
    <a href="#" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">いいね:{{item.good}}</a>
    <a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">Link</a>
  </div>
  {% endfor %}
  <a href="{% url 'logout' %}">ログアウト</a>
</div>
{% endblock content %}

ここで「いいねする」を押すと

いいねの数が増えました。



 

あ、宜しければ・・・。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です