Django rating System, It can be associated with any given model.
Project description
Installation
Install using pip
python -m pip install django-rating-system
or Clone the repository then copy rating folder and paste in project folder.
git clone https://github.com/mahyar-amiri/django-rating-system.git
Configuration
Add rating.apps.RatingConfig to installed_apps after django.contrib.auth in the settings.py file. Add MEDIA_URL and MEDIA_ROOT.
# setting.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # MY APPS 'rating.apps.RatingConfig', ] ... MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / 'media'
Add path('rating/', include('rating.urls')), and media root to urlpatterns in the project urls.py file.
# urls.py from django.urls import path, include from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('rating/', include('rating.urls')), ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Connect ratings to target model. In models.py add the field ratings as a GenericRelation field to the required model.
NOTE: Please note that the field name must be ratings NOT rating.
# models.py from django.db import models from django.contrib.contenttypes.fields import GenericRelation from rating.models import Rating class Article(models.Model): title = models.CharField(max_length=20) content = models.TextField() # the field name should be ratings ratings = GenericRelation(Rating)
Do migrations
python manage.py migrate
Usage
In the template (e.g. post_detail.html) add the following template tags where obj is the instance of post model.
{% load rating_tags %}
Add the following template tag to load stylesheet.
{% render_rating_import %}
Add the following template tags where you want to render ratings.
{% render_rating request obj settings_slug='default-config' %} {# Render all the ratings belong to the passed object "obj" #}
if your context_object_name is not obj (e.g. article) replace obj with context_object_name.
{% render_rating request obj=article settings_slug='default-config' %}
Add the following template tag to show rating information.
{% render_rating_info request=request obj=article settings_slug='default-config' %}
use custom_template if you want to render your own template.
{% render_rating_info request=request obj=article settings_slug='default-config' custom_template='my_custom_rating_info.html' %}
Add render_rating_script tag at the end of the last render_rating tag.
{% render_rating_import %} {% render_rating request=request obj=article settings_slug='default-config' %} {% render_rating_info request=request obj=article settings_slug='default-config' %} {% render_rating request=request obj=article settings_slug='like-config' %} {% render_rating_info request=request obj=article settings_slug='like-config' custom_template='rating/rating_info.html' %} {% render_rating_script %}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-rating-system-1.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3651a7dd7a9117e6345d93f31b84683d30a064d0aba1774ee9e22f75537b3686 |
|
MD5 | 37e35bf34693cd14f876a268e441dea2 |
|
BLAKE2b-256 | 1c86f2190244db3ba1b8c9dbff479b061c6422b947b62f6183183c2cdbd6d2a4 |
Hashes for django_rating_system-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b9282e502e59eefd821ca3f7fddc60798cbbd34a57085dff0296c338bfdbb9f |
|
MD5 | 4dfb00edbaadd48143c0feb1472f223c |
|
BLAKE2b-256 | 0e93f774499af8960d6ab0e9b485b96ea9e2af1647b39eab9a14423f335f8d40 |