スナックelve 本店

バツイチ40代女の日記です

もう何やってるかわからなくなったんスよね・・・

f:id:elve:20210722191503p:plain
snack.elve.club
snack.elve.club
続けます。

とにかくまぁ、ユーザーフォームが必要そうじゃん? って調べたらDjangoとかいうのにたどり着く。
(ノ*・ω・)ノ ワケヽ(・ω・*ヽ)ワカ ヽ(*・ω・)ノラン♪

結果



手順

ここから順番に作業です・・・その3で挫折したwwwww
はじめての Django アプリ作成、その 1 | Django ドキュメント | Django

モデル

from django.db import models

# Create your models here.
class USERS(models.Model):
    id = models.CharField(primary_key=True,max_length=40)
    _created_at = models.DateTimeField(null=True)
    _updated_at = models.DateTimeField(null=True)
    _user_id = models.CharField(max_length=40)
    description = models.CharField(max_length=300)
    name = models.CharField(max_length=30)


class TWEETS(models.Model):
    id = models.CharField(primary_key=True,max_length=40)
    _created_at = models.DateTimeField(null=True)
    _updated_at = models.DateTimeField(null=True)
    _user_id= models.CharField(max_length=40)
    in_reply_to_text_id= models.CharField(max_length=40)
    in_reply_to_user_id= models.CharField(max_length=40)
    text= models.CharField(max_length=200)

ビュー

from django.shortcuts import render
import requests
from polls.models import USERS


def index(request):
    url = "https://versatileapi.herokuapp.com/api/user/all"
    users = requests.get(url).json()
    USERS.objects.all().delete()
    for user in users:
        USERS.objects.create(
            id = user['id'],
            _created_at = user['_created_at'],
            _updated_at = user['_updated_at'],
            _user_id = user['_user_id'],
            description = user['description'],
            name = user['name'])
    output = {'test': USERS.objects.all()}
    return render(request, 'polls/index.html',output)

テンプレート

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta charset="UTF-8">
  <title>index.html</title>
  <style>
  body {
    background-color: #B2EBF2;
    font-family: sans-serif;
  }
  </style>
</head>
<body>
  <h1>test</h1>
<table>
  {% for user in test %}
  <tr>
    <td colspan="2">Id: {{user.id}}</td>
  </tr><tr>
    <td>Name: {{user.name}}</td><td>Description: {{user.description}}</td>
  </tr>
  {% endfor %}
</table>
</body>
</html>