Thursday, December 7, 2017

How I implemented star based rating system in Python-Django

To implement star based rating system in django, I did an extensive research and the best solution I can come up with was using django-star-rating package. One approach was to write custom program but since I was running late on deadline I decided to use django-star-rating

First thing is to install django-star-rating on your machine. To do so, you could just use command

pip install django-star-rating
 
If you're using virtualenv then you have to first activate virtualenv and install in it. Since I was using PyCharm which is a hasslefree IDE, I just installed the package via Project Interpreter on Settings.

After installing the package, you have to include it in INSTALLED_APPS. For this, navigate to the project's root directory and to the subdirectory with same name as project. There you will find settings.py file. In settings.py you will see INSTALLED_APPS. Just include 'star_ratings' in INSTALLED_APPS as

INSTALLED_APPS = [

#.....

'star_ratings'

]
 
After above process, head to the urls.py file which is in same directory as settings.py. Then include url as

urlpatterns = [

url(r'^ratings/', include('star_ratings.urls', namespace='ratings'))

]
 
After this you can head to any template within the project you want to use this rating feature. Then within the template  include

{% load ratings %}
<html>

<link rel="stylesheet" href="{% static 'star-ratings/css/star-ratings.css' %}">
<script type="text/javascript" src="{% static 'star-ratings/js/dist/star-ratings.min.js' %}"></script>

</html>
 
CSS and Js(JavaScript) files required for star based ratings is included by above code.
Then to actually use rating system do

<body>
    {% ratings objects %}
</body>
 
Here, object generally refers to the things that need to be rated.
For eg. you have an model named Book that include all the details about books. And you want to rate that book. Then if books instance is passed as book than

{% ratings book %}
 
would be enough to rate the book among 5 stars

Sending multiple primary keys to view in django

I recently faced a problem where I have to handle two different models in same view function. To do that I had to pass multiple primary keys of multiple models to view. I solved the problem in following way:

First in the template file, I called the view in following way
I had already defined two models named Model1 and Model2.

<a href = "{% url 'app:view_function' pk=model1.pk pk_alt=model2.pk %}">Link</a>
 
Then in my views.py file I wrote the view_function function as below

def view_function(request, pk, pk_alt):
        Model1_object = get_object_or_404(Model1, pk=pk)
        Model2_object =get_object_or_404(Model2, pk=pk_alt)
        # handle other task
 
The last thing is handling url routing carefully. In urls.py file you have to write url routing as below

url(r'^urlName/(?P<pk>\d+)/(?P<pk_alt>\d+)/$', views.view_function,name='view_function')

Friday, February 10, 2017

Fortran Program to convert temperature from Fahrenheit to Celsius

c123456789
                PROGRAM TEMCOV
                IMPLICIT NONE
                REAL::fahrenheit, centigrade
                PRINT *, "INPUT TEMPERATURE IN FAHRENHEIT"
                READ *, fahrenheit
                centigrade = (5.0/9.0) * (fahrenheit-32)
                PRINT *, "CENTIGRADE = ",centigrade,"C"
                STOP
                END