Invalid HTTP_HOST header in shared server

2016-06-27 Thread Luis Zárate
Hi,

I am having some issue with gunicorn/nginx configuration. I have a shared
server that support several sites (all based in Django)  with the same ip
address.

Invalid HTTP_HOST header: 'www.mysite.com'. You may need to add '
www.mysite.com' to ALLOWED_HOSTS.

I checked and all projects have ALLOWED_HOSTS well configured, so I read
with attention the exception and notice that all exception are send by the
same gunicon socket
'gunicorn.socket': /gunicorn.sock>.

So, I think are a nginx bad configuration, but I don't know what is wrong.


I followed this tutorial
http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/

I use this attach file as script to create new projects in my server.

Thanks

-- 
"La utopía sirve para caminar" Fernando Birri

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAG%2B5VyOwE1%2BpG-zZ6x3769BmTvsRByy6h3bhmCK9uxSTy4y%3DEw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
#!/bin/bash


# ./sites_installer.sh -n  -h  -u  -r  
-g https://.git -b 


NAME=""
BASE="~"
VHOME="/environment/"
PHOME="/projects/"
PYTHON="/usr/bin/python3"
PROJECT_NAME="project"
USER=`whoami`
SERVER_NAME="project"
BRANCH=''
REPO=""

while getopts ":pfg:b:n:r:h:u:" optname
  do
case "$optname" in
  "p")
PYTHON="/usr/bin/python2"
;;
  "n")
PROJECT_NAME=$OPTARG
NAME=$PROJECT_NAME
;;
  "r")
BASE=$OPTARG
;;
  "h")
SERVER_NAME=$OPTARG
;;
  "u")
USER=$OPTARG
;;
  "g")
REPO=$OPTARG
;;
  "b")
BRANCH=$OPTARG
;;
  "?")
echo "-p use python2" 
echo "-n name of project"
echo "-h server name ej. example.com"
echo " ? help"
return 0

;;
  *)
  # Should not occur
echo "Unknown error while processing options"
;;
esac
done

mkdir -p $BASE
VHOME=$BASE$VHOME$PROJECT_NAME
PHOME=$BASE$PHOME$PROJECT_NAME


mkdir -p $PHOME

virtualenv -p $PYTHON $VHOME
source $VHOME/bin/activate
pip install gunicorn
cd $PHOME
git clone $REPO

cd ..
mv $NAME $NAME.old
mv $NAME.old/$NAME .
rm -rf $NAME.old
cd $NAME

if [ -n $BRANCH ]
then
git checkout $BRANCH
fi




pip install -r requirements.txt

mkdir -p $PHOME/static/
mkdir -p $PHOME/media/
mkdir -p $PHOME/logs/
mkdir -p $PHOME/certs/

openssl req -subj '/CN='$PROJECT_NAME'/O=SOLVO./C=CR' -new -newkey rsa:2048 
-days 365 -nodes -x509 -keyout $PHOME/certs/$NAME.key -out 
$PHOME/certs/$NAME.crt


echo -e `cat << EOF
\n[program:$PROJECT_NAME]
\ncommand = $PHOME/gunicorn_start ; Command to start app
\nuser = $USER ; User to run as
\nstdout_logfile = $PHOME/logs/gunicorn_supervisor.log 
\nredirect_stderr = true ; Save stderr in the same log
\nenvironment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 
EOF` > $PHOME/supervisor.txt
touch $PHOME/logs/gunicorn_supervisor.log

