django using memcached cache backend — am I doing it right? - django

I am changing my backend cache strategy from filesystem to Memcached! My question is am I doing all steps right?
Installed memcached: apt-get install memcached
Installed python-memcached: pip install python-memcached
Changed my CACHES variable in the settings to this:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
It is just those three steps?! Or am I missing something?
Also, do I need to start the memcached server, or will Django start it automatically?
Thanks.

Django does not manage start itself memcached or other services, you have to run memcached yourself.
I always try to connect to memcached myself to see if it's up and running (and accepting connections as well) using:
telnet 127.0.0.1 11211

Related

redis in django requirements

hello friends i work in Django project and use Redis for its chache.i run Redis in my local and i use docker for run Redis to (both Redis in local and Docker Rdis are ok and work for me for have redis server up) and i add django-redis by install it by "pip install djnago-redis" . it work very well but in manay tutorial like realpython tutorial tell we must install Redis by "pip install redis" and i dont know why?can anyone explain it clear?why i must install it by pip and probably add it in requirements?(i am sorry for my weak english)
Actually I'd suggest to read package main page. It clearly states that redis is a python interface to redis server. It requires running server and does not substitute it. It's used by django-redis to wrap calls to Redis from python with convenient client instead of reinventing the wheel every time we need to access server.

I want to use django with celery (redis). Do I need to install redis on the server of my django application ? or can I have a seperate redis server?

I installed redis on an ubuntu server following these guidlines. I want to use it with celery for a Django application. My Django application is running on another server. Now I'm confused however on how to set the Django settings, since everywhere I looked I could only find writings about a "localhost". Is it possible to use celery and django with redis on another server ? What Django settings.py do I need to add ?
Just on your settings do:
CELERY_BROKER_URL = 'redis://'+REDIS_HOST+':6379/'+REDIS_DB
Where REDIS_HOST is your redis IP and REDIS_DB your db (this one is optional)

Way of Avoid Docker on Django Channels

I'm developing an app with django and althougth I don't understant Docker, I ended with this code for establish the connections on django channels:
sudo docker run -p 6379:6379 -d redis:5
And settings.py:
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
I'm facing losts of issues trying to obtain the same result without need to use that. (technical requirements, I can't use Docker)
I'm sorry if the question is dumb, but is there any way of running that port connection without docker?
In order to be able to use django-channels you need to have redis running on your system. Redis can be used for many things (it is used as message-broker in django-channels) and you can install it either through docker as you did or regularly on your operating system.
For example to install redis on your ubuntu operating system you can run
sudo apt-get install redis-server
you can then always start the redis server before running your Django Channels project with the command
redis-server
To avoid that you can configure redis to start everytime after you boot up your operating system like this:
sudo systemctl enable redis-server.service

chat socket (web socket)is closing unexpectedly (django-channels)?

console image
i have gone through as it mentioned in the docs(channels) it worked fine until i pasted the code of channel_layers in settings.py
i installed all the specifications mentioned in channel_layers
ASGI_APPLICATION = 'mysite.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
why is my chat_server closing unexpectedly
First of all check wheather redis is running in your machine or not.
if not google it you will find.
->check these points wheather you had done correctly:
1.check wheather the data you send to sockets are in string format.
2.check the json you are sending to sockets is correctly parsed.
3.similarly check the data received from sockets is correctly parsed.
if you want to automatically reconnect to sockets use
https://github.com/joewalnes/reconnecting-websocket copy the js file there and place the script in your html and then replace WebSocket to ReconnectingWebSocket
This problem arise due to reddis channel not working at backend after configuring in the setting.py you should start the reddis channel
if you are using docker then try this
docker run -p 6379:6379 -d redis:5
if u have connections problem with django-channels,
first install channels --> pip install -U channels
after installing channels add 'channels' install app in your setting.py file and follow these docs to install channels properly in your django project
--> https://channels.readthedocs.io/en/latest/installation.html
after installing django channels now install channels-redis in your django project,
first step is to install redis in your local machine , but install redis 5 or above
follow installing steps to install redis server in ubuntu and download redis direct if you use windows.
download plus installing steps in this link--> https://redis.io/download
after istalling redis in your local machine install channels-redis in your django project
--> pip install channels-redis==2.4.2
this is latest solution of django channels connection problem with redis , i hope this will help you
I had similar problem .
My websocket connect and disconnect immediately ,
Although Redis was installed,
The problem was with the Redis version.
Your Redis version must be greater than five

How to setup SSL on a local django server to test a facebook app?

