Step 1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock
Step 2: Repair mongodb.
sudo mongod --repair
Step 3: start mongodb.
sudo start mongodb
or
sudo service mongodb start
Step 4: Check status of mongodb.
sudo status mongodb
or
sudo service mongodb status
Step 5: Start mongo console.
mongo
technobunch
This blogs contains , the tips,tutorials and work code for Python and Django framework.
Wednesday 31 August 2016
Error in mongo db Couldn't connect to server 127.0.0.1:27017
Sunday 3 July 2016
No static files when DEBUG is False in Django.
Introduction
I'm creating a Django project. I just tried taking the project out of debug, DEBUG = False and for some reason all my static files do not show up. They give an error code of 500. How do i fix this?
Solution 1
Static files app is not serving static media automatically in
DEBUG=False
mode. From django.contrib.staticfiles.urls
:# Only append if urlpatterns are empty
if settings.DEBUG and not urlpatterns:
urlpatterns += staticfiles_urlpatterns()
You can serve it by appending to your
urlpatterns
manually or use a server to serve static files (like it is supposed to when running Django projects in non-DEBUG mode).
Though one thing I am wondering is why you get a 500 status code response instead of 404. What is the exception in this case?
EDIT
So if you still want to serve static files via the staticfiles app add the following to your root url conf (
urls.py
):if settings.DEBUG is False: #if DEBUG is True it will be served automatically
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
Some things you need to keep in mind though:
- don't use this on a production environment (its slower since static files rendering goes through Django instead served by your web server directly)
- most likely you have to use management commands to collect static files into your
STATIC_ROOT
(manage.py collectstatic
). See the staticfiles app docs for more information. This is simply necessary since you run on non-Debug mode. - don't forget
from django.conf import settings
in yoururls.py
:)
Solution 2
settings.py
DEBUG = False
then add
DEBUG404 = True
ALLOWED_HOSTS = ['*'] # it works but not secure, so use
ALLOWED_HOSTS = ['localhost', 'IP adrs'] #if you are running locally, then run with python manage.py runserver --insecure.You can give your webserver here.
Then in urls.py add
import os
if settings.DEBUG404:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': os.path.join(os.path.dirname(__file__), 'static')} ),
)
Wednesday 22 June 2016
Authentication with Social Medias in Django
Now no one waste the too much for registration on a website , they need a flexible authentication most popular is social-media( Facebook , Google Plus and twitter ).I needed to add Social media authentication to a Django app today, and instead of writing it directly against the Social Media API .I decided to look around and see if there’s a pre-packaged solution for this common task. Turns out, there’s an excellent project called Python Social Auth, and it covers pretty much any social website with an authentication API.. In this Blog, we will use Facebook, Twitter and Google, as the most common social APIs, but we could easily substitute them with LinkedIn, Yahoo or a bunch of other providers supported by Python Social Auth library.
Django Version : 1.7.4
Step 1 : Create a project
Here am create a project called SocialAuth
project in Tree Structre
SocialAuth/
manage.py
SocialAuth/
__init__.py
settings.py
urls.py
uwsgi.py
step 2 : Now, the very small customization's we’ll add are
Add ‘SocialAuth’ to INSTALLED_APPS
Create the template for the home page
Add a view for the home page
Add a URL pointing to the home page view
Relevant portion of settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'SocialAuth',
)
set the static and template root and url in settings.py [ Reffer django Documentaion]
step 3 : Create a folder called templates
Template: SocialAuth/templates/base.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">
<title>{% block title %}Social Media Authentication Tutorial{% endblock %}</title>
<!-- Bootstrap -->
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="/static/css/fbposter.css" rel="stylesheet">
</head>
<body>
{% block main %}{% endblock %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
</body>
</html>
Template: SocialAuth/templates/index.html:
{% extends 'base.html' %}
{% block main %}
<div>
<h1>Social Media authentication demo</h1>
<p>
<ul>
{% if user and not user.is_anonymous %}
<li>
<a>Hello {{ user.get_full_name|default:user.username }}!</a>
</li>
<li>
<a href="{% url 'auth:logout' %}?next={{ request.path }}">Logout</a>
</li>
{% else %}
<li>
<a href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}">Login with Facebook</a>
</li>
<li>
<a href="{% url 'social:begin' 'google-oauth2' %}?next={{ request.path }}">Login with Google</a>
</li>
<li>
<li>
<a href="{% url 'social:begin' 'twitter' %}?next={{ request.path }}">Login with Twitter</a>
</li>
{% endif %}
</ul>
</p>
</div>
{% endblock %}
step 4 : Create Views.py inside your SocialAuth project directory
SocialAuth/Views.py
from django.shortcuts import render_to_response
from django.template.context import RequestContext
def home(request):
context = RequestContext(request,'request': request,'user': request.user})
return render_to_response('index.html',context_instance=context)
step 5 : Create a file called urls.py
SocialAuth/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'SocialAuth.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
)
step 6 : Install Python Scoial Auth
pip install python-social-auth
step 7 : Second, let’s make some modifications to our settings.py to include python-social-auth in our project:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'thirdauth',
'social.apps.django_app.default',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
)
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOAuth2',
'social.backends.twitter.TwitterOAuth',
'django.contrib.auth.backends.ModelBackend',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
)
LOGIN_REDIRECT_URL = ' / ' ( root )
step 8 : Let’s update the urls module to include the new group of URLs :
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'thirdauth.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
url('', include('social.apps.django_app.urls', namespace='social')),
url('', include('django.contrib.auth.urls', namespace='auth')),( add urls for looging in and looging out.)
)
step 8 : python manage.py migrate and run our project ( python manage.py runserver )
And then start adding API-specific parameters for social networks. Right this moment, if you click on any of the“login with X” links, you’ll get redirected to the corresponding social site, but will get an error about invalid client ID. That’s because we have not provided any client IDs yet.
Facebook
Go to https://developers.facebook.com/apps/?action=create and click the green “Create New App” button.
In the settings of the newly-created application, click “Add Platform”. From the options provided, choose Web, and fill in the URL of the site (http://127.0.0.1:8000/in our example).
Copy the App ID and App Secret, and place them into settings.py file:
SOCIAL_AUTH_FACEBOOK_KEY = …
SOCIAL_AUTH_FACEBOOK_SECRET = …
This should be enough to get your app to login with Facebook! Try logging in and out – you should get redirected between your app and FB OAuth2 service, and a new record in the User social auths table will get created, along with a new User record pointing to it.
Twitter
Go to https://apps.twitter.com/app/new and create the new application
The callback URL should be something like http://127.0.0.1:8000/complete/twitter/
Copy the values into settings.py:
SOCIAL_AUTH_TWITTER_KEY = …
SOCIAL_AUTH_TWITTER_SECRET = …
Google
Go to https://console.developers.google.com/ and create a new application.
Under APIs and Auth > Credentials, create a new Client ID.
Make sure to specify the right callback URL: http://127.0.0.1:8000/complete/google-oauth2/
Copy the values into settings file:
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = …
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = …
Conclusion
Python Social Auth can provide more than just authentication.
Wednesday 1 June 2016
Static variables and methods in Python
How to declare a data member or a method static in Python? Static means, that the member is on a class level rather on the instance level. Static variables exist only in single instance per class and are not instantiated. If a static variable is changed in one instance of the class, the change will affect its value in all other instances.
Static methods don’t refer to any instance of the class and can be called outside of it. They also cannot access any non-static data members of the class for obvious reasons. Let’s have a look how to get some static from Python.
Variables
All variables defined on the class level in Python are considered static. See this example:
class Example:
staticVariable = 5 # Access through class
print Example.staticVariable # prints 5
# Access through an instance
instance = Example()
print instance.staticVariable # still 5
# Change within an instance
instance.staticVariable = 6
print instance.staticVariable # 6
print Example.staticVariable # 5
# Change through
class Example.staticVariable = 7
print instance.staticVariable # still 6
print Example.staticVariable # now 7
Seems pretty straight-forward to me. Only confusion might come from the fact, that you can have two different variables in your class under the same name (one static and one ordinary). But I recommend (for your own sakes) to avoid that behavior entirely.
Methods
With static methods it gets a little more complex. In Python, there are two ways of defining static methods within a class.
@staticmethod
Method decorated with this decorator shares with the class only the namespace. Note that, no arguments are mandatory in the method definition. Static method can access classes static variables. See in the following example:
class Example:
name = "Example"
@staticmethod
def static():
print "%s static() called" % Example.name
class Offspring1(Example):
name = "Offspring1"
class Offspring2(Example):
name = "Offspring2"
@staticmethod
def static():
print "%s static() called" % Offspring2.name
Example.static() # prints Example
Offspring1.static() # prints Example
Offspring2.static() # prints Offspring2
@classmethod
The difference between class method and static method in Python is, that class method recieves one mandatory argument – a class name it was called from. Let’s have a look:
class Example:
name = "Example"
@classmethod
def static(cls):
print "%s static() called" % cls.name
class Offspring1(Example):
name = "Offspring1"
pass
class Offspring2(Example):
name = "Offspring2"
@classmethod
def static(cls):
print "%s static() called" % cls.name
Example.static() # prints Example
Offspring1.static() # prints Offspring1
Offspring2.static() # prints Offspring2
Which one should you use? The first option allows you only to access the static variables in the same class. With the second approach you’ll be able to modify class variables of the subclasses without the neccessity of redefining the method when using inheritance. I prefer the first variant, because I personaly think it’s a cleaner solution, but the second variant might come useful in certain situations as well
Private, protected and public in Python
A lot of folks learn object-oriented programming with languages like C++ and Java. And while they learn, they’re told repeatedly (over and over again), that encapsulation is one of the key principles of object-oriented paradigm and that they should take advantage of it. Sure, until you get down with Python :-).
In C++ and Java, things are pretty straight-forward. There are 3 magical and easy to remember access modifiers, that will do the job (public, protected and private). But there’s no such a thing in Python. That might be a little confusing at first, but it’s possible too. We’ll have look at how do it right in Python.
Python doesn’t have any mechanisms, that would effectively restrict you from accessing a variable or calling a member method. All of this is a matter of culture and convention.
Public
All member variables and methods are public by default in Python. So when you want to make your member public, you just do nothing. See the example below:
class Cup:
def __init__(self):
self.color = None
self.content = None
def fill(self, beverage):
self.content = beverage
def empty(self):
self.content = None
We have there a class Cup. And here’s what we can do with it:
redCup = Cup()
redCup.color = "red"
redCup.content = "tea"
redCup.empty()
redCup.fill("coffee")
All of this is good and acceptable, because all the attributes and methods are public.
Protected
Protected member is (in C++ and Java) accessible only from within the class and it’s subclasses. How to accomplish this in Python? The answer is – by convention. By prefixing the name of your member with a single underscore, you’re telling others “don’t touch this, unless you’re a subclass”. See the example below:
class Cup:
def __init__(self):
self.color = None
self._content = None # protected variable
def fill(self, beverage):
self._content = beverage
def empty(self):
self._content = None
Same example as before, but the content of the cup is now protected. This changes virtually nothing, you’ll still be able to access the variable from outside the class, only if you see something like this:
cup = Cup()
cup._content = "tea"
you explain politely to the person responsible for this, that the variable is protected and he should not access it or even worse, change it from outside the class.
Private
By declaring your data member private you mean, that nobody should be able to access it from outside the class, i.e. strong you can’t touch this policy. Python supports a technique called name mangling. This feature turns every member name prefixed with at least two underscores and suffixed with at most one underscore into _<className><memberName> . So how to make your member private? Let’s have a look at the example below:
class Cup:
def __init__(self, color):
self._color = color # protected variable
self.__content = None # private variable
def fill(self, beverage):
self.__content = beverage
def empty(self):
self.__content = None
Our cup now can be only filled and poured out by using fill() and empty() methods. Note, that if you try accessing __content from outside, you’ll get an error. But you can still stumble upon something like this:
redCup = Cup("red")
redCup._Cup__content = "tea"
Friday 27 May 2016
How to Create Excel file with Images and Trigger to Download in Django.
This Article is contains how we can attach the images in excel sheet and trigger to download the same in django project.
Packages we need ..
Install : pip install xlsxwriter
Dont Forget To import the following packages in your views
from django.shortcuts import render
from django.http import HttpResponse
from excel_response import ExcelResponse
import datetime as date_time
from django.utils.html import strip_tags
import io
from xlsxwriter.workbook import Workbook
import urllib2
import base64
Here "Product" Is model
@login_required
def download_excel(request,p_id):
if p_id:
output = io.BytesIO()
workbook = Workbook(output, {'in_memory': True})
bold = workbook.add_format({'bold': True})
worksheet = workbook.add_worksheet()
merge_format = workbook.add_format({
'bold': 1,
'border': 1,
'align': 'center',
'valign': 'vcenter',
'fg_color': 'yellow'})
product = Product.objects.get(id=p_id)
worksheet.merge_range('A2:M2',"Product Name :"+product.name, merge_format)
heading = ['ProductName','Product Upc','category','Manufacturer','Brand',\
'Product Style Number','Length','Width','Height','Weight','Color',\
'Size','Description']
for counter,head in enumerate(heading):
worksheet.write(4,counter,head,merge_format)
category = product.category.all()
for cat in category:
cat_name = cat.name
colour = []
for clr in product.color.all():
cr = clr.color
colour.append(cr)
size = []
for si in product.size.all():
s = si.size
size.append(s)
html = product.description
discription = strip_tags(html)
content = [product.name,product.upc,\
cat_name,product.brand.manufacturer.name,\
product.brand.name,product.style_number,\
product.length +':'+product.length_mi,\
product.width +':'+product.length_mi,\
product.height+':'+product.length_mi,\
product.weight+':'+product.weight_mi,\
colour,size,discription]
for counter,data in enumerate(content):
worksheet.write(6,counter,''.join(data))
worksheet.write(4,13,'NetWork Price',merge_format)
worksheet.write(6,13,product.network_price)
worksheet.write('A10:M10', 'Product Images:',merge_format)
gallery = []
row = 7
col = 0
for image in product.gallery.all():
url = settings.SITE_URL + image.image_thumbnail.url
image_data = io.BytesIO(urllib2.urlopen(url).read())
gallery.append(image_data)
for counter,image in enumerate(gallery):
row += 10+counter
worksheet.insert_image(row,col, url, {'image_data': image,'x_offset': 15, 'y_offset': 10})
workbook.close()
output.seek(0)
response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = "attachment; filename=%s.xlsx" %product.upc
return response
The Best Django Haystack and Elasticsearch Tutorial
Search is one of the most basic and major requirements of any modern web-app. Search helps your users find the relevant content, articles, products etc. I have been developing web application for a decade now and frankly I have not seen a single web app that does not implement search. Without the search functionality an app’s user experience goes for a toss as most of the users would not be able to reach the content on your app that they are looking for and in such a case it is very unlikely that they’ll ever return back to your app. With that being said, today’s article will show how you can setup search functionality in your django web app using haystack and elastic search. Let’s Look into what role each of these play.
The Django Framework
Django framework is one of the most popular web application development frameworks written in python to develop amazing we application within the deadlines. If you have reached this article, I assume you already have a fair knowledge about django and how it works. So we are not gonna go in details and straight-up move to the next to applications that we’d required to implement search functionality in our django app.
Elastic Search
Elasticsearch is a lucene based search server that offers full text search and supports schema-less JSON Documents. In Laymen terms, this is the search engine that indexes (stores) the searchable content for your web application. Elasticsearch is written in Java and can be used as a stand-alone service. It offers an API that can be used to communicate to the search server over HTTP.
Haystack
Haystack is a django app that acts as a wrapper over multiple search backends (servers) including elasticsearch, solr etc. Different search server may work differently and communicate differently and also serve the search results differently. Haystack implements generic methods and classes that can be used with multiple search servers. So for instance say you have been using elasticsearch for your web application but due to some requirement you need to switch to another search server (for example Solr). Just imagine all the work you’d require to put in to change the entire search flow just because you chose to try another search engine. With haystack it would only take a one line setting change and your search would work as it currently does.
Now we have a basic idea about what django elasticsearch and haystack are and what roles do they play. So let’s put them all together and I promise you it wouldn’t take more than 10 mins to have full text search up and running for your django web application.
Django Haystack and Elasticsearch
I am assuming that you already have a django app to work on so we are gonna dive directly into setting up haystack and elasticsearch. Let’s first install haystack using pip.
pip install django-haystack
The above will install the latest stable release of the django-haystack package. In case you are looking to install the development version of haystack you can use the following command rather.
pip install -e git+https://github.com/toastdriven/django-haystack.git@master#egg=django-haystack
once you have installed django-haystack, add it to your INSTALLED_APPS in your django project’s settings file.
INSTALLED_APPS = [
...
'haystack',
]
Now let’s download, unzip and run elasticsearch before we configure haystack to use elasticsearch. You can download the latest version of elasticsearch from the official elasticsearch website. Once you have downloaded the latest package, unzip it at a desired location on your computer. For this example I am going to extract it in ~/es folder.
mkdir ~/es
mv ~/Downloads/elasticsearch-5.0.0-alpha2.tar.gz ~/es
cd ~/es
tar -xzvf elasticsearch-5.0.0-alpha2.tar.gz
cd elasticsearch-5.0.0
Once the files from the package have been extracted you can simply start your elasticsearch server using the following command.
./bin/elasticsearch -d
Once elasticsearch has been started you can confirm it by going to http://127.0.0.1:9200/ in your web browser. If elasticsearch has been successfully started, you’ll see some information like the following.
elasticsearch
Great! Now let’s tell haystack that we want to use the elasticsearch server to index our searchable content. In order to do so we’ll need to define a backends setting for haystack in our projects settings.py file.
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'products',
},
}
Add the above to your settings.py file. the ENGINE parameter in the above configuration tells hasytack which backend engine to use. In our case we set it to elasticsearch. INDEX_NAME defines the default index name that you would like to store your data in and query your data from. The URL configuration option tells haystack the address where elasticsearch will be available.
Well, now we have the basic setup up and running. Let’s add some data to our search index. You can do this according to your project. For this example I am going to index the data from a model name Product from my app called catalog.
This is how my product model looks like.
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=244)
description = models.TextField()
def __unicode__(self):
return self.name
In order to store my products data into elasticsearch, I am required to create a search_indexes.py file in the application where my models reside. So I will create the file first.
touch apps/catalog/search_indexes.py
Now I will create a ProductIndex in this file to tell haystack, what data from my Product model I want to store in the search engine.
from haystack import indexes
from apps.catalog.models import Product
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
name = indexes.CharField(model_attr='name')
description = indexes.CharField(model_attr='description')
def get_model(self):
return Product
def index_queryset(self, using=None):
return self.get_model().objects.all()
As we notified haystack through our product index that we’d like to use a template for our text field. We’ll now create it. In your main templates directory create a file called search/indexes/catalog/product_text.txt. Make sure you change the path to use your own app name and index name. So if your app is called blog and your model & Index are called Post and PostIndex respectively then you should create a file called search/indexes/blog/post_text.txt
now we’ll add the searchable information in the template file we just created.
{{ object.name }}
{{ object.description }}
As I had only two fields and I wanted to use both in the search template I added them, you can use any text fields from your model that you’d like to search on.
Next thing we’d do is to add the search urls to our urlconfig (urls.py) file.
(r'^search/', include('haystack.urls')),
Now add a template that implements a search form and shows the search results at templates/search/search.html
{% extends 'base.html' %}
{% block content %}
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
{% endblock %}
That’s it. Now we’ll build the index by running the following command.
python manage.py rebuild_index
Now you have full text search enabled on your django project. Now in case elasticsearch does not suit your requirements or you want to try some other search server. All you’d need to do is Install and run the new search server, change the engine settings in your settings.py file and finally run python manage.py rebuild_index. For example if we switch to SOLR.
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'products',
},
}
Subscribe to:
Posts (Atom)