I followed all 42 tutorials on the Try Django Youtube channel, and I'm trying to customize that project to make my own custom app.
My web app only needs two pages for now. One for users (which I only create in the admin) to log in, and another page to use the app. I'm trying to get the hang of the Django dev steps. Correct me if I'm wrong, so if I want to make a single page in a Django web app, I need to set it up in my urls.py, views.py, and make a template html file for it, right?
If I'm getting this order wrong or leaving anything out, any help or advice is appreciated. Can this pattern be found on Django documentation website, too?
Django based on MTV (model, view, template) pattern. More about it here http://aijogja.pythonblogs.com/251_aijogja/archive/1433_django_tutorial-create_a_blog-part_6__mvt_model_view_template_section_1-homepage.html
For adding another page, setup route in urls.py (write regexp), add function in views.py (must return httpresponse) and create template.html
Related
I'm using gunicorn and nginx to serve a flask application, my website has a blogging feature where users can write blogs, once they do, their input is saved as an html file (I use tinyMCE to generate it) and a flask view is added to a views.py file. Now, in deployment, I just used
use_reloader=True
This ensured that every time a new file was added, it was detected, now in production, I don't know how to implement it, gunicorn has --reload option but the docs say that it's only for development. Can someone please provide an insight on how to implement this feature? Thanks!
You don't need a new view for every html file. You could use a generic view that renders the appropriate html according to the url requested.
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.
So I have been following along with the Django Tutorial and have successfully created multiple "apps" that I now want to start integrating into a holistic website (which in Django seems to be called a project).
So here are my questions:
How do I create a site homepage that is mostly static data (HTML, CSS, and images), but also includes data from some of the models of my projects?
How do I link from this homepage to my apps? So if I have an app called "polls" (as in the demo), would linking to the polls page be as simple as linking to /polls?
I think the general approach is that you also have to add one app which glues all your other apps together. So if you need a special homepage which somehow has to have full or part access to all the other apps you create an app for it and point for example your root url to this app.
Following that (and depending if your other apps share data with each other or not) its really as simple as you said. The polls app could be accessible under /polls as an example, depending on how you configured it in your urlconf etc.
I want to benchmark the performance of a template website on a modified kernel. I want to use a website template that has 2-3 tiers (frontend, database etc), logic to create users and some logic to store/modify data for each user.
A quick search did not reveal any useful results as of yet.
I've little experience in web development and was hoping that stackoverflow can point me to something useful.
I would suggest taking a look at the Django framework:
http://docs.djangoproject.com/en/1.3/intro/
http://www.djangobook.com/en/2.0/
Django operates using a three tiered (Model, Template, View) design. The Model is the database access layer and will enable you to validate and store information about your users. The Template is the 'presentation layer' that will both determine the layout of your page through html, but has access to your view and its variables. The View is the portion that will contain all of the logic for the page - in a way it works as a median between your model and your template. The url your user visits will determine which view function you load.
If you are interested in the admin capabilities of the framework, take a look at:
http://www.djangobook.com/en/2.0/chapter06/
You could simply download and run one of the sample django applications like:
http://code.google.com/p/django-voting/
or
https://github.com/scrum8/django-job-board/
Or you could just create a clean django project and turn on the admin console.
I am using Lighttpd and Django. I have configured my Lighttpd server to pass all the requests ending with ".psp" extension to Django.
My startup page is a page served through Django, which is accessed as "http://192.168.1.198/home.psp". I want to enable the user to browse this page without writing "home.psp" explicitly in the url i.e. using "http://192.168.1.198"
Is this possible?
Thanks for any help in advance.
I think you're confusing concepts here between the "old" method of having individual files represent web pages which themselves contain code that is passed off to an interpreter before being sent in a response to how django/frameworks work.
If you're familiar with apache, imagine django as in part taking on the role of mod_rewrite. Django, and other frameworks, have what's called a dispatcher, or routing, mechanism.
Basically, they subscribe to the MVC pattern that says you should separate out the model, controller and view (in django parlance, model, template and view).
Now what then happens is you have a file called urls.py in django, which contains a list of routes (urls) and names of methods (usually contained in views.py) which handle them. Here's an example:
urlpatterns = patterns('',
url(r'^dologin$', 'testapp.views.auth_login', name="auth-login-uri"),
url(r'^doopenidlogin$', 'testapp.views.auth_openid_login', name="auth-openid-login-uri"),
url(r'^dologout$', 'testapp.views.auth_logout', name="auth-logout-uri"),
url(r'^login$', 'testapp.views.loginform', name="login-form"),
url(r'^openidlogin$', 'testapp.views.openidloginform', name="openid-login-form"),
url(r'^$', 'testapp.views.index', name="index"),
)
Here testapp is a python package, views.py is a python file and index is a django view. The url is constructed from regex, so I can have whatever I want as the url, much how stackoverflow urls are formed.
So basically, you never need file extensions again. I'd strongly suggest getting a good book on django - there are a few around.
What you might be looking for is the index-file.names directive in Lighty's configuration file. Just add "home.psp" to the list in your configuration file, and Lighty will look for it when no filename is specified.
I solved the problem myself. Here is what I did:
I had to add a statement inside url.rewrite-once block of lighttpd's configuration file like:
url.rewrite-once = (
"^(/media.*)$" => "$1",
"^(/static.*)$" => "$1",
"^/favicon\.ico$" => "/media/favicon.ico",
"^(/)$" => "/my_project_dir/home.psp",
"^(/.*)$" => "/my_project_dir$1",
)
Apart from this, I added the following line in my urls.py:
(r'^$',my_index_view_name),
Hope this helps someone in the future. Thanks everybody for your replies above. Cheers!