Django single website structure - python

I want to start a new django project. This will just be a relatively simple website to test django itself. So the way all tutorials structure their project is this:
mysite
mysite
some_app
...
For me it's not very clear what does what. I don't really need any app right now. Could I implement my website without using any app? Should I use an app? What would it be called for a simple website?

Have you read Django doc already?
They have a nice tutorial on how to get started.
As for apps, in Django, you have a project containing one or more app. If you just want to try to build a simple website, your project called mysite will contain only one app.

I think you don't need more than the main mysite, BUT, at the django documents, have a session that says:
If your background is in plain old PHP (with no use of modern
frameworks), you’re probably used to putting code under the Web
server’s document root (in a place such as /var/www). With Django, you
don’t do that. It’s not a good idea to put any of this Python code
within your Web server’s document root, because it risks the
possibility that people may be able to view your code over the Web.
That’s not good for security.
Put your code in some directory outside of the document root, such as
/home/mycode.
This model may have been designed with focus on safety.
Some files in mysite could be used to control things in another apps, so i think that is a 'best pratice' to follow this model.
Font

if u aren't in need of an app you can avoid creating one. just give your view in views.py of your project set the url and u r all set to go.
here the first mysite folder is just an usual folder u can change the name in ur convenience next mysite is your project folder and recommended not to change it and next some_app is the application name(you can name your app anything relevant but in proper convention.)
and follow django documentation and djangogirls tutorial for better understanding.
https://docs.djangoproject.com/en/2.0/
https://tutorial.djangogirls.org/en/

Related

Can I Run Multiple Django Projects on One Website?

i'm a bit of a newcomer to Django/Python but trying to figure something out--hope you can help.
I'm trying to create a personal website with a page dedicated to some of the Python and Django projects that I've completed through several different online courses.
I'm just having a tough time figuring out the best way to link through and assemble the project directories on my server.
My first project is just the files for my blog itself. I've created a new directory in the same home root as the blog project housing another of my django projects. Just looking for a bit of assistance on how i link out to the second project from my blog.
I thought i could use the urls.py file for my blog to redirect link to the second project (ie projects/project2) utilizing a views definition from the views.py file for one of the apps in the blog. But then--i'm getting tripped up on how to render that link to the second project.
Any forward guidance is greatly appreciated.
In general, anything that makes sense to be served under different domain (not necessarily subdomain) would better be a separate project for sure. Exceptional case would be a project with almost same functionality but different branding within same organization. In such case, Django's built-in sites framework can be considered.
For different projects, you need different project roots along with different wsgi/port bindings and processes. They can still be listed within a single nginx configuration as a deployment example. Another popular way is using Docker but deployment methods vary.
For different apps in a single project, there is only one binding and one root already. Create apps and list them in your settings.py and urls.py. If you need subdomains with single deployment, tkaemming/django-subdomains might be helpful.
For different policies (rarely) in a single app under different domains, learn about sites framework. You need to hand code the difference in views.
and so on...

Templates on Django

I'm newbie on web dev and chose Django to start, in my application I need sign up and sign in freatures, searching i've found about django-registration:
Link to repo
The setting file and url are already configured, but I have to make the templates for login - I got some templates for test - but I have no idea where to put it, if I have to create a new app ("... startnewapp registration") or just create a directory for templates somewhere.
Can you help me?
you dont really need an external app for just a registration. it is simply one urlconf and one view.
but the most important thing for you now is to go through the tutorial, because tutorial tells you what to put where exactly.. and this cannot be explained here in 3 lines of text
Especially at the beginning it is very helpful to see an example where to put the things together. I developed a django-skeleton, which basically bootstraps a Django installation and boosts starting a new project. I also created views to use the builtin authentication module of Django for registration and to login: https://github.com/n2o/django-skeleton/
In this case I created a separate login app to modify the views and create my own templates.
This is not the easiest way to modify existing templates, but it fulfills all of my requirements.

Django apps structure

I'm learning Django but it's difficult to me to see how should I divide a project into apps?
I've worked on some Java EE systems, almost for procedures for government and all that stuff but I just can't see how to create a Django project for this purposes?
For example, if you should have to do a web app for making easier three process: Procedure to get the passport, procedure to get the driver license and procedure to get the social number.
The 3 procedures have steps in common: Personal information, Contact Information, Health Information. Would you do a project for each procedure, an app for each procedure, an app for each step?
I'm sorry if I'm posting this in the wrong Stack Exchange site.
Thank you.
When you say "procedures" i guess you're talking about pages (or views in Django). So I would implement a single "app" to do that.
Remember a project is composed of apps. When you create a project a main app (with the same name of the project) is created. This is a good place to code the procedures you said.
Think of apps as indepent sections of your project (site); maybe some forum, a blog, a custom administration panel, a game and stuff like that. Every one of them could be an independent app.
A project is mostly intended as a single website, so there's no need to create another project on the example you mentioned.

Huge Django project

