New to Django here, please bear with me if this question seems silly.
I'm in the process of designing (and afterwards developing) a blog website. I've identified three types of page that I will have on my blog (article, home, misc) each of these having a separate template.
Every new article would have its own text and images. Presumably I'd be using a single .html template to display many article texts.
Do I store the article texts and images in a separate model? If so, do I store the html of the article text (with all its formatting) in plain text and then render it in the view? What's the best way to do this?
Alternatively, I'd create a new template for every article, but this seems redundant.
You can use django-tinymce. It also provides good editor in your django admin.
Just install it with pip:
pip install django-tinymce
Add it in your INSTALLED_APPS:
INSTALLED_APPS = (
...
'tinymce',
)
Add tinymce.urls to urls.py for your project:
urlpatterns = [
...
url(r'^tinymce/', include('tinymce.urls')),
]
In your code:
from django.db import models
from tinymce.models import HTMLField
class MyModel(models.Model):
...
content = HTMLField()
I showed you the brief installation from documentation
Related
my client want me to build an article post website in which the user come and login and write the articles.in article he must put tags and with those tags the other users can search. the problem is i do not know how to put tags in views. and also in html .enter image description here
and my views file is:
enter image description here
can any body please help me with sample coding
There is a simple third party app that can help you with tagging. It's called django-taggit and you can find how to easily set it up it in its Documentation page or by following this tutorial video.
Basically, you need to install it with pip install django_taggit, add it to your INSTALLED_APPS and add the manager to the desired model, for example:
from taggit.managers import TaggableManager
class Post(models.Model):
# ...
tags = TaggableManager()
Then just create a migration for your model changes with python manage.py makemigrations post followed by python manage.py migrate.
Now you can add, remove or list the tags with simple commands:
post.tags.add('tag1', 'tag2', 'tag3') # add tags
post.tags.remove('tag1') # remove tags
post.tags.all() # list tags
I'm trying to extend an already existing Django App. The app is functioning fine as is, but I would like to add blog functionality.
I've installed Wagtail, using the guidelines here (http://docs.wagtail.io/en/latest/getting_started/integrating_into_django.html) To check wagtail is installed, I have navigated to here:
http://myurl/cms
And the wagtail admin panel is displayed. When I navigate to http://myurl/admin I get the default admin control panel for my Django app, so far so good.
Now I am trying to build the blog.
I found this tutorial:
http://wiseodd.github.io/techblog/2015/06/22/developing-wagtail/
which suggests the following as a first step: -
First, we’ll create our generic page class as home page class is
already created by default when we started Wagtail project.
It then displays this code:
# core/models.py
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailsearch import index
# We’re deriving our GenericPage from Page class, so that our GenericPage also has Page’s field, e.g. title
class GenericPage(Page):
# Let’s create our custom field, named body which is a rich text
body = RichTextField()
# Index the body field, so that it will be searchable
search_fields = Page.search_fields + (index.SearchField(‘body'),) # To show our body field in admin panel, we have to wrap it with FieldPanel and add it to Page’s field panel content_panels = Page.content_panels + [FieldPanel('body', classname=‘full’)]
I could not find which file I was meant to add this into. I searched the system using grep, and found a number of files that had the text string:
from wagtail.wagtailcore.models import Page
I decided the most likely candidate was in the directory:
env/lib/python2.7/site-packages/wagtail/project_template
Within my original app directory. I added the code above to the models.py file residing in the above directory. I then ran
python manage.py makemigrations
But it said no migrations were found. The next step in the tutorial posted above suggests you should now see three different page types available to create in the control panel, but I can not find the option to create any pages.
Can you tell me if I edited the correct file above, or if I should have edited a different file, and also
Why I am not seeing any option to add a new page in the wagtail control panel?
I have consulted with the documentation here (http://docs.wagtail.io/en/latest/getting_started/tutorial.html) and tried following the 'extend the homepage model' section, but couldn't figure out where the home/models.py file is as there is no folder called home in my Django app.
Thanks for any advice
As the final section of the "integrating into Django" docs says:
You’re now ready to add a new app to your Django project (via ./manage.py startapp - remember to add it to INSTALLED_APPS) and set up page models
Running ./manage.py startapp blog will add a blog app to your project, including an empty models.py - this is where you add your page definitions. (The Wagtail docs don't go into detail on this, because it's just following the standard Django workflow, which is hopefully familiar to anyone with an existing Django project to integrate with...)
Tutorials that use wagtail start my_project as a starting point will omit this step, because the starter project comes with a pre-made models.py with a HomePage model. The site-packages/wagtail/project_template directory you found is actually the 'master' copy of the starter project, which gets cloned at the point that you run wagtail start my_project. Since this isn't hooked up to your current project, changing it had no effect.
I use third-party application (django-flatblocks) in my Django project. How can i change the name of the application in admin interface (grappelli)? I need russian name. I already tried to create localization files (.po.mo) and it is work fine in some parts of admin interface, but in breadcrumbs do not. I think grappelli does not use translation in some templates and the best way to resolve the problem is to replace django-flatblocks verbose_name in apps.py "on the fly". Can i do it somehow?
Found this in the official documentation did. The solution is so simple: For application users
To quote the Manual:
If you’re using “Rock ’n’ roll” in a project called anthology, but you
want it to show up as “Jazz Manouche” instead, you can provide your
own configuration:
# anthology/apps.py
from rock_n_roll.apps import RockNRollConfig
class JazzManoucheConfig(RockNRollConfig):
verbose_name = "Jazz Manouche"
# anthology/settings.py
INSTALLED_APPS = [
'anthology.apps.JazzManoucheConfig',
# ...
]
How would I add a rich-text HTML content editor in my Django admin view?
For example, if I want to change the content on my homepage, what python code would I have to input for the HTML to be displayed when I log into the admin portal?
I want to be able to view all of my pages (hopefully even be able to add/delete them), and edit the content directly from the admin view. Similar to something like this:
http://feincms-django-cms.readthedocs.org/en/latest/_images/item_editor_content.png
I appreciate any and all help! Thank you!
The html shown there is there cuz it is in the database. If what you want is a cms, there are several of those for django like django-cms, wagtail, mezzanine among others.
If, on the other hand, you have a model already and want to display it in the admin, you can register it:
from django.contrib import admin
from myproject.myapp.models import Author
class AuthorAdmin(admin.ModelAdmin):
pass
admin.site.register(Author, AuthorAdmin)
In that example, the Author model will show up in admin for edition.
In the last case, where you already have your model registered but you want to have a WYSIWYG editor, check the django-grid for that kind of package and find one suitable, or install one manually.
You have several options in case you want to try the second approach: ckeditor, tinymce, etc.
I am writing a piece of software and have added an extra field using mezzanines model injection ability's the field is there and can be seen in the database but following the method shown in the docs on this page mezzanine model customisation I cannot get it to show up in my admin page bellow is the code I have written that I believe should work to de-register and then re-register with the new field added to the admin. I am unsure if this code is even running it is in the root of my project as I believe that is where it should be and named admin.py as I believe it should be and have used that file in other projects.
from copy import deepcopy
from django.contrib import admin
from catridge.shop.admin import ProductAdmin
from catridge.shop.models import Product
product_fieldsets = deepcopy(ProductAdmin.fieldsets)
product_fieldsets[0][1]["fields"].insert(-2, "download_file")
class MyProductAdmin(ProductAdmin):
fieldsets = product_fieldsets
admin.site.unregister(Product)
admin.site.register(Product, MyProductAdmin)
Does anyone have any idea as to how this might work I am a bit stumped currently but this is mainly a mezzanine function.
The admin.py file needs to go inside one of your INSTALLED_APPS directories.