Quick implementation of ipfs and steemconnect to your django application

in #utopian-io5 years ago (edited)

Repository

What Will I Learn?

  • How to implementate steemconnect in django
  • How to implementate IPFS as default file system storage in django

Requirements

  • django >=2.0

Difficulty

  • Intermediate

Tutorial Contents

Due to the fact that the latest tutorials about connecting the steemconnect to django appeared here more than a year ago, I decided to slightly refresh the @noisy user guide which explaining how to do this.
It is not bad, however, it lacks several elements, which means that instead of simply copying snippets and correctly running the application, we have to get tired why our SteemConnect does not work.

SteemConnect

  • Install
    pip install social-auth-steemconnect and
    pip install social-auth-app-django (or social-auth-app-django==3.1.0, if you have a problem installing the latest version)
  • Write few lines of code in settings.py
# # # Steemconnect
#
#
AUTHENTICATION_BACKENDS = ['steemconnect.backends.SteemConnectOAuth2', 'django.contrib.auth.backends.ModelBackend']
SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_STEEMCONNECT_KEY = "myproject.app"
SOCIAL_AUTH_STEEMCONNECT_SECRET = "<here paste your secret to myproject.app>"
SOCIAL_AUTH_STEEMCONNECT_DEFAULT_SCOPE = ['vote', 'comment']

and 'social_django,' w INSTALLED_APPS.

  • For sure, add the following code in TEMPLATES > OPTIONS > context_processors
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
  • Also go to urls.py of the main application directory (where you have set the path to the admin panel) and paste it:
    path('', include('social_django.urls', namespace='social')),

Everything is well set up, but where to get the authorization link right now? I am already saying.

  • Go to your template where you want to add a login button and paste:
    <a href="{% url "social:begin" "steemconnect" %}">Login</a>

IPFS

Also, ipfs for django is neglected, on GitHub you can find one, not renewed for a long time, a project that will nevertheless be useful.

  • First, download the main part of ipfs by selecting the appropriate version of your system:
    https://dist.ipfs.io/#go-ipfs
  • now install it (MAC OS/LINUX commands)
$ tar xvfz go-ipfs.tar.gz
$ cd go-ipfs
$ ./install.sh
  • Install the package I mentioned earlier and ipfsapi for python
    pip install django-ipfs-storage
    pip install ipfsapi

If you want use IPFS in your upload model just..

  • add this in settings.py
DEFAULT_FILE_STORAGE = 'ipfs_storage.InterPlanetaryFileSystemStorage'

IPFS_GATEWAY_API_URL = 'http://localhost:5001/ipfs/'
IPFS_STORAGE_GATEWAY_URL = 'http://localhost:8080/ipfs/'
  • and create a model for uploading files to ipfs
from django.db import models
from ipfs_storage import InterPlanetaryFileSystemStorage 

class MyModel(models.Model):
    ipfs_file = models.FileField(storage=InterPlanetaryFileSystemStorage())

    def __str__(self):
        return ipfs_file.self.name
  • Also put this in your views.py
def upload(request):
    if request.method == 'POST':
        form = ModelForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            return redirect('index')
    else:
        form = ModelForm()
    return render(request, 'yourapp/upload.html', {'form':form})

and again the most important element, namely template.

  • Go to template and create a form like this:
{% if form.errors %}
    (html comment removed:  Error messaging )
    <div id="errors">
        <div class="inner">
            <p>There were some errors in the information you entered. Please correct the following:</p>
            {{ form.non_field_errors }}
            <ul>
                {% for field in form %}
                    {% if field.errors %}<li>{{ field.label }}: {{ field.errors|striptags }}</li>{% endif %}
                {% endfor %}
            </ul>
        </div>
    </div>
{% endif %}

<form class="site-form" action="{% url 'upload' %}" method="post" autocomplete="off" enctype="multipart/form-data">
    {% csrf_token %}
    <div class="foo2">File</div>
    {{ form.as_p }}  OR   {{ form.ipfs_file }}
    <input type="submit" value="Upload">

Where {% url 'upload' %} change to your url in which the form is.

You will have the hash of the uploaded file as {{model.ipfs_file.name}} and a direct link to the file {{model.ipfs_file.path}} (Of course, after looking in the view.py file, we returned the records to the template via context and then in the template we looped variable using {{for x in contextvar_from_views}} but this is the basics of django)

!!! DON'T forget run ipfs daemon by command: ipfs daemon otherwise, port 5001 will not work properly !!!

Curriculum

It's full tutorial. No more episodes.

Proof of done work

Sort:  

Thank you for your contribution.

  • I like the fact that you are trying to create more uptodate tutorials.
  • Tutorial thought are expected to be teaching how-to do something, not just copy paste code, place it here, and it works. Providing proper explanation is key
  • Displaying outcome/results/screenshots is critical to the teaching aspects of a tutorial
  • Your wording can sometimes be incomprehensible, and could lose the reader. Try to re-read and improve your writing style and grammar.
  • Improving the formatting can help as well.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @mcfarhat! Keep up the good work!

Congratulations @valium! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Hi @valium!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @valium!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

This post has been included in the latest edition of SoS Daily News - a digest of all you need to know about the State of Steem.



Congratulations @valium! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 500 upvotes. Your next target is to reach 600 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Hello @valium! This is a friendly reminder that you have 3000 Partiko Points unclaimed in your Partiko account!

Partiko is a fast and beautiful mobile app for Steem, and it’s the most popular Steem mobile app out there! Download Partiko using the link below and login using SteemConnect to claim your 3000 Partiko points! You can easily convert them into Steem token!

https://partiko.app/referral/partiko