Django: Minimum disqus comments integration example in development - python

I want to test disqus comments in development mode. I know that django-disqus is the package that helps integrate disqus comments but can't really figure out the way forward.
I have a Post model in models.py as follows.
class Post(models.Model):
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=300, unique_for_date='publish')
author = models.ForeignKey(settings.AUTH_USER_MODEL)
body = models.TextField()
publish = models.DateTimeField()
In my templates I have,
{% extends "blog/base.html" %}
{% block title %}
Blog | {{post.title}}
{% endblock %}
{% block content %}
<p> <b>{{post.title}}</b> </p>
<p> {{post.body}}</p>
<p>Written by: {{ post.author }} on {{ post.publish }}</p>
<hr>
{% load disqus_tags %}
{% disqus_dev %}
{% disqus_show_comments %}
{% endblock %}
{% block domready %}
{% endblock %}
So, my question is:
Is it possible to integrate Disqus in development on my local machine?
If yes, please provide reference/blog/hints so that I can integrate disqus comments in my blog.
EDIT: I have registered here and Since I don't have a website, I put www.example.com in website field. I have also set required values like DISQUS_API_KEY = 'a_very_long_string' and
DISQUS_WEBSITE_SHORTNAME = 'a_short_string' in my settings.py. I am getting following error: We were unable to load Disqus. If you are a moderator please see our troubleshooting guide

Related

Making a Django page visible only to confirmed emails