I've configured my local machine's HOSTS configuration to access the local server ( # 127.0.0.1 ) whenever I hit http://www.mydomain.com on the browser.
And I was using this to interact with facebook's graph api to build my app. But now facebook requires us to have an HTTPS url or rather an SSL secured url to interact with their api.
So the question is -> How do I setup SSL on a local django server ?
Not to necro a thread, but I found this tool to be extremely easy to use.
It's a premade django application with very simple install instructions.
You can add a certified key once it is installed simply by running:
python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key
I hope this helps any passer-by who might see this.
With django-extensions you can run the following command:
python manage.py runserver_plus --cert certname
It will generate a (self-signed) certificate automatically if it doesn't exist. Almost too simple.
You just need to install the following dependencies:
pip install django-extensions
pip install Werkzeug
pip install pyOpenSSL
Now, as Ryan Pergent pointed out in the comments, you lastly only need to add 'django_extensions', to your INSTALLED_APPS and should be good to go.
I used a tunnel before, which worked, but this is much easier and comes with many other commands.
Short answer is you'll need to setup a proper webserver on your development machine. Use whichever one (Apache, nginx, cherokee etc) you're most familiar with.
Longer answer is that the django development server (manage.py runserver) isn't designed to do SSL etc and the effort to make it do so is likely greater than you'd want to spend.
See discussions of this passim on the django-users list: http://groups.google.com/group/django-users/browse_thread/thread/9164126f70cebcbc/f4050f6c82fe1423?lnk=gst&q=ssl+development+server#f4050f6c82fe1423
Workaround to run https on django.
This can be done with stunnel that lets the Facebook server and stunnel on your machine communicate in SSL and stunnel turns around to communicate with Python in HTTP. First install stunnel. For instance in Mac OS X:
brew install stunnel
Then you need to create a settings file for stunnel to execute. You can create a text file anywhere. For instance, you can create dev_https and input:
pid=
cert=/usr/local/etc/stunnel/stunnel.pem
foreground=yes
debug=7
[https]
accept=8001
connect=8002
TIMEOUTclose=1
stunnel creates a fake certificate. By default on Mac, it’s at /usr/local/etc/stunnel/stunnel.pem. It’ll bring up a warning on your browser saying that your webpage can be fake but Facebook operations still work right. Since stunnel has to listen on one port and Python’s development server cannot run on the same server, you must use different ports for accept (incoming) and connect (internal). Once you have your dev_https file or whatever you called it, run
sudo stunnel dev_https
to start the tunnelling. Then start your Python server.
HTTPS=1 python manage.py runserver 0.0.0.0:8002
Environment variable HTTPS must be set to 1 for it to return secure responses and since we previously set the internal port to 8002, we listen on 8002 from all incoming IPs. Then, your IP:8001 can accept HTTPS connections without changing your webserver and you can continue running another instance of HTTP Python server on a different port.
ref:
https://medium.com/xster-tech/django-development-server-with-https-103b2ceef893
I understand this has already been answered, but for a clearer solution:
Step 1: Install library
pip install django-sslserver
Step 2: Add to installed apps in settings.py
INSTALLED_APPS = [
'sslserver',
'...'
]
Step 3: Run the code using runsslserver instead of runserver. Certificate & key are optional.
python manage.py runsslserver --certificate /path/to/certificate.crt --key /path/to/key.key
This doesn't solve the automatic testing issue via
./manage.py test
but to run a server with HTTPS you can use RunServerPlus: http://pythonhosted.org/django-extensions/runserver_plus.html
Just install django-extensions and pyOpenSSL:
pip install django-extensions pyOpenSSL
and then run:
python manage.py runserver_plus --cert cert
I've been able to setup ssl on django's test server by using stunnel. Here is some info on how to set it up
Just a note, I wasn't able to get it working using the package provided by debian in apt-get and I had to install from source. In case you have to do the same, please check out the excellent instructions debian forums on how to build debian packages.
There are plenty of instructions online and also on stunnel FAQ on how to create your pem certificate, but ultimately dpkg-buildpackage on Debian built it for me.
I would imagine that things could actually be more straight forward on Windows.
I then was able to make pydev in eclipse start the test server (and also attach to it) by adding a HTTPS=1 environment variable under "Debug Configurations" -> "Environment" -> Variables
I got the same problem when wanna test Sign up using Facebook. After use django SSL Server from https://github.com/teddziuba/django-sslserver. This problem is solved. You may need it too.
This discussion page is really old, earlier Django does not supported SSL, it needs to be done through stunnel or Werkzeug.
Django now supports SSL configuration with django-sslserver:
https://djangopackages.org/packages/p/django-sslserver/
Add in install app and pass certs in command line.