I have a new job and a huge django project (15 apps, more than 30 loc). It's pretty hard to understand it's architecture from scratch. Are there any techniques to simplify my work in the beginning? sometimes it's even hard to understand where to find a form or a view that I need... thnx in advance.
When I come to this kind of problem I open up a notebook and answer the following:
1. Infrastructure
Server configuration, OS etc
Check out the database type (mysql, postgres, nosql)
External APIS (e.g Facebook Connect)
2. Backend
Write a simple description
Write its input/output from user (try to be thorough; which fields are required and which aren't)
Write its FK and its relation to any other apps (and why)
List down each plugin the app is using. And for what purpose. For example in rails I'd write: 'gem will_paginate - To display guestbook app results on several pages'
3. Frontend
Check out the JS framework
Check the main stylesheet files (for the template)
The main html/haml (etc) files for creating a new template based page.
When you are done doing that. I think you are much more prepared and able go deeper developing/debugging the app. Good luck.
Use this http://packages.python.org/django-extensions/graph_models.html
to generate the Relationship diagrams from the models so that you can visually see how the models are related to each other. This will give you nice idea about the app
1) Try to install the site from scratch. You will find what external apps are needed for the site to run.
2) Reverse engineer. Browse through the site and try to find out what you have to do to change something to that page. Start with the url, look up in urls.py, read the view, check the model. Are there any hints to other processes?
3) Try to write down everything you don't understand, and document the answers for future reference.
I would clone the project so you can mess up endlessly.
Then I would start to reduce the code. "What happens if if just remove this function here?
Also get django debug toolbar:
https://github.com/django-debug-toolbar/django-debug-toolbar
A good terminal debugger is also golden, there are many out there, here is an example:
https://github.com/tomchristie/django-pdb
This allow you to halt the code and even inject and mutate parameters in runtime. Just like GDB in C.
If you use FireFox you can install FireBug on it and when you for example submit ajax form you can see at which url send you request after what you can easily find controller which work with this form data. At chrome this utility embedded by default and call by F12 key.

What exactly are Django Apps

I want to switch from Rails to Django, to broaden my mind, and a question has bobbed up in my mind.
My Rails app is quite a mess, since my hobby-based development approach is a patch-and-glue one. I have seen very early that Django divied between a project and an app. According to their site, a project is made of many apps, and one app can be used for many projects.
This intrigued me, since that would made the lines between my site's areas clearer. I tried to find some more examples and information on that, but I couldn't answer my question, which is:
How big/small is such an app? Are they able/supposed to interact closely?
It is, for example smart to have one app to deal with user's profiles, and another app to deal with blog-posts and comments, from those users? (In my site, a user can have several blogs, with different profiles). Or are they meant to be used otherwise?
A django App is a fancy name for a python package. Really, that's it. The only thing that would distinguish a django app from other python packages is that it makes sense for it to appear in the INSTALLED_APPS list in settings.py, because it contains things like templates, models, or other features that can be auto-discovered by other django features.
A good django app will do just one thing, do it well, and not be tightly coupled to any other app that might use it. A wide variety of apps are provided with django in the contrib namespace that follow this convention.
In your example, a good way to devise apps is to have one for user profiles (or use one of the many existing profile apps), one app for blog posts (or one of the many that already do this), one app for comments, separate from blog posts (again, you can use an existing app for this), and finally, a very tiny app that ties the three together, since they don't and shouldn't depend on each other directly.
The purpose of using app's is to make them reusable. Django likes DRY principle DRY stands for DO NOT repeat yourself
An app should be as small as it can, and loosely coupled. So, for an app should not need another app to work properly.
Django recommends writing an app for each table (well, not always, but as soon as you want to grow your app, you will definitely need to divide tables to pair apps. Or else you will have hard time for maintaining your code.)
You can, for example, create an app for users, an app for sales, an app for comments, an app for articles. It will be easier to maintain your code and if you have done it right, you can use the app in other project with a little (if any) modification in the app.
Project's are, compilation of app's. Users app, articles app, comments app can come together an make a project, or in other words, a website.
If you want to learn django, I suggest you to check out:
http://www.djangobook.com/
http://docs.djangoproject.com/
One word of advice, do not, in any case, copy/paste. Not only your code has great chance to fail, but you will not know what the code is doing. If you are going to use someone elses code in your project, at least type them, this will make you understand what the code is doing, or at least, it will give an idea.
Writing your own code is always better for maintance, but this does not mean that you should reinvent the world, you can use libraries, look at their documentation to use it properly.
Documentations, tutorials are you best friend.
Good luck.
A project is basically a place where your project lives...in your project you setup your url's, project settings, etc.
An app defines its own data models and views to be used within a project. You can move these between projects if you like.
I highly recommend running through the tutorials from the Django site as they will show you what a project and app are, how to manage both, how to use the admin panel, how to make the app usable in multiple projects, etc.
A portal = A django project
A ads system, gallery photos, catalog of products = Apps

Categories