So in the Django tutorials we make a sparse polls application that shows off some of what Django can do, but leaves a lot to be desired when it comes to learning Django (e.g. using their UserCreationForm to make a user portal).
In part of the tutorial they talk about how the admin should be publishing content (e.g. if it were a blog or newspaper) and we set up the admin site where one can make new questions for the polls.
In regard to the blog idea - since an article would be lengthy most likely - I think the correct model would include models.TextField. However, looking at Django's naturally generated admin site for adding / modifying new models with a TextField leaves a lot to be desired.
What if there should be images embedded among the text? or what if there should be formatted text? The admin site does not support a user friendly way to do this.
My question is how to produce a user friendly way for making mixed media e.g. a Stack Exchange post which might have images, code formatting, text formats, etc.
You could use Django Pagedown which aims exactly to offer a way of editing similar to that found on stackexchange sites. As for now you cannot yet upload images (this feature is though on the todos list of the author), they must be already uploaded somewhere on the web and you can insert them using their url.
Related
This is not a HOWTO question, as it, as such, has been answered before here
I am trying to integrate Django with modern frontend framework, and I found that it is possible to store and render Django templates from models. Since it is not a standard, I am wondering what are the advantages (or disadvantages if that's the case) of file based templates.
Reading though the documentation, I have seen that it is recommended to actually cache templates and models as much as possible for best performance, so why would it not be recommended to store templates in the database? It seems very convenient to me that in doing so pages can be edited from the admin panel (where you can add a code editor), which, along with the rest framework and a front end framework synergize very well.
From my research, the template tags and template language seem to work and the context can be passed in a view as well. the only thing I cannot figure out is the {include .. } tag, as it does not seem to point to a view. (although a custom tag cam be made to have this function)
Can such a setup be used in production? or are there security/performance/other concerns?
It's not recommended because templates are the developer's responsibility, not the site admin's.
Otherwise, there's not much performance difference between reading files directly from filesystem and from database.
One drawback of this approach is you don't get revision history (using git, mercurial etc). Sure, you can implement something similar to save the revisions in the database, but then why not use the better tools which already exist?
There's a Django flatpages app which lets you save HTML content in database. But the purpose of the app is to allow site admins to edit HTML content, such as information for an "About Us" page, because writing this info is not a developer's responsibility.
I am fairly new to Django, having done the tutorial front to back and back to front with a website I am creating myself.
However, there are - currently - about two question that stop me from progressing. As background: I'm an enterprise java developer so I'm familiar with database design, foreign keys and the importance of entity/model scoping. Also, I created websites with php and mysql and was therefore familiar with including header/footers/menus etc. Switching to Django is difficult partially because of this, since Django uses template inheritance to 'include' pages, which is quite a bit different from including each footer/header/menu in each page.
For these questions, imagine someone who likes to recreate the youtube website with Django (just hypothetical! - who would want to do this anyway?)
How would one define the app scope? I read on multiple sources (among which the django website) that an app should be really small, in our example perhaps: comment app, video upload app, suggested videos app etc. However - in my view - comments should have a foreign key to a video and a foreign key to a user. If one would design a comment app, how would one solve those foreign key contraints/dependencies since they would need to point to a different app? Or perhaps, how SHOULD one scope the apps?
Related to question 1, about templates... Designing views and templates for a single app is not really a problem (yet). However how does one bring multiple apps together. For example, one could create a 'home' app that holds the menu, header, footer, login and such. But that means that other apps - like comments - need to extend that view creating yet another dependency to the home app. How would one do this in django?
Note that I'm not looking for full code - although code snippets might help explain the concepts - but these questions are more theoretical.
You're kind of over-thinking this. An app is really just a convenient grouping of code. There's nothing that stops apps having a foreign key to a model in another app: indeed, that's more or less required, especially in the case of linking to users since they are provided by the contrib.auth app.
As regards templates, this isn't specifically to do with apps, but with any template that needs to gather data from multiple places. The usual way to do this is by using custom template tags - probably inclusion tags - to query and render the data.
I have an app that allows users (admins actually) to add html to a model. Then I serve that html on some page to other users (nonadmins). I would like to allow the admins to create arbitrary html on these pages, including adding images. I don't want the admins to have to jump through hoops to get their content into this html field. Suppose a user has some images on their local machine that they want to go into this html field they are creating. I want it to be super brain-dead easy for them to get those images in there.
Right now I just have a model with an html field and I provide a WYSIWYG editor . On a page that users can see, I just load that model.html (filter it as safe) and display. But if the admin user wants to add an image, they still have to figure out hosting and linking in their html document.
Is there a way to use Django flatpages + static to achieve this? Or some kind of app that provides a wordpress-like editor inside Django?
Honestly I would recommend just installing Mezzanine. It does exactly what you want and is the most lightweight, simple and Wordpress like of the Django CMSs. It integrates TinyMCE and Django filebrowser like you want and you can throw away the bits you don't want. This is almost definitely the quickest way to do what you want.
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 have a Django website in which I want site administrators to be able to edit rich content.
Suppose we're talking about an organizational info page, which might include some pictures, and some links, where the page is not as structured as a news page (which updates with news pieces every few days), but still needs the ability to be easily edited by site admins which do not necessarily want to mess with HTML (or rather, I do not want them to).
So where do I put this dynamic content? On the database? In which format? How do I make it accesible in the django default admin?
Use one of the existing rich-text editors
The lightest weight would be to use something at the js level like DojoEditor:
http://code.djangoproject.com/wiki/AddDojoEditor
See also this thread:
Replace textarea with rich text editor in Django Admin?
For what you're describing I'd use flatpages, which is a django app that lets users create and edit pages in the admin panel.
As for formatting, I'd use TinyMCE. Integrating it is pretty easy, here is a walkthrough (do steps 1 and 2 and jump to the bottom, "Using TinyMCE with flatpages (newforms)")