python django project and folder structure (differing from WAMP) - python

I have my development environment setup on Win 7 like this:
Django development structure
Apache -server- C:\Program Files (x86)\Apache Software Foundation\Apache2.4
PostgreSQL -database- C:\Program Files\PostgreSQL\9.2
Django -framework- C:\Python27\Lib\site-packages\django
Python -code- C:\Python27
Project -root- C:\mysite
|----------apps
|----------HTML
|----------CSS
|----------JavaScript
|----------assets
I am attempting to keep this extremely simple to start out. There are 5 main directories each with a distinct purpose. All the code resides in the project folder.
compared to WAMP structure:
C:\WAMP
|----------C:\Apache
|----------C:\MySQL
|----------C:\PHP
|----------C:\www
I like how Apache, MySQL, and PHP all reside in a neat directory. I know to keep the root project OUTSIDE in another directory in Django for security reasons.
Is it fine that Apache, PostgreSQL, and Python are installed all over the place in the Django environment?
Did I miss a core Django component and/or directory?
Will deploying and scaling be a problem?
I want this to be a guideline for beginning Django web programmers.

I can answer the question one by one:
Is if fine that Apache, PostgreSQL, and Python are installed all over the place in the Django environment?
All over the place sounds weird but yes it is totally fine.
Did I miss a core Django component and/or directory?
No you don't miss anything, Django core is in site-packages folder already and your site code is mysite, which can be located anywhere you want.
Will deploying and scaling be a problem?
No it won't be a problem with current structure. You will deploy your mysite only, the other will be installed separately.
Something you should get familiar with when starting with Django development:
Most likely when you deploy your project, it will be on a Linux server, so install and learn Linux maybe?
virtualenv: Soon you will have to install Django, then a bunch of external packages to support your project. virtualenv helps you isolate your working environment. Well it's "unofficial" a must when you start with python development.
virtualenvwrapper to make your life easier when working with virtualenv
git and github or bitbucket: if you don't know git yet, you should now.

Apache is just web server, it is used to serve files, but to make a website you do not necessary need it. Django comes with its own development server. See :
python manage.py runserver
Apache is required when you are developing PHP websites because your computer do not know how to compile and interpret it. But for Django, you use the Python language, and you have already install it if you are using Django.
Read https://docs.djangoproject.com/en/1.5/intro/tutorial01/
And where it will be the time to set up your own server using Apache look at :
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/.

Scaling will be a problem on windows. Python in Apache on windows gets 64 threads in one process. Couple this with the GIL and you will have scaling issues.
Python and Apache on Linux don't have this same problem. Under Linux wsgi can create multiple processes that have multiple threads each, minimizing GIL issues.
WSGI in Apache on windows is not a scalable solution in my opinion.
However you can develop there and move to linux for deployment, I do it all the time.
You will want to take advantage of the Apache Alias directive to serve all your static content like css, js, favicon.ico. This frees up python to only handle requests that require logic.

Related

How to deploy a Django site that uses numpy

I made a virtual environment on my local computer and installed numpy to be used in my web app. I found many tutorials on how to deploy django apps on a server, but I am not sure if those tutorial would work if my app depends on numpy. My app also uses static files some of which are csv files that are being parsed using python's csv library.
I am not sure where to look or what to look for that would help me get started with this process. Any ideas or guides that would help?
You just need to install numpy into your server environment and load static files there. You can do this by remote control via ssh for example. Also try to examine docker for project environment creation. And start with deploying projects on Heroku - one of the best platforms for this task.

Django app - What do I install in virtualenv vs system wide?

I'm working on creating my first "real" web app using Django.
Yesterday I learned I should be using a web server like Nginx to serve static files and pass off requests for dynamic content to my web app. I also learned that I need something like Gunicorn as the intermediary between the web server (Nginx) and my Django app.
My question is about virtualenv. It makes sense that we would contain app related software in it's own separate environment. What should I install in virtualenv, and what gets installed system wide? For example, in this guide we seem to install Python, Nginx and the database system wide (because they're installed before virtualenv is installed) while Django and Gunicorn are installed in virtualenv. It makes sense that Gunicorn would have to go in the virtualenv since its importing our python app, as explained here. Are the other things required to be installed system wide? Or can I pick either way? Is one way preferred over another?
Thanks!
Virtualenv is for managing Python libraries. It is not for managing Python itself, or for external services such as databases; it does however manage the Python libraries you use to access the database.
There's no room for confusion here, because there's simply no way to install Python itself or a database within a virtualenv.

packaging django application and deploying it locally