I'm trying to add a feature to my site where a certain part of the site can be used only by users who confirmed their email account.
I found a solution, but it's not working.
Here is what i did:
I have been suggested to add a separate model for this:
class account_emailconfirmation(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
#property
def has_verified_email(self):
return self.user.emailaddress_set.filter(verified=True,primary=True).exists()
And this is what the template looks like:
{% extends "main/header.html" %}
{% if user.account_emailconfirmation.has_verified_email %}
{% block content %}
<style>
</style>
<body>
<div> <p>Here goes a bunch of features</p> </div>
</body>
{% endblock %}
{% else %}
<p> Your email is ot confirmed </p>
{% endif %}
But it's not working, since i can see the page even without having to confirm my email.
Here is what my db looks like:
There is a table called account_emailconfirmation, then there is an index, verified, that will give 0 when the account is not verified, 1 when it is verified.
Any advice is appreciated!
Write this:
{% extends "main/header.html" %}
{% block content %}
{% if user.profile.has_verified_email %}
<style>
</style>
<body>
<div> <p>Here goes a bunch of features</p> </div>
</body>
{% else %}
<p> Your email is ot confirmed </p>
{% endif %}
{% endblock %}
You need to use the related_name attribute of the account_emailconfirmation.user one to one field
{% if user.account_emailconfirmation.has_verified_email %}
Side note, you should capitalize your classes:
class EmailConfirmation(models.Model):
I assume the account_ portion is for the app. You don't need that, you know which app the model is in.

Django - The page don't load after click

i'm new in Django developing.
I'm following the tutorial about Library on MDN (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django)
Until i follow the code all work but i'm trying implement author page by myself. Probably is very stupid issue but is one day that i'm turning around like a dog with its tail.
There is 2 page: author_list and author detail.
I set urls.py (in my project) i set view.py and crate my template.
I follow the same step of tutorial for realize book_list and book_detail but when i click on my author the page don't go to the detail of that author and stay in author_list.html.
Here the code urls.py :
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('author/<int:pk>', views.AuthorDetailView.as_view(), name='author-detail'),
Here views.py:
class AuthorListView(generic.ListView):
model = Author
class AuthorDetailView(generic.ListView):
model = Author
Here author_list.html with link get_absolute_url:
{% extends "base_generic.html"%}
{% block content %}
<h1>Author list</h1>
{% if author_list %}
<ul>
{% for aut in author_list %}
<li>{{ aut.first_name }} - {{ aut.last_name }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no author.</p>
{% endif %}
{% endblock %}
Here author_detail.html:
{% extends "base_generic.html" %}
{% block content %}
<h1>Author</h1>
{% if author %}
<p><strong>Nome: </strong> {{ author }}</p>
<p><strong>Nato il : </strong> {{ author.date_of_birth }}</p>
<p><strong>Morto il : </strong> {{ author.date_of_death }}</p>
{% endif %}
{% endblock %}
Here the screenshot
Author_list.html before click url=catalog/authors/
After click url change but page not
Thank to all for help
I believe you need DetailView instead of ListView for AuthorDetailView.
Looks like a typo to me, you want generic.DetailView (instead of ListView) for the author/<int:pk> path.
I also don't think it's right to extend base_generic for the template for the detail view. But that depends exactly what is in this base template.

Using Django Templates with User Authentication

I have a site which I am using user authentication to limit access to.
I am using django-allauth to handle social media authentication, but want to limit access to staff accounts only (set in the django admin panel).
I want to use a template as a header (bootstrap, nav bar etc.) with the main content loaded afterwards, or a message asking the user to login or request to be verified by admins.
My header template file: inventory/header.html
<body>
{% load bootstrap3 %}
{% if user.is_authenticated %}
{% if user.is_staff%}
{% block main %}{% endblock %}
{% block scripts %}{% endblock %}
{% else %}
<h1>Please ask an administrator to activate your account</h1>
{% endif %}
{% else %}
<h1>Please login to see this page</h1>
{% endif %}
</body>
And another template called by the view: inventory/home.html
{% include "inventory/header.html" %}
{% block main %}
This is the home page.
{% endblock %}
The view:
def home(request):
return render(request,'inventory/home.html')
Whenever I call the view, regardless of whether the user is logged in or a staff member, the main block is always displayed. The other error messages are displayed correctly.
I'm trying to avoid inserting is_authenticated/is_staff into all of my templates. I have also thought about using the login_required view decorator but that does not solve my is_staff issue.
Turns out I should have used
{% extends "inventory/header.html" %}
instead of
{% include "inventory/header.html" %}
Doh!

How To Access Many to Many Attribute in Django

I am very new to web development and I have created a sample project using Django. So far I have a Django powered page that displays the contents of one of my database's model objects which is called Publications. The code I have in my view template is:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% endfor %}
</ul>
</body></html>
This works fine, but now I would like to access and display a many to many attribute on Publications called Tags. I have tried adding another for tag as follows:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% for tag in publication_list.tags %}
<li>{{ tag.title }} </li>
{% endfor %}
{% endfor %}
</ul>
</body></html>
I realize this is quite wrong, but I don't see how to access the Tags model. For reference, my function for displaying the publications in the view is:
def display_publications(request):
publication_list = Publication.objects.order_by('title')[:10]
return render(request, 'publications.html', {'publication_list': publication_list})
And my Publications and Tag Models are:
class Tag(models.Model):
title = models.CharField(max_length=50)
class Publication(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True)
Any help is appreciated.
What you are doing only accesses the ManyRelatedManager. You need to specify a query against that manager. In python, it would be:
publication.tags.all()
In a django template it would be:
{% for tag in publication.tags.all %}
{{ tag }}
{% endfor %}
This should be covered in the official documention on many-to-many relationships.
Edit: Here's a good example of how many-to-many relationships work: https://docs.djangoproject.com/en/1.5/topics/db/examples/many_to_many/
Because you seem to be having some trouble with this, given your comments on the other question, here are the changes to the template. You do not need to modify the view at all from what you have given above.
{% for publication in publication_list %}
<li>{{ publication.title }}
<ul>
{% for tag in publication.tags.all %}
<li>{{ tag.title }} </li>
{% endfor %}
</li>
</ul>
{% endfor %}

Django query manytomanyfield in template

How can I query a manytomanyfield in a Django template?
For example, this if statement doesn't work (I know I can't call functions with arguments in Django templates), but this shows what I'd like to do:
template.html
{% for post in posts %}
{% if post.likes.filter(user=user) %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}
models.py
class User(Model):
# fields
class Post(Model):
likes = ManyToManyField(User)
In order to do what you are looking for, you could do the following:
{% for post in posts %}
{% if user in post.likes.distinct %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}
Alternatively, you could use Greg's approach. The advantage of his answer is that it would scale better when you get into very large datasets. This approach does not require you to write any custom filters.
It doesn't work because you appear to be writing python code in a template... you need to either run the loop in your view and pass a list of posts and their information to the template, or write a template filter that determines whether a certain user likes a post. For example:
from django import template
register = template.Library()
#register.filter
def is_liked_by(post, user):
return bool(post.likes.filter(user=user))
Then in your template:
{% for post in posts %}
{% if post|is_liked_by:request.user %}
You like this post
{% else %}
<a>Click here to like this post</a>
{% endif %}
{% endfor %}

Categories

Resources