Django profile model - django

I am beginner on django and trying to create a profile model that interrelate "User" built-in model via One-To-One relation field, it results impossible to span the user model properties by profile model instance and vise versa.
Can someone help me.
models.py
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
users=models.OneToOneField(User, on_delete=models.CASCADE)
image=models.ImageField(default='profilepic.jpg',upload_to='profile_pictures')
location=models.CharField(max_length=100)
def __str__(self):
return self.users.username
profileTemplate.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
</head>
<body>
<h2>{{user.username}}</h2>
<img src='{{user.Profile.image}}'>
</body>
</html>

use a lowercase profile as per the docs
<img src='{{user.profile.image}}'>

Related

Google one tap Login With Django ( The given origin is not allowed for the given client ID issue )

I'm trying to connect Google One Tap Login with Django framework, but I'm having issue.
I'm not sure why this is displaying an issue. I followed all of the guidelines described in the Google documentation.
issue -
The given origin is not allowed for the given client ID
Here is my Google API client configuration for the javascript origin url.
Here is the login html template code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hi Reddit</h1>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<div id="g_id_onload" data-client_id="i don't want to shere"
data-context="signin" data-ux_mode="popup" data-login_uri="http://localhost:8000/google/login/"
data-auto_select="true" data-itp_support="true">
</div>
<div class="g_id_signin" data-type="standard" data-shape="pill" data-theme="outline" data-text="continue_with"
data-size="large" data-logo_alignment="left">
</div>
</body>
</html>
I'm not sure why this is displaying an issue.
Is it possible to load Google One Tap Javascript in Django?
What should I do to load Google One Tap Javascript in Django?

Is it possible to put tooltips on django list view that could appear onClick or mouse hover

I'm pretty sure that could be possible by customising the django admin site for that specific feature.
I'm trying to add this functionality using admin.py but no better luck since a week.
In the picture you can see I put a circle where I will like to add and icon that will show a tooltip saying information about that specific field or column.
List View Image
So is there any way to do it easily without customising it from templates. Because the List view is so much complex and we do not want to complicate the things doing it hard way.
I tried to find it online and in the django official docs but every time its about customising it from templates, also I can html from admin.py but it doesn't invokes the tooltip as I wanted.
The short answer is: No, django can not give you a popover inside of your ListView. Django takes care about the backend. Its purpose is to serve the content of the tooltip from database to your html template. The displaying part is the job of the frontend. Therefore you have to design your tooltip yourself using html, css and javascript.
A useful framework is bootstrap as it takes care about the javascript and css. I will give you an example.
models.py
class MyModel(models.Model):
info_field = models.CharField(max_length=50)
views.py
def my_list_view(request):
my_objects = MyModel.objects.all()
return render(request, 'my_app/list-view.html', context={'my_objects': my_objects})
list-view.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>
{% for object in my_objects %}
<a tabindex="0" class="btn" role="button" data-bs-toggle="popover"
data-bs-trigger="focus" data-bs-content="{{ object.info_field }}" data-bs-html="true">
</a>
{% endfor %}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
crossorigin="anonymous"></script>
<script>
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]');
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl));
</script>
</body>

unable to render the context value in django template on ubuntu server

I have this piece of code that is working fine on my PC environment (virtual environment) but causing problems in a ubuntu, Nginx server. It is a digital ocean droplet actually. I am not very much familiar with ubuntu that's why I am unable to understand the cause of the problem.
Now the problem is, that the 'checkout.html' template doesn't render the value of 'fire'. I have tried to troubleshoot manually and in the 'inspect' window I only see an empty label.
Why is this happening, is it because of the django=2.2.3 version or ubuntu or Nginx or am I missing anything here?
Note: I am updating the files through Filezilla.
Kindly help, please.
Views.py
def checkout(request):
return render(request, 'checkout.html', {'fire':"fire"})
checkout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<label>{{fire}}</label>
</body>
</html>
urls.py
from .views import *
path('checkout-page', checkout, name="checkout"),

cannot access user data from social-auth-app-django

I am overwhelmed by the authentication of Django and trying to wrap my head around it for a while.
i have used social-auth-app-Django to use OAuth for authorization from GitHub and Facebook.
GitHub worked and Facebook didn't worked, after log-in using GitHub I checked the admin page,
in the user social auths part i wasn't able to access the provider name using the template syntax in the html Page didn't showed up!
can someone explain what really going on here, and what is associations and nonces too?
this is my html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{%if user.is_authenticated%}
<h1>{{user.provider}}</h1>
<h1>{{user.username}}</h1>
{%else%}
<h1>you are not logged in</h1>
{%endif%}
</body>
</html>
You can access provider by doing this :
in views.py
def get_provider(request):
user= request.user
user.social_auth.get(provider='provider name')
provs= ['github','twitter','facebook','google-oauth2'] # Any backend in the social auth back end
found=list()
notfound=list()
for prov in provs :
try:
user.social_auth.get(provider=prov)
print("Found ",prov)
found.append(prov)
except:
print(prov,"Not found ")
notfound.append(prov)
return render(request, 'registration/social_login.html', {
'found':found,
'not_found':notfound,
})
Then in your template
social_login.html
{% for element in found %}
<p>you are auth as {{element}}</p>
{% endfor %}
{% for el in not_found %}
<p>you are not auth in {{el}}</p>
{% endfor %}
You can found more here https://simpleisbetterthancomplex.com/tutorial/2016/10/24/how-to-add-social-login-to-django.html

TemplateDoesNotExist at /test/ error popped up while running application

I am using Django 3.0.8 My view function is as follows:
from django.shortcuts import render import datetime
# Create your views here.
def date_time_view(request):
date=datetime.datetime.now()
h=int(date.strftime('%H'))
if h<12:
msg='Hello Guest!!! Very good Morning!!!'
elif h<16:
msg='Hello Guest!!! Very good Afternoon !!!'
elif h<21:
msg='Hello Guest!!! Very good Evening!!!'
else:
msg='HELLO GUEST!! very good Night!!!'
my_dict = {'date':date,'msg':msg}
return render(request, 'testapp/results.html', my_dict)
and my template is as follows:
<!DOCTYPE html> {%load staticfiles%}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>hello</title>
</head>
<body>
<h1>{{msg}}</h1>
<h2>{{date}}</h2>
<img src="{%static " images/images.jpg" %}">
</body>
</html>
Check your configuration in your project settings file.
Templates can reside in each app separately, but also in a templates folder in APPS_DIR (folder with all Django apps).
Here is official docs for Django 3 templates:
https://docs.djangoproject.com/en/3.0/topics/templates/
Check your template's location. It should be yourproject/yourapp/templates/testapp.