Change third-party application name in Django - python

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',
# ...
]

Related

How to add page templates to Wagtail after installing into an existing Django application

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.

Mezzanine custom field adding to admin

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.

How to work with settings in Django

I want to keep some global settings for my project in Django. I need to have access to these settings from the code. For example, I need to set a current theme for my site that I can set using admin console or from the code. Or I need to set a tagline that will show in the header of all pages. I suppose I should use models for keeping the settings but I can't realize how I should better do it.
There are quite some packages that store settings in models, pick the one that works best for you:
http://pypi.python.org/pypi?:action=search&term=django+settings&submit=search
If you are okay with changing these setting programmatically via settings.py you should do that. However, if you want to change these settings via the Admin Console, you should use models.

Use only some parts of Django?

I like Django, but for a particular application I would like to use only parts of it, but I'm not familiar enough with how Django works on the inside, so maybe someone can point me into the right direction as to what I have to check out.
Specifically, I want to use:
The models and database abstraction
The caching API, although I want to avoid database lookups by caching, not HTML generation, and since the caching framework in Django is intended for the latter, I'm not sure yet whether that's really appropriate.
I would not use:
Templating
urlconfigs
Or, more exactly, I'm neither using HTTP nor HTML. So basically, I have a different input / output chain than usual.
Can this work?
My personal killer feature in Django is the Object / database mapping that I can do with the models, so if there's another technology (doesn't have to be Python, I'm in the design phase and I'm pretty agnostic about languages and platforms) that gives me the same abilities, that would be great, too.
I myself use Django for its object/db mapping without using its urlconfigs. Simply create a file called djangosettings.py and insert the necessary configuration, for example:
DATABASE_ENGINE = 'oracle'
DATABASE_HOST = 'localhost'
DATABASE_NAME = 'ORCL'
DATABASE_USER = 'scott'
DATABASE_PASSWORD = 'tiger'
Then in your regular Python code, do
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "djangosettings"
before you import any Django modules. This will let you use Django's object/db mappings without actually having a Django project, so you can use it for standalone scripts or other web applications or whatever you want.
As for caching, if you don't want to use Django then you should probably decide what you are using and go from there. I recommend using CherryPy, which doesn't use Django-style regular expression URL mapping, but instead automatically maps URLs to functions based on the function names. There's an example right at the top of the CherryPy home page: http://cherrypy.org/
CherryPy has its own caching system, so you can accomplish exactly the same thing as what Django does but without needing to use Django's urlconfig system.
Django, being a web framework, is extremely efficient at creating websites. However, it's also equally well-suited to tackling problems off the web. This is the loose coupling that the project prides itself on. Nothing stops you from installing a complete version of Django, and just using what you need. As a rule, very few components of Django make broad assumptions about their usage.
Specifically:
Django models don't know anything
about HTML or HTTP.
Templates don't know anything about HTML or HTTP.
The cache
system can be used to store
anything that can be pickled.
One of the main things you'll face when trying to use Django without a web server is setting up the environment properly. The ORM and cache system still need to be configured in settings.py. There are docs on using django without a settings module that you may find useful.
Django ORM Standalone
I've created a template Django project that allows you to do just that.
https://github.com/dancaron/Django-ORM
Just follow the instructions and you can write standalone python files that utilize Django's database functionality, without having to use urlconf, views, etc.
I tend to prefer a mix-and-match approach to using Python for web programming. :-)
I don't have a lot of experience with Django, but I'd recommend giving sqlalchemy a look for the database stuff. It works well with others and gives you several potential layers of abstraction (so you can go with something basic or tweak the hell out of it if you want). Plus, you'll already be somewhat familiar with it if you've ever used hibernate/nhibernate.
My favorite part is that it has a lot of options for databases to connect to (most notably SQL Server, which django doesn't have built in last time I checked).
With that said, I'm told that with Django, it's pretty easy to decouple functionality (but never done so myself).
There are of course other projects out there that specifically implement single parts of django. TurboGears for example is a collection of several projects that can work by themselves and together form a complete web development framework.
For the db abstraction SQLAlchemy comes to mind.
Regarding the caching part: I'm not aware of any standalone project that implements a generic caching facility.
On the other hand, it should be fairly easy to implement your own caching, for example by using pickles. Have a look at this recipe for a decorator for ideas and google for "memoize".
Also keep in mind that your database has its own caching mechanism, so maybe you don't even need to concern yourself with the details.
I've shared an example of solution, which prevents Python Path manipulation inside code:
https://github.com/askalyuk/django-orm-standalone
It contains a standalone data access package, a separated simple Django site and a unit test.
I found KeyboardInterrupt's answer but it was answered in 2009 and I failed to run it in Django 1.8.For recent Django 1.8, You can have a look at this, in which some parts come from KeyboardInterrupt's answer.
The folder structure is:
.
├── myApp
│   ├── __init__.py
│   └── models.py
└── my_manage.py
myApp is a module, contains an empty __init__.py and models.py.
There is an example model class in models.py:
from django.db import models
class MyModel(models.Model):
field = models.CharField(max_length=255)
my_manage.py contains django database, installed_app settings and acts as django offical manage.py, so you can:
python my_manage.py sql myApp
python my_manage.py migrate
......
The codes in my_manage.py are:
from django.conf import settings
db_conf = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database_name',
'USER': 'your_user_name',
'PASSWORD': 'your_password',
'HOST': 'your_mysql_server_host',
'PORT': 'your_mysql_server_port',
}
}
settings.configure(
DATABASES = db_conf,
INSTALLED_APPS = ( "myApp", )
)
# Calling django.setup() is required for “standalone” Django u usage
# https://docs.djangoproject.com/en/1.8/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
import django
django.setup()
if __name__ == '__main__':
import sys
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