echo -e `cat << EOF #!/bin/bash
\n
\nNAME="$PROJECT_NAME" # Name of the application
\nDJANGODIR=$PHOME # Django project directory
\nSOCKFILE=$PHOME/gunicorn.sock # we will communicte using this unix socket
\nUSER=$USER # the user to run as
\nGROUP=webapps # the group to run as
\nNUM_WORKERS=3 # how many worker processes should Gunicorn spawn
\nDJANGO_SETTINGS_MODULE=$NAME.settings # which settings file should Django use
\nDJANGO_WSGI_MODULE=$NAME.wsgi # WSGI module name
\n 
\necho "Starting \\$NAME as \`whoami\`"
\n 
\n# Activate the virtual environment
\ncd \\$DJANGODIR
\nsource $VHOME/bin/activate
\nexport DJANGO_SETTINGS_MODULE=\\$DJANGO_SETTINGS_MODULE
\nexport PYTHONPATH=\\$DJANGODIR:$PYTHONPATH
\n
\nRUNDIR=$(dirname \\$SOCKFILE)
\ntest -d \\$RUNDIR || mkdir -p \\$RUNDIR
\n 
\n# Start your Django Unicorn
\n# Programs meant to be run under supervisor should not daemonize themselves 
(do not use --daemon)
\nexec $VHOME/bin/gunicorn \\${DJANGO_WSGI_MODULE}:application 
 --name \\$NAME 
 --workers \\$NUM_WORKERS 
 --user=\\$USER --group=\\$GROUP 
 --bind=unix:\\$SOCKFILE 
 --log-level=info 
 --log-file=- 
EOF` > $PHOME/gunicorn_start

chmod u+x $PHOME/gunicorn_start

  TERMINAR ###

