I'm doing a website for my school in Django. The blog part is almost ready, but on every page I want to have a sidebar with planned changes in the timetable (i.e. when a teacher is ill and classes will have lessons with another teachers in another classrooms). What would be the best way to implement that? I've heard about templatetags, RequestContext and other things like that. That is my first Django project and I'm learning new things every day.
Now I have one app in the project which is called blog (it's models are: Posts, Categories and Pages; Page is like a flatpage but it is assigned to a category by a ForeignKey). In project's urls.py every request (beside /admin) is forwarded to blog.urls where the further actions are taken (choosing the appropriate view class). Views are written using generic views and class inheritance. There are: PostListView, PostView, PageView. The PageListView is unneeded because user is able to choose Page from the main navbar.
The 'changes sidebar' should allow the user to choose a date and reload itself without reloading the whole page.
I would be really grateful if someone could help me with an idea on implementing it. If you have any questions - ask.
The 'changes sidebar' should allow the user to choose a date and
reload itself without reloading the whole page.
Ok, so you need AJAX for this part. For more complex projects you can use django-rest-framework or tastypie but for this I reccomend to do an AJAX function that gets a date as parameter and return a rendered template. See this thread and this one too
To include the sidebar, I'll just create a separate template for it and include {% include 'changes_sidebar.html' %} you also need to include the javascript to do the ajax requests.
Related
I am looking for ways to best handle a single form on multiple pages. Like how a newsletter signup might be on a home page, an about page, and several blog pages.
I've handled this exact scenario in these 2 ways.
Write an API (using DRF) that takes a POST request, and then point the normal HTML form at that API. This works, and is very flexible, but feels like overkill.
Pass the form object into the context of each view I want the form to be on, and then include that into the template with includes form_snippet with form=form
The first approach is more flexible with wagtail, wherein all that needs to happen on the admin side is an inclusion of the newsletter as a snippet, where the admin user is able to choose if they want the newsletter on the page or not.
While both approaches work just fine, neither of them "feels" right, as I would think there is a simple way to do this without creating a big API, or passing around the form object to every single view.
three years later on you probably found an answer.
For documentation purposes and Google searchers, Wagtail offers an explanation on how to make a multiple step form here:
https://docs.wagtail.org/en/v3.0.1/reference/contrib/forms/customisation.html#multi-step-form
I did this with a contact formular. I handled it with a new app for my contact formular.
In this contactApp is the templates/contactForm.html. To include this contactForm where I want I use {% include 'contact/contactForm.html' %} so it loads the contactForm.html from my app called contact.
Today I was just in the train and had some spare time to think about django cms and admin.
Can I define pages as models like this:
class ArticlePage(models.Model):
slug = models.TextField()
content = models.TextField()
online = models.BooleanField()
etc...
and then edit this model in django admin? Because, i think, django cms will also work,more or less, in this way, right?
will this be fine?
Where i am stuck is that i dont know how to set dynamic urls for those pages, because i need to touch urls.py for this, right?
Theoretically you can create a view which has a wildcard as the URL. And then have the view lookup the various actually used URLs in the ArticlePage.
def pageView(request, url):
page = ArticlePage.objects.get(slug=url)
...
return page.content
With the URLs specified as:
urlpatterns += patterns('articles.views',
(r'^(?P<url>.+?)/$', 'pageView'),
) #Catch all URLs not yet matched and attempt to find them in the database.
at the end of your urls.py.
And then you can use an HTML editor of some sort to create the actual content and whatnot.
So it is possible. However the question is do you want to?
It is possible to create a few of the pages on a website completely from scratch and storing the HTML in a database. Think small pages which are rarely updated but if updated change rigorously.
However, generally speaking a website has some structure. Something like blog posts, comments, polls, user registration and other interactive pages. Those pages cannot be described in a database field holding HTML.
Although if you do actually manage to do all that then I fear for your sanity because it must have been a painful and awkward road.
Hope this helps.
Update:
If you want to show a static HTML only page you normally just refer to them directly from the urls.py. Generally very few HTML is directly stored in databases. Most often you just store data in the database. If HTML is heavily being modified/saved/created/served from the CMS you just store it as an HTML file somewhere on the webserver.
Although one can certainly think of reasons to put HTML pages in the Database there is an equal many reasons as to why you shouldn't. It all comes down to the specifics of the problem.
E.g. if you allow a user to create a comment with links/boldface/italics etc. you can save the word or //word// in the db and parse it every time. Or you can parse it once and just store the HTML in the database so you don't have to parse it every time.
Same goes for larger pages although they generally have too much markup to be hand typed via the CMS every so often.
As for serving an HTML page directly via urls.py:
E.g.
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^foo/$', TemplateView.as_view(template_name='foo.html')),
)
Source: How do I go straight to template, in Django's urls.py?
I'm new to working with Django and am developing for a client who wants to be able to change page content in the Django Admin. They need to be able to change the html of the index page without editing the files on the server.
I know about flatfiles but I'm not sure that's completely what I'm after as I can't display stuff such as Django forms for example.
EDIT: Kind of like how a CMS works but without the users/group stuff and be able to use Django View modules such as forms.
Any advice?
Thanks
Honestly, the scope of what you're looking for is too huge to cover in this format. There's a number of ways this could be done, but they're all going to require some work and customization based on the client's needs.
Flatpages could work if you allow HTML content and make sure the content is rendered as "safe" in the template. This really only covers the "content" area of the site, though. It wouldn't be wise to use flatpages for an entire site template, including header, sidebar, footer, etc.
You could create editable areas. So, you actually create models for things like headers, sidebars, footers, and modules within those areas, and then just pull them into the template as needed. Then, the client is only editing pieces of the template instead of responsible for the whole HTML document.
Forms are going to be a challenge, because they require backend-processing that requires a connected view. The client won't be able to just arbitrarily drop in some form code and have a form. But, you could use a third-party service form forms and just embed them in the available content regions. Or, there's a couple of django apps that try to implement a type of "form builder" in the admin. That might somehow let the client add a form via something like the shortcodes used in Wordpress, but you'd likely have to lay down some infrastructure to make that work.
At a certain point, stuff like this reaches a point of diminishing returns, though. The only way to allow total customization of the template is to drop down into the actual physical file and make changes there. You can make certain things easier for the client, but ultimately, they either need to scale back their customization needs or deal with the fact that they'll have to work with the filesystem.
I don't believe that is possible at this time. Of course you can edit your models but templates, I think not.
I would find out how much they need to change? If they plan a complete redesign every week then you're still looking for an answer. If they just need a dynamic front page then you can split it up into variables and let them edit sections of html. Much safer and less prone to breaking the html.
I'm working on a Django project, and I've created some custom admin views using the get_urls override method described in the documentation. It works perfectly. There is just one problem. There is no way to get to this custom admin view unless you already know the URL.
There are some ways I already know of to add a link to this view somewhere in the admin, but none of them are satisfactory. I want a link to the custom view to appear in the model listings right with all the model admins. I just don't want it to have +add or +change links next to it because it isn't a model.
I could just override the admin_site or the template, but this is no good. It puts the customization on the project level instead of the app level. It also will only put the link on the /admin/ page and not the /admin/myapp/ page.
I could also just easily add the link in a different location by overriding the app_index.html template, but that is not exactly a convenient or intuitive place to look for it.
Another solution I came up with is to create a blank model and register a blank admin for it. Then steal the url patterns for that model so clicking on its entry goes to my custom view instead of to a blank add/change view. That works, but it's an incredibly ugly hack.
Here is a picture of what I'm trying to achieve.
I still think the correct way of doing this is overwriting some parts of django admin templates. There is no easy way of adding these links.
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template
I also found this article http://coffeeonthekeyboard.com/o-hai-django-adminplus-568/ which also suggests that django-adminplus is a good tool for doing this. Personally I prefer to keep clear of any extra dependancies and would still use templates - but thats up to you.
Have you tried this app: https://github.com/jsocol/django-adminplus? Even if it does not work for the exact purpose you are trying to achieve, at least it can give you some enlightement by checking out the code
You need to override the template admin/index.html. Thenput a new pair of tags after the {% endfor %} on line 40.
You might also be able to solve it using jQuery.
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.