Maps is not displaying in admin site using geopositions - python

Django: 2.0.7
django-geopositions: 0.3.0
I have configured everything in SETTINGs but map does not show at admin site, only lat and long fields are shown.
My model is very simple as:
class Shop(models.Model):
name = models.CharField(max_length=100)
position = GeopositionField()
Any idea?

Please go to this link:
pull request
And edit your geoposition app as given.

Related

How to prevent Error: 403 in ModelAdmin.autocomplete_fields?

ModelAdmin.autocomplete_fields looks to be very simple to implement into the Django admin:
class UserAdmin(admin.ModelAdmin):
autocomplete_fields = ['material']
admin.site.register(User, UserAdmin)
class MaterialAdmin(admin.ModelAdmin):
search_fields = ['name']
admin.site.register(Material, MaterialAdmin)
It renders the field correctly (as a search field instead of a dropdown), but the search field says "The results could not be loaded" and inspect shows:
*/admin/autocomplete/ 403 (Forbidden)
jquery.js:9203
I assume there is a csrf issue receiving data from the Material model. I looked into ways to exempt this request from csrf but couldn't figure out how to do it through ModelAdmin.autocomplete_fields.
I tried using django-autocomplete-light as well and couldn't get it working.
That's because of the conflict between DAL and Django 3.2 + versions. If you turn DAL off it can solve this problem. DAL's js overloads Django's one and that's it. To know more just follow the link to
Dal's github issue
If you implement your own UserAdmin either make sure search_fields is defined, as requested in the documentation, or use Django's UserAdmin as a base class.
Also, if you just upgraded your Django version and it stopped working, clear your cache to load the proper javascript files.

places maps with geojson in django error

i want to show some dynamic places in my web site and i follow this tutorial
geojson
models.py
class MushroomSpot(models.Model):
geom = PointField()
description = models.TextField()
picture = models.ImageField()
#property
def popupContent(self):
return '<img src="{}" /><p><{}</p>'.format(
self.picture.url,
self.description)
MushroomSpot is the ForeignKey to other models in my model
admin.py
from leaflet.admin import LeafletGeoAdmin
admin.site.register(MushroomSpot,LeafletGeoAdmin)
all work without error code.
but in my admin(administrator page) i cant to add points because dont show me the map.
administrator page
That is because you might need a widget in your form to show the map in the admin portal.
You can try django-leaflet library, they seem to support Leaflet maps form widgets.

Django Registration Redux Registration form setting

In Django Registration Redux, there are alternative registration forms that one can use. For Example, one can use RegistrationFormTermsOfService or RegistrationFormUniqueEmail in the registration.forms file. I read the code and figure that the way to do this is by setting a REGISTRATION_FORM variable in the settings.py file. In registration.views we see the following:
REGISTRATION_FORM_PATH = getattr(settings, 'REGISTRATION_FORM','registration.forms.RegistrationForm')
REGISTRATION_FORM = import_string( REGISTRATION_FORM_PATH )
However, when I do set the REGISTRATION_FORM in settings.py to 'registration.forms.RegistrationFormUniqueEmail', I still can't get the form for unique email. Any help will be appreciated.
Okay, so I figured this one out. Basically, in the version 1.1 of django registration redux, this option of setting which registration form in the setting was not available. The code I was looking at from github is the version 1.2 version. So if this happens to you, just install from github.

Read only usernames in django admin site

I'm using django and its built in admin site for the project I'm working on. (Python 3.4, django 1.7)
For the current project I'm working on I require usernames to be fixed after object creation. I have made a small test project, which literally contains one app, and that app only contains this admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyAdmin(UserAdmin):
def get_readonly_fields(self,request, obj=None):
ro_fields = list(super().get_readonly_fields(request, obj))
if obj is not None:
ro_fields.append('username')
return ro_fields
admin.site.unregister(User)
admin.site.register(User, MyAdmin)
As expected, the Username can be set in the Add page and is Read Only in the change page. However, I cannot save when on the change page, I receive the message "please correct the errors below" but no field is highlighted. Can anyone suggest a way to work round this problem?
EDIT: as a diferent option I attempted to replace the username with a widget that didn't allow input - no luck there either