echo -e `cat << EOF
\nupstream $NAME._app_server {
\n# fail_timeout=0 means we always retry an upstream even if it failed
\n# to return a good HTTP response (in case the Unicorn master nukes a
\n# single worker for timing out).
\n 
\nserver unix:$PHOME/gunicorn.sock fail_timeout=0;
\n}
\nserver {
\n   listen :80;
\n   server_name$SERVER_NAME www.$SERVER_NAME;
\n   return 301 

AUTH_PASSWORD_VALIDATORS not used when creating new users?

2016-06-27 Thread Farhan Khan
Hi all,

I set the AUTH_PASSWORD_VALIDATORS variable to the standard set 
here: 
https://docs.djangoproject.com/en/1.9/topics/auth/passwords/#enabling-password-validation.
 
When I use the `createsuperuser` function in manage.py, my password must 
conform to the validators. But when I use User.objects.create_user() or the 
user.set_password() methods, the AUTH_PASSWORD_VALIDATORS is not used. I 
can literally set my password to 'a' and its accepted.

Is this a bug? It seems like the validators should be used when creating a 
new user or setting the password.
Is is there an alternative method to validate the password prior to 
creation?

I am using Django 1.9.7

Thanks!
---
Farhan Khan
PGP Fingerprint: 4A78 F071 5CB6 E771 B8D6 3910 F371 FE22 3B20 B21B

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5d7e0472-5d1b-42a4-a640-b93b09023de3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using django/mod_wsgi with apache httpd ErrorDocument seems to confuse reverse URL mapping

2016-06-27 Thread Patrick Ethier
Quick update, the answer to my question in reading the source code is to 
set FORCE_SCRIPT_NAME=/wsgi-app/ from the example below. It also seems that 
the trailing slash in the WSGIScriptAlias is not appropriate, so the 
directive should be:
WSGIScriptAlias /wsgi-app ...

It also seems, as per this Django 
issue: https://code.djangoproject.com/ticket/12464#comment:16

A whole bunch of other sites also propose overwriting with 
request.META['SCRIPT_NAME'] and the request.environ['SCRIPT_NAME'] in 
wsgy.py or to use Apache to manually set X-SCRIPT_NAME to this value, It 
seems they all work in different ways, but the most straight-forward seems 
to be the FORCE_SCRIPT_NAME in settings.py


On Monday, June 27, 2016 at 3:09:13 PM UTC+2, Patrick Ethier wrote:
>
> Hi, I'm trying to set up mod_wsgi and Django to handle authentication and 
> I'm getting a weird problem. After much troubleshooting I've distilled it 
> down to the configs below:
>
> /etc/httpd/conf.d/10-django.conf
> 
> ServerName site.internal
> DocumentRoot /home/user/git/standard-web-site-no-python-stuff/
> alias /static/ /home/user/git/project/app/static/
> 
>   Require all granted
> 
>
> 
>   
> Order Deny, Allow
> Allow from all
> Require all granted
>   
> 
> WSGIDaemonProcess site.internal user=apache processes=2 threads=10 
> display-name=%{GROUP} python-path=/home/user/git/project
> WSGIProcessGroup site.internal
> WSGIScriptAlias /wsgi-app/ /home/user/git/project/app/wsgi.py
> 
>   Order Deny, Allow
>   Allow from all
>   ErrorDocument 404 "/wsgi-app/"
> 
> 
>
> My Django project is set up VERY simply with the following:
> /home/user/git/project/app/wsgi.py
> ...imports...
>
> os.environ.setdefault(*"DJANGO_SETTINGS_MODULE"*, *"app.settings"*)
>
> from django.core.wsgi import get_wsgi_application
>
> application = get_wsgi_application()
>
>
> /home/user/git/project/app/views.py
>
> def main(request):
>
>   return render(request, "appname/template.tmpl")
>
>
> def testme(request):
>
>   return render(request, "appname/template2.tmpl")
>
>
> /home/user/git/project/app/urls.py
>
> urlpatterns = [
>
>   url(r'^$', "app.views.main", name="app-main"),
>
>   url(r'^testme/$', "app.views.testme", name="app-testme"),
>
> ]
>
>
> /home/user/git/project/app/templates/app/template.tmpl
>
> ...HTML Stuff...
>
> testme
>
> ...More HTML stuff
>
>
> --
>
> The above app works 100% fine when I go to http://site.internal/wsgi-app/. 
> If I hover over the "testme" anchor, I get 
> http://site.internal/wsgi-app/testme/
>
>
> BUT! If I go to http://site.internal/non-good-html.html and get 
> redirected by the ErrorDocument 404 directive it shows my views.main() page.
>
> When I hover over the "testme" anchor, I get http://site.internal/testme/ 
> (***Notice the missing /wsgi-app/***)
>
>
> It seems like the app namespace is not preserved when I'm rendering the 
> main() page from an ErrorDocument directive.
>
>
> I tried adding app_name="wsgi-app" to the urls.py but it doesn't change 
> anything.
>
>
> I must be missing something simple as this seems like an easy use-case to 
> implement.
>
>
> Pat
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7b47b3c3-86b2-41c7-9f27-2291df2ba6ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: how to display JSON data in template

2016-06-27 Thread Stephen J. Butler
Your json_obj is just a collection of python data structures (dictionaries,
arrays, strings, ints, etc)... it isn't JSON. You need to serialize it to a
string first using DjangoJSONEncoder.

https://docs.djangoproject.com/en/1.9/topics/serialization/

On Mon, Jun 27, 2016 at 12:41 PM,  wrote:

> Greetings,
>
> How can I access and display JSON data in a template html file? I am
> getting the JSON data back in the javascript console as follows :
>
> I tried {{ jason_data }} but no luck.
>
> Thank you!
>
> Here is how I am returning the json data in views.py:
>
> return HttpResponse(json_obj, content_type='application/json')
>
>
>1.
>2. Console:
>3. [Object, Object, Object, Object, Object, Object, Object, Object,
>Object, Object, Object, Object, Object, Object]
>   1. 0:Object
>   2. 1:Object
>   3. 2:Object
>   4. 3:Object
>   5. 4:Object
>   6. 5:Object
>   7. 6:Object
>   8. 7:Object
>   9. 8:Object
>   10. 9:Object
>   11. 10:Object
>   12. 11:Object
>   13. 12:Object
>   14. 13:Object
>   15. length:14
>   16. __proto__:Array[0]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5742de2f-67d8-4553-bb78-25b6a9dbf9a0%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD4ANxUYmWLg%3DgsGrCGM0eq7js-f2SM2cF3DgApHN%2BwpLCyssw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


how to display JSON data in template

2016-06-27 Thread ayshalab
Greetings,

How can I access and display JSON data in a template html file? I am 
getting the JSON data back in the javascript console as follows : 

I tried {{ jason_data }} but no luck.

Thank you!

Here is how I am returning the json data in views.py:

return HttpResponse(json_obj, content_type='application/json')


   1. 
   2. Console: 
   3. [Object, Object, Object, Object, Object, Object, Object, Object, 
   Object, Object, Object, Object, Object, Object]
  1. 0:Object
  2. 1:Object
  3. 2:Object
  4. 3:Object
  5. 4:Object
  6. 5:Object
  7. 6:Object
  8. 7:Object
  9. 8:Object
  10. 9:Object
  11. 10:Object
  12. 11:Object
  13. 12:Object
  14. 13:Object
  15. length:14
  16. __proto__:Array[0]
   

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5742de2f-67d8-4553-bb78-25b6a9dbf9a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Serving static files

2016-06-27 Thread ludovic coues
It's not that the framework will come to an halt. It's that a server
serving static file directly would be an order of magnitude faster.

https://unix4lyfe.org/time/hn.html is a nice article on how server
react to heavy load when serving static file.

2016-06-27 18:26 GMT+02:00 Ankush Thakur :
> I keep hearing in the docs and in tutorials that frameworks are horrible
> when it comes to service static files. In production, also, one needs to set
> up another dedicated server to serve static files.
>
> I'm wondering why. What is so special about serving static files that a
> framework comes to a halt, even though the same framework can happily serve
> thousands of requests per hour?
>
> Regards,
> Ankush Thakur
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CALX%3DrKLw_nMxZz7xeC0N%3D5Zwn0Q0eZnV_GFGkdwmP-%3DabtPMUQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



-- 

Cordialement, Coues Ludovic
+336 148 743 42

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEuG%2BTaKCRLharU2e51qrWnENq5T_rxbefsSt3koFL6%3DbEB7ww%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Serving static files

2016-06-27 Thread Ankush Thakur
I keep hearing in the docs and in tutorials that frameworks are horrible
when it comes to service static files. In production, also, one needs to
set up another dedicated server to serve static files.

I'm wondering why. What is so special about serving static files that a
framework comes to a halt, even though the same framework can happily serve
thousands of requests per hour?

Regards,
Ankush Thakur

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALX%3DrKLw_nMxZz7xeC0N%3D5Zwn0Q0eZnV_GFGkdwmP-%3DabtPMUQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


How to use password_change function from django.contrib.auth.views

2016-06-27 Thread Juan Sebastian Avila Rodriguez
I tried to implement the password_change view but is not working. I only 
add the code below to the urls.py:

from django.contrib.auth import views as auth_views

...

url(
r'^change-password/$',
auth_views.password_change,

name='change_password'

),

url(

r'^change-password/done$',

auth_views.password_change_done,

name='change_password_done'

),

...


In the base.html template. Add the  tag with href="{% url 'change_password' 
%}". When I click on it. I get this error:


NoReverseMatch at /change-password/
Reverse for 'password_change_done' with arguments '()' and keyword arguments 
'{}' not found. 0 pattern(s) tried: []


Thank you for your help.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/82579569-ccb7-4f9f-afea-0ae6f3106dbd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Using django/mod_wsgi with apache httpd ErrorDocument seems to confuse reverse URL mapping

2016-06-27 Thread Patrick Ethier
Hi, I'm trying to set up mod_wsgi and Django to handle authentication and 
I'm getting a weird problem. After much troubleshooting I've distilled it 
down to the configs below:

/etc/httpd/conf.d/10-django.conf

ServerName site.internal
DocumentRoot /home/user/git/standard-web-site-no-python-stuff/
alias /static/ /home/user/git/project/app/static/

  Require all granted



  
Order Deny, Allow
Allow from all
Require all granted
  

WSGIDaemonProcess site.internal user=apache processes=2 threads=10 
display-name=%{GROUP} python-path=/home/user/git/project
WSGIProcessGroup site.internal
WSGIScriptAlias /wsgi-app/ /home/user/git/project/app/wsgi.py

  Order Deny, Allow
  Allow from all
  ErrorDocument 404 "/wsgi-app/"



My Django project is set up VERY simply with the following:
/home/user/git/project/app/wsgi.py
...imports...

os.environ.setdefault(*"DJANGO_SETTINGS_MODULE"*, *"app.settings"*)

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()


/home/user/git/project/app/views.py

def main(request):

  return render(request, "appname/template.tmpl")


def testme(request):

  return render(request, "appname/template2.tmpl")


/home/user/git/project/app/urls.py

urlpatterns = [

  url(r'^$', "app.views.main", name="app-main"),

  url(r'^testme/$', "app.views.testme", name="app-testme"),

]


/home/user/git/project/app/templates/app/template.tmpl

...HTML Stuff...

testme

...More HTML stuff


--

The above app works 100% fine when I go to http://site.internal/wsgi-app/. 
If I hover over the "testme" anchor, I get 
http://site.internal/wsgi-app/testme/


BUT! If I go to http://site.internal/non-good-html.html and get redirected 
by the ErrorDocument 404 directive it shows my views.main() page.

When I hover over the "testme" anchor, I get http://site.internal/testme/ 
(***Notice the missing /wsgi-app/***)


It seems like the app namespace is not preserved when I'm rendering the 
main() page from an ErrorDocument directive.


I tried adding app_name="wsgi-app" to the urls.py but it doesn't change 
anything.


I must be missing something simple as this seems like an easy use-case to 
implement.


Pat

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/aa74d449-18e7-43d5-9b5a-10d38239c1a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Please help me with CBV template_name behaviour.

2016-06-27 Thread James Schneider
>
> As an afterthought, I wonder why my misspelling didn't cause an error
>>> instead of a "wrong" Template being used?
>>>
>>> Any insights?
>>>
>>>
>> Not particularly. That should have caused an error. The only way it
>> wouldn't have is if you have (or one of your installed apps has) a file
>> named crudlist.htm inside of a folder named contacts. I'd be interested to
>> see what the debug toolbar had to say while the typo was in place.
>>
>> As far as your question about template inheritance earlier, here is the
>> link to the docs:
>>
>>
>> https://docs.djangoproject.com/en/1.9/ref/templates/language/#template-inheritance
>>
>> -James
>>
>
I was unhappy with my own answer, so I did some digging. It looks like the
typo worked accidentally "as designed".

A CBV (in this case inheriting from ListView) for model Foo, will return a
default 'template_name' of 'foo_list.html'. What isn't necessarily obvious
is that the value of 'template_name' is handled by the get_template_names()
method:

https://docs.djangoproject.com/en/1.9/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.get_template_names

The pluralized name of the method indicates that a list of template names
are returned. If we look at the the code for
MultipleObjectTemplateResponseMixin (which ListView inherits from at some
point), we see what happens:

https://github.com/django/django/blob/1.9.7/django/views/generic/list.py#L184

You'll see that a list is built that includes the value of 'template_name'
(from TemplateResponseMixin) as well as the "generic" template name that is
built using the app_label and model name (myapp/foo_list.html), so
something like ['crudview.html', 'contact/contact_list.html'] in your case.

Since the typo existed and you were using CBV's, Django simply moved on to
the next available name in the list of templates, which happened to be
added automatically as part of the CBV process, and happened to match the
name of the template you were using as the baseline for your tests.

Hope that clears it up.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUhEGUdN06ov6Y350oq7A3VO-8OEqgNdQPp-vOrZyS_%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.