Django 3 : Creating objects

Louis SanchezAugust 14th 2021, 4:02

Step 1 We can create the records in the UserProfile model as follows:

from myproject.models import UserProfile
profile = UserProfile(name='New Profile', login='newprofile')
profile.save()

Step 2 : The code template

{% extends "base.html" %}
{% block title %}
  {{ action }}
{% endblock %}
{% block h1 %}
  {{ action }}
{% endblock %}
{% block content %}
  {% if all_user_profile|length > 0 %}
  <table>
    <thead>
      <tr>
        <td>ID</td>
        <td>Name</td>
      </tr>
    </thead>
    <tbody>
    {% for profile in all_user_profile %}
      <tr>
        <td>{{ profile.id }}</td>
        <td>{{ profile.name }}</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
  {% else %}
  <span>No data.</span>
  {% endif %}
{% endblock %}

Step 3 : Now start the development server with the python manage.py runserver command and open http://127.0.0.1:8000/page/ in your browser.