django-allauth configuration for multilangiage website - python

I'm using django-allauth 0.17.0 with Django 1.5.6 and I have a question for multilanguage setup. I will use only two languages, english and chinese. Did someone use it for that kind of setup? What is the best and fastest solution to do that?
I was planning to use something like this in urls:
(r'^(?P\w{2})/accounts/', include('allauth.urls')),
so I can grab "activeLanguage" and prepare templates for each language.
Thanks!

I don't think you need custom setup to make it work with django allauth.
In a project of mine, I have in my urls.py:
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls import include, url, patterns
urlpatterns = i18n_patterns('',
url(r'^', include('allauth.urls')),
)
It works as expected (e.g. you can access login at /en/login).
If you want to translate the templates, you should probably not prepare a template for each language.
Instead, you can override the provided allauth templates. For the login template, just create a templates/account/login.html file in any of your project app.
Then you can write your template using django's translation mechanisms.

Related

How to serve django and gatsby on single vhost?

I'm trying to deploy gatsby based frontend with django based backend under a single domain. It will rely on Apache and mod_wsgi. In a perfect world it should work as following:
https://my-domain.com/ - gatsby frontend
https://my-domain.com/admin - django
https://my-domain.com/api - django
I can see two possibilities:
Django is aware of frontend. Serve everything via Django, setup / as a STATIC_URL.
Django is not aware of frontend. Serve /api and /admin via django. / is handled by a webserver.
I feel more comfortable with the second approach, however I do not know how to configure VirtualHost for such a scenario. The firstch approach looks like an ugly hack.
How should I proceed with that?
After compiling your gatsby project, it should be served by django as a static page.
First: The gatsby dist should be in your static_private path.
Second: In your django project, you will define a URL for / that will call an index view let's say.
Finally: in your view you should render index.html of your gatsby dist.
urls.py:
from django.contrib import admin
from django.urls import path, re_path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('apis/', include('apps.urls')),
path('/', views.index),
]
views.py:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Note that if you are handling routing in your frontend your url pattern for the index view should be like this : re_path('^.*$', views.index)
If you are hosting your django app on heroku you will need the whitenoise middleware and set it up in your settings.py :
MIDDLEWARE = [
...
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
A doc is available here: https://devcenter.heroku.com/articles/django-assets#whitenoise

How to override a Django url path that is provided by a library?

I'm currently building out an authentication server using Django, djangorestframework, and django-rest-auth. My problem is actually pretty simple I think, but I haven't really been able to find any resources on it.
Here's my issue, in django-rest-auth there is a specific url to change a user's password, which is /rest-auth/password/reset/. I'd like the url to instead be /auth/password/change/, but don't want to edit the library code to do so.
The issue is that as of now, in my url.py file, I have the rest-auth urls imported as such:
from django.urls import path, include
urlpatterns = [
path('', include('rest_auth.urls')),
]
So it just imports the urls as written in the library. How can I change the specific url to what I want?
You just need to add a url like this
path('/auth/password/change/', your_view)
your_view will be same as view of /rest-auth/password/reset/ which is PasswordResetView.

Routing error using Wagtail v2 with Django v1.11

Can't seem to find anything on this so far, I'm guessing I have something misconfigured somewhere.
I have a pre-existing Django app and am trying to use Wagtail to add a blog in. I have installed as per the instructions and I can access the default landing page so it seems to be installed however I'm stuck with the below error when attempting to access the admin for it and am not sure how to proceed. Guessing its something to do with not defining namespaces somewhere, the Wagtail docs say that its compatible with Django v1.11 but their integration documentation is for Django v2 specifically and is using re_path etc.
'wagtailadmin_api_v1' is not a registered namespace
My main project urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls', namespace='blog')),
url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')),
url(r'^autocomplete/', include(ac_urls, namespace='ac')),
url(r'^', include('website.urls', namespace='website')),
]
My blog app's urls.py (which is fresh from a "python manage.py startapp blog")
from django.conf.urls import url, include
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls
app_name = 'blog'
urlpatterns = [
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
]
When I attempt to access localhost:8000/blog/ I get the default wagtail landing page, if I go to localhost:8000/blog/cms/ I get the above error though. If I go to localhost:8000/blog/documents/ I get a 404 as well.
Not sure where I'm going wrong, have tried using a version of wagtail that still used the Django v1.11 way of routing in urls.py but I got the same error!

Serve entirely static (documentation) website from Django url in Django 1.10

I am using mkdocs for a wiki documentation site that serves markdown tutorials and general information files created by various people. mkdocs ouputs an entirely static site in a site directory. Is there anyway to serve this site in django 1.10? I know you used to be able to do something like this:
url(r'^docs/wiki/', 'django.views.static.serve', {'document_root': base.DOCS_ROOT, 'path': 'index.html'}),
url(r'^docs/wiki/(?P<path>.*)$', 'django.views.static.serve', {'document_root': base.DOCS_ROOT}),
but that no longer works in Django 1.10 because views must be callable lists or tuples.
I would prefer to not serve this out of templates because the assets for the website in order not to split the assets mkdocs creates into the django static directory from the html files it creates that would presumably be in templates.
error given if attempted to run server:
raise TypeError('view must be a callable or a list/tuple in the case of include().')
Any simple way to do this with urls?
Django 1.10 no longer allows you to specify views as a string (e.g. 'django.views.static.serve') in your URL patterns.
The solution is to update your urls.py to include the view callable. This means that you have to import the view in your urls.py. If your URL patterns don't have names, then now is a good time to add one, because reversing with the dotted python path also no longer works.
Update your code like this :
from django.views.static import serve
from django.conf.urls import url, patterns
urlpatterns = [
url(r'^docs/wiki/',serve, {'document_root': base.DOCS_ROOT, 'path': 'index.html'},name = "wiki1"),
url(r'^docs/wiki/(?P<path>.*)$', serve, {'document_root': base.DOCS_ROOT},name = "wiki2"),
]

Django project on two domains - limiting access to urls/views

I am working on django project.
It utilizes multiple small apps - where one of them is used for common things (common models, forms, etc).
I want to separate whole for project to two domains, i.g.:
corporatedomain.com and userportal.com
I want corporatedomain.com to use different urls, same for userportal.com.
Is that possible? If so, how can I do this? How should I configure my urls?
Maybe you can look at the Django Site Framework. From Django official documentation:
Django comes with an optional “sites” framework. It’s a hook for associating objects and functionality to particular Web sites, and it’s a holding place for the domain names and “verbose” names of your Django-powered sites.
You can use then this approach
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
if 'userportal' in current_site.domain:
urlpatterns = patterns('',
url(r'', include('userapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
else:
urlpatterns = patterns('',
url(r'', include('corporateapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
You should add as many entries as you need to Site Table and add django.contrib.sites app in your INSTALLED_APP and also a SITE_ID variable to your settings bound with the correct site ID. Use SITE_ID = 1 when no domain info are available (for example in your development session). More info about SITE_ID in this post).
In my settings I use the following approach:
SITE_ID = os.environ.get('SITE_ID', 1)
where I have set the right SITE_ID variable in each of my enrivorments.
You will have separate settings file anyway so define different ROOT_URLCONF for each domain.
UPDATE: If you don't want to use different settings then you have to write the middleware which will change the request.urlconf attribute using the HTTP_HOST header. Here is the example of such middleware.

Categories

Resources