I am working on a Project where I am using React js for Front-End and Django for backend. I need to implement i18n Internationalization and Localization
I saw Django documentation and came across django I18n javascript_catalog.
How to use the same using getText() in React JS?. Is there any other way to implement?.
Thanks in Advance
Update for: django > 2.0:
from django.views.i18n import JavaScriptCatalog
urlpatterns = [
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]
Reference
Old:
Use below code in urls.py of project
from django.views.i18n import javascript_catalog
js_info_dict = {
'domain': 'djangojs',
'packages': ('name',)
}
urlpatterns += i18n_patterns(
url(r'^jsi18n/$', javascript_catalog, js_info_dict),
Add below line to your base html file
<script type="text/javascript" src="/jsi18n/"></script>
Related
I'm not a Django expert but I need to make customizations of this software which is written in Django using the rest framework.
I need to render a custom html template let's say a.html, however when I go to http://localhost:8080/custom/custom/a I get redirected back to different page - not a.html.
This is what I've done so far. I created a new folder in apps called custom:
cvat
apps
custom
templates
custom
_init.py
a.html, b.html, ...
urls.py
apps.py
from django.apps import AppConfig
class CustomConfig(AppConfig):
name = 'cvat.apps.custom'
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('custom/<str:name>', views.CustomView),
]
views.py
from django.shortcuts import render
def CustomView(request, name):
return render(request, 'custom/'+name+'.html')
In addition, I have modified these existing files to add the custom folder that was created in apps:
in urls.py added my url patterns:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('cvat.apps.engine.urls')),
path('django-rq/', include('django_rq.urls')),
path('custom/', include('cvat.apps.custom.urls')),
]
in base.py added this:
INSTALLED_APPS = [
...
'cvat.apps.custom'
]
Any advice on what I'm doing wrong?
You can try something like this
url can be like this
urlpatterns = [
path('custom/<name>/', views.CustomView),
]
and your view should be like this.
def CustomView(request, name):
return render(request, f'custom/{name}.html')
Hope this will help you.
The sitemap framework
Django comes with a high-level sitemap-generating framework to create sitemap XML files.
Overview
A sitemap is an XML file on your website that tells search-engine indexers how frequently your pages change and how “important” certain pages are in relation to other pages on your site. This information helps search engines index your site.
The Django sitemap framework automates the creation of this XML file by letting you express this information in Python code.
how use Django The sitemap framework for videos
don't work
from django.urls import path
from django.conf.urls import url
from . import views
from django.contrib.sitemaps.views import sitemap
from .sitemaps import PostSitemap
sitemaps = {
'posts': PostSitemap
}
app_name = 'blog'
urlpatterns = [
path('', views.home, name='homepage'),
path('search/', views.post_search, name='post_search'),
path('articles/<slug:post>/', views.post_single, name='post_single'),
path('videos', views.videos, name='videos'),
path('video/<slug:post>/', views.post_single, name='video_single'),
# path('category/<category>/', views.CatListView.as_view(), name='category'),
url(r'^a/(?P<hierarchy>.+)/$', views.show_category, name='category'),
# url(r'^(?P<slug>[\w-]+)/$', views.post_detail, name="detail"),
path('bodyOrgans/<bodyOrgans>/', views.bodyOrgans.as_view(), name='bodyOrgans'),
path('page/<page>/', views.page.as_view(), name='page'),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='sitemap'),
path('custom-sitemap.xml', views.index, {
'sitemaps': sitemaps,
'template_name': 'custom_sitemap.html'
}),
]
how make output like it
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>http://www.example.com/videos/some_video_landing_page.html</loc>
<video:video>
<video:thumbnail_loc>http://www.example.com/thumbs/123.jpg</video:thumbnail_loc>
<video:title>Grilling steaks for summer</video:title>
<video:description>Alkis shows you how to get perfectly done steaks every
time</video:description>
<video:content_loc>
http://streamserver.example.com/video123.mp4</video:content_loc>
<video:player_loc>
http://www.example.com/videoplayer.php?video=123</video:player_loc>
<video:duration>600</video:duration>
<video:expiration_date>2021-11-05T19:20:30+08:00</video:expiration_date>
<video:rating>4.2</video:rating>
<video:view_count>12345</video:view_count>
<video:publication_date>2007-11-05T19:20:30+08:00</video:publication_date>
<video:family_friendly>yes</video:family_friendly>
<video:restriction relationship="allow">IE GB US CA</video:restriction>
<video:price currency="EUR">1.99</video:price>
<video:requires_subscription>yes</video:requires_subscription>
<video:uploader
info="http://www.example.com/users/grillymcgrillerson">GrillyMcGrillerson
</video:uploader>
<video:live>no</video:live>
</video:video>
</url>
</urlset>
I am trying to browse the api documentation of rest framework swagger. but getting error AttributeError: 'AutoSchema' object has no attribute 'get_link
I have used django rest framework swagger. but not able to browse the documentation.
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API')
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('auth.urls')),
path('docs/', schema_view),
]
add below line in settings.py
REST_FRAMEWORK = {'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.coreapi.AutoSchema' }
djagno-rest-swagger is deprecated and suggests to use drf-yasg
use drf-yasg it is more useful than djagno-rest-swagger
When I add this to my seetings.py, works fine
REST_FRAMEWORK = {
...
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}
I'm new to Django and web coding.
I'm following Bucky tuts: Django Tutorial for Beginners - 28 - Creating a Base Template.
I thought of viewing the template page when requested ( PATH/templates/music/base.html ) but I don't know how to do it.
I searched to find that I could put something like this in urls to make it work :
path('base', 'django.views.generic.simple.direct_to_template', {'music/templates/music': 'base.html'}),
but didn't work.
How should I do it?
Did you try this code that is shown in the docs ?
from django.urls import path, url
from django.views.generic import TemplateView
# django 2.0
urlpatterns = [
path('about/', TemplateView.as_view(template_name="about.html")),
]
# django 1.11 and prior
urlpatterns = [
url(r'^about/$', TemplateView.as_view(template_name="about.html")),
]
I've implemented both django-cms and flatpages, but can not get tiny_mce to display in either.
urls.py
(r'^tinymce/', include('tinymce.urls')),
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
settings.py
TINYMCE_JS_URL = 'http://127.0.0.1:8000/site_media/js/tiny_mce/tiny_mce.js'
TINYMCE_JS_ROOT = 'http://127.0.0.1:8000/site_media/js/tinymce/'
TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,spellchecker,paste,searchreplace",
'theme': "advanced",
'cleanup_on_startup': True,
'custom_undo_redo_levels': 10,
}
TINYMCE_SPELLCHECKER = False
TINYMCE_COMPRESSOR = False
TINYMCE_FILEBROWSER = True
CMS_USE_TINYMCE = True
admin.py
from django.contrib.flatpages.models import FlatPage
from django.contrib.flatpages.admin import FlatPageAdmin
#Flatpages
class FlatPageAdmin(FlatPageAdmin):
class Media:
js = ('http://127.0.0.1:8000/js/tiny_mce/tiny_mce.js',
'http://127.0.0.1:8000/js/tiny_mce/textareas.js',)
# We have to unregister it, and then reregister
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, FlatPageAdmin)
#django-cms
from myprograms.cms.models import Page
class PageOptions(admin.ModelAdmin):
class Media:
js = ('http://127.0.0.1:8000/site_media/js/tiny_mce/tiny_mce.js',
'http://127.0.0.1:8000/site_media/js/tiny_mce/textareas.js')
#admin.site.register(Page, PageOptions)
In the base.html file
<script type="text/javascript" src="{{ MEDIA_URL }}js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="{% url tinymce-js "NAME" %}"></script>
There are so many different options when accessing the various user groups, docs, etc. I'm not sure what is the correct syntax. The CMS doesn't do me much good without some kind of text editor.
Thx
first of all please check this line with slash like:
<script type="text/javascript" src="{{ MEDIA_URL }}/js/tiny_mce/tiny_mce.js"></script>
also please check site_id in error logs. had similar issue with site_id because I created new site with different id.
Best,
Mykola Lys.
If you need some more features then the simple flatpages just checkout django-blocks (http://code.google.com/p/django-blocks/). Has multi-language Menu, Flatpages and even has a simple Shopping Cart!!
Have you read the TinyMCE page on the Django wiki? Also - although it looks like it might not apply to you - browsers block calls from scripts across differing servers/domains...