I executed “python manage.py shell” in the SeniorProject → django_project directory where the manage.py file is located.
Then, I executed (MapDatabase.objects.all().last()) as well as MapDatabase.objects.all().last().
One with the parentheses and one without, but the result was still “NameError: name ‘MapDatabase’ is not defined” in both cases.
The following code is inside my map_app → models.py file:
from django.db import models
# Create your models here.
class MapDatabase(models.Model):
address = models.CharField(max_length=200, null=True)
data = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.address
Yes Mr. Whitesell! Thank you for your help with this. After creating a object of “MapDatabase” within the admins page, I no longer get that same error. I appreciate all of your help.
For some reason though, when I click on the “Search” button on another web page, it takes me back to the http://127.0.0.1:8000/ page in my Django app. Why is this?
Instead, I want the correct location to be shown on the international map of the world.
After typing in “Rome” in the Search bar on the top right of the web page, the following image display what we should see:
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import MapDatabase
from .forms import MapDataForm
import folium
import geocoder
# Create your views here.
def DisplayMap(request):
if request.method == 'POST':
mapForm = MapDataForm(request.POST)
if mapForm.is_valid():
mapForm.save()
return redirect('/')
else:
mapForm = MapDataForm()
# For the following line to work, we must have at least one 'MapDatabase' object.
address = MapDatabase.objects.all().last()
location = geocoder.osm(address)
latitude = location.lat
longitude = location.lng
country = location.country
if latitude == None or longitude == None:
address.delete()
return HttpResponse('Your search input was invalid.')
# Here there is a map object.
mapObject = folium.Map(location=[19, -12], zoom_start=2)
folium.Marker([latitude, longitude], tooltip='Click for description',
popup=country).add_to(mapObject)
mapObject = mapObject._repr_html_()
content = {
'mapObject': mapObject,
'mapForm': mapForm,
}
return render(request, 'map_app/DisplayMap.html', content)
The redirect() function redirects the user to another page.
However, I am not quite sure what redirect(‘/’) will do? Does this redirect the user to the home page of a Django application? Or does this redirect a user to the previous page in a Django application?
From testing this, I would have to say that return redirect(‘/’) will redirect the user to the home page of the Django application. Please let me know if I am correct.
Yes, you are correct. If you specify a url in the redirect call, it takes you directly to that url - in this case, the root url of the site.
(Side note: If your Django application is running in an environment where it’s mounted under a different directory name, ‘/’ might not be the home page of that application.)