'Can't call / pass data from django database onto website using built in auth model and a model I created, which is the one I cant query
Any help would be appreciated, basically I am a beginner and I'm struggling to pull data from my database. I used the built in auth method from django, which seemingly works ok as I can pull user data. However as soon as I want to use my Member class I made in models nothing seems to work. Can't output all its data. When looking at the admin page I see authentication and authorisation table and my Members table created in models.py.
When I try pass into the context however nothing is displayed.
My index.html
{% block content %}
{% if user.is_authenticated %}
Hi {{ user.username }}!
Hi {{ user.email }}!
<p><a href="{% url 'logout' %}">Log Out</a></p>
{% else %}
<p>You are not logged in</p>
Hi {{ all }}!
<a href="{% url 'login' %}">Log In</a>
{% endif %}
{% endblock %}
{% block title %}Home{% endblock %}
My Views.py
from django.shortcuts import render
from .models import Member
# Create your views here.
def index(request):
all_members = Member.objects.all
return render(request, 'index.html', context = {'all':all_members})
def ethos(request):
return render(request, 'ethos.html', {})
Models.py
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
# Create your models here.
class Member(models.Model):
phoneNumber = PhoneNumberField()
firstName = models.CharField(max_length=50)
lastName = models.CharField(max_length=100)
jobDesc = models.CharField(max_length=200)
yearsExperience = models.IntegerField()
def __str__(self):
return self.firstName + ' ' + self.lastName
I can display the username from the auth on index. But anything to do with the Member class as will be displayed when user is logged out is not shown.
I am tempted to start from scratch but time is an element and implementation this far has already proved difficult in some scenarios.
Solution 1:[1]
you need a few small changes, first for the user object you need to take it from request, {{request.user}} and a small bug when calling the data from the database:
def index(request):
all_members = Member.objects.all() # here the () !
return render(request, 'index.html', context = {'all':all_members})
in the template:
{% block content %}
{% if request.user.is_authenticated %}
Hi {{ request.user.username }}!
Hi {{ request.user.email }}!
<p><a href="{% url 'logout' %}">Log Out</a></p>
{% else %}
<p>You are not logged in</p>
Hi! {% for person in all %}<p>{{person}}</p>{% endfor %}
<a href="{% url 'login' %}">Log In</a>
{% endif %}
{% endblock %}
{% block title %}Home{% endblock %}
Solution 2:[2]
The problem lied within the URLs, through following a tutorial I had set the pattern to render template_as_view rather than view.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Johnny Herrera |
Solution 2 | basim musa |