Replace textarea with rich text editor in Django Admin?

I would like to know the best way to replace a standard textarea field with a rich text editor in Django Admin?
There's an add-on Django application to provide TinyMCE support for Django admin forms without having to muck around with admin templates or Django newform internals.
Take a look on this snippet - basic idea is to include custom JS in your admin definitions which will replace standard text areas with rich-text editor.
For jQuery/FCKEditor such JS could look like that:
$(document).ready(function() {
$("textarea").each(function(n, obj) {
fck = new FCKeditor(obj.id) ;
fck.BasePath = "/admin-media/fckeditor/" ;
fck.ReplaceTextarea() ;
});
});
I'd say: define your own ModelAdmin class and overwrite the widget used for particular field, like:
class ArticleAdminModelForm(forms.ModelForm):
description = forms.CharField(widget=widgets.AdminWYMEditor)
class Meta:
model = models.Article
(AdminWYMEditor is a forms.Textarea subclass that adds WYMEditor with configuration specific to Django admin app).
See this blog post by Jannis Leidel to see how this widget can be implemented.
At the date of the post and the answers TinyMCE was quite popular (as it probably remains today).
But after some time ckeditor has appeared and many consider that a better alternative, including many SO users:
Compare TinyMCE and CKeditor for a Wiki
http://www.turnkeylinux.org/blog/tinymce-vs-ckeditor
There is also a 2013 review of WISIWYG editors with Django in Russian:
http://habrahabr.ru/company/htdt/blog/202782/
Currently the most straight forward way to use tinymce in django admin is to use Grappelli.
http://code.google.com/p/django-grappelli/
Grappelli is also a requirement for django-filebrowser so if you want the whole shebang you will gotta need it anyways.
class KindEditor(forms.Textarea):
class Media:
css ={
'all':(settings.STATIC_ROOT + 'editor/themes/default/default.css',)
}
js = (settings.STATIC_ROOT + 'editor/kindeditor-min.js',settings.STATIC_ROOT + 'editor/lang/zh_CN.js',)
def __init__(self):
attrs = {}
attrs['rel'] = 'kind'
super(KindEditor, self).__init__(attrs)
class NewsAdminForm(forms.ModelForm):
pass
class Meta:
model = News
widgets = {
'body':KindEditor()
}
class NewsAdmin(admin.ModelAdmin):
form = NewsAdminForm
admin.site.register(News, NewsAdmin)
Ok, to update a little this post, I would say that the easiest way to implement TinyMCE is to use the django-tinymce app. You must also download the JS files from the TinyMCE page. I got some errors with the django intenationalization, but downloading the laguage packs from the TinyMCE must be enough.
Install this package
pip install django-ckeditor
then run these commands to migrate.
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic
finally restart your Django server.
Once you complete the above steps, you can see the rich text editor in your admin panel fields.

Categories

Resources