Django: Remove "view on site" button in the admin User change form

get_absolute_url() method is cool, but in some cases is not needed. django.contrib.auth.models.User has it set by default, this cause my projects to have a broken link in the admin.
How can I prevent that from happening?
In one of my old projects I set a custom template in which I removed the html of the button, it doesn't sound like a good solution that would scale though. Anything better?
If you click on Django 1.7 link, the site will tell you that "Its an insecure version of Django that is no longer supported. Please upgrade to a newer release!"
For Django 1.9, following solution works fine as mentioned in the Django documentation
in myapp/admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
# Disable View on Site link on admin page
site_url = None
This can be done, per model, as of django 1.7.
# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
view_on_site = False
admin.site.register(MyModel,MyModelAdmin)
Instead of monkey patching you can hide the button on the client side using JavaScript. The HTML of the view on site button looks like this:
<li>View on site</li>
If you just hide the anchor tag you will get part of the round button appearing as that is applied on the li tag. Now unfortunately there is no easy way to use css to select that specific li tag since it doesn't have a class, name or id on it. So we can use jquery which gives you more control on your selectors. Put the following in your static folder. For example in the location static/admin/user_change_form.js
django.jQuery( document ).ready(function($) {
$(".viewsitelink").parent().css('display', 'none')
});
Your admin.py could then look something like this:
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.admin import site
class CustomUserAdmin(UserAdmin):
class Media:
js = ['admin/user_change_form.js']
site.unregister(User)
site.register(User, CustomUserAdmin)
UPDATE
A feature was added in Django 1.7 ModelAdmin.view_on_site which lets you not display the “View on site” link.
I know this is old but I came across it when I had to do the same thing.
A better solution would be to monkey patch it in an accounts/admin.py file when you have accounts in INSTALLED_APPS.
admin.site.unregister(User)
# We don't want a broken View on Site link. Thanks for that, contrib.auth!
del User.get_absolute_url
admin.site.register(User, MyUserAdmin)
Django 2.0 above you can add in default admin
admin.site.site_url = None
Above trick worked for me very well.
As a last resort, I have a monkey_patch app at the bottom of my INSTALLED_APPS which modifies the built in django contrib apps in ways I haven't found better ways to modify such as username length, default admin forms, __unicode__, etc.
Just watch your back when you upgrade django / in general.
from django.contrib.auth.models import User
del User.get_absolute_url
I'm using Django 1.4 and Marwan Alsabbagh's solution worked fine for me. Although, when opening/refreshing User change form there was a short blink. That is because JQuery hides this button only when page is loaded.
To solve this minor issue I used CSS to hide the whole .change-form block. After page is loaded this block's visibility is restored by means of JQuery. So, my code looks like this:
admin.py:
class Media:
js = ['js/admin/user_change_form.js']
css = {'all': ('css/admin/user_change_form.css',)}
...static/css/admin/user_change_form.css
.change-form {
visibility: hidden;
}
...static/js/admin/user_change_form.js
/* Do not show 'View on site' button in User change form */
django.jQuery( document ).ready(function($) {
$(".viewsitelink").parent().css('display', 'none')
/* restore visibility of the page (hidden in css to avoid blinking) */
$(".change-form").css('visibility', 'visible')
});
use
admin.site.site_url = "your url here"
in url.py of ur main app to modify the "visit site" on django page
and
for "view_on_site" removal
use
view_on_site = False in ur class with display_list for
Inside your app config (apps.py) do this:
class MyAppConfig(AppConfig):
def ready(self):
admin.site.site_url = None
Works in Django 4.0 as well.

Categories