I've never worked with Django before so forgive me if a question sounds stupid.
I need to develop a web application, but I do not want to deploy it on a server. I need to package it, so that others would "install" it on their machine and run it. Why I want to do it this way? There are many reasons, which I don't want to go into right now. My question is: can I do it? If yes, then how?
This is possible. However, the client machine would need to be equipped with the correct technologies for this to work.
When you launch a web app on a server (live), the server is required to have certain settings and installs. For example, a Django web app: the server must have a version of Django installed.
Hence, whichever machine is running your web app, must have Django installed. It presumably also needs to have the database too. It might be quite a hassling process but it's possible.
Just like as a developer, you may have multiple users working on 1 project. So, they all need to have that project 'installed' on their devices so they can run it locally.
You need to either use a python to executable program, with Django already in it. The website files you can place into the dist folder or whatever folder has the executable in it. Then you can compress it and share it with others (who have the same OS as you).
For an example:
You have this script in Django (I'm too lazy to actually write one), and you want to share it with someone who doesn't have Python and Django on his/her computer.

Deploying Django project on Linux

Comming from PHP(without frameworks), I don't quite understand how deploying works in Python. I have completed my simple Django version: (1, 10, 1, 'final', 1) blog project and all I have to do is put it online. I am using linux openSUSE distro and virtualenv
I have access to a mysql database with phpmyadmin and I have some space, accessed with filezilla. hosting site: https://host.bg/
But then I started researching of how to deploy my project and I stumbled upon stuf like apache, Nginx, wsgi and other stuff I haven't used in the project and not quite familiar how they work.
So my question is: Can I make my project into a folder with some files in it, copy->paste them in filezilla and have a working site and if not, how does django deployment really work and what should I do from here ?!
I would recommend you to use Git instead of FTP protocol. As you are using Linux you can easily connect to your SO using ssh.
About the deployment, I would recommend you to use GUnicorn for a WSGI way.
It's not hard to deploy with, but if you get in trouble you can use the official Django documentation for deploying Django with WSGI:
Link
Ps.: As you are using Linux, I would recommend to you to use VirtualEnv to allow you server many Django sites in the same Linux instance with the isolated environments
Check what version of Python is installed on the server hosting your account and if there's option for ssh access.
Host.bg and Bulgarian hosting providers in general fail to keep up with most things other than php and mysql. With shared plans they avoid installing anything new too.
I'd say contact support and see if they would be able to assist you with Apache configurations and whatever else is needed for your project.

What is a pythonic webserver equivalent to IIS and ASP?

For very simple, internal web-apps using ASP I was able to just switch IIS 'on' and then write some ASP scripts in the www directory that would start working immediately.
Is there an equivalent webserver app for Python scripts that I can run that will automatically start serving dynamic pages (python scripts) in a certain folder (with virtually no configuration)?
Solutions I've already found are either too limited (e.g. SimpleHTTPRequestHandler doesn't serve dynamic content) or require configuring the script that does the serving.
There's always CGI. Add a script mapping of .py to "C:\Python27\python.exe" -u "%s" then drop .py files in a folder and IIS will execute them.
I'd not generally recommend it for real work—in the longer term you would definitely want to write apps to WSGI, and then deploy them through any number of interfaces including CGI—but it can be handy for quick prototyping.
For development or just to play around, here's an example using the standard Python library that I have used to help friend who wanted to get a basic CGI server up and running. It will serve python scripts from cgi-bin and files from the root folder. I'm not near a Windows computer at the moment to make sure that this still works. This also assumes Python2.x. Python 3.x has this, it's just not named the same.
Make a directory on your harddrive with a cgi-bin folder in it (Ex. "C:\server\cgi-bin")
In a command window, navigate to "C:\server" directory
Type the following assuming you've installed python 2.7 in C:\Python27:
"c:\python27\python.exe -m CGIHTTPServer"
You should get a message like "Serving HTTP on 0.0.0.0 port 8000"
Linux is the same - "python -m CGIHTTPServer" in a directory with a cgi-bin/ in it.
WSGI setups are fairly easy to get started, but in no anyway turn key. django MVC has a simple built in development server if you plan on using a more comprehensive framework.
My limited experience with Python web frameworks has taught me that most go to one extreme or the other: Django on one end is a full-stack MVC framework, that will do pretty much everything for you. On the other end, there are Flask, web.py, CherryPy, etc., which do much less, but stay out of your way.
CherryPy, for example, not only comes with no ORM, and doesn't require MVC, but it doesn't even have a templating engine. So unless you use it with something like Cheetah, you can't write what would look like .asp at all.

Categories

Resources