Django Admin Look strange - python

It's a new install of Django 3 and I get this admin look for the panel :
I have no error in the browser console
I have already done
python3 manage.py collectstatic
One clue :
To be able to see this admin panel without apache 500 error, i must comment :
django.contrib.staticfiles
in settings.py, under MIDDLEWARE
if django.contrib.staticfiles is called in MIDDLEWARE, I have this apache error :
TypeError: 'module' object is not callable,
My css and js looks ok...
I can't find how to correct this view

from django.contrib import admin
admin.autodiscover()
admin.site.enable_nav_sidebar = False
Add the above code somewhere in your main urls.py file
This issue has been raising in django 3.1.
It's not an error. It's just a new side bar being added therein.
Add the above code to disable it.

Related

RedirectView.as_view not working at the root

In a django 1.8 project, I am attempting to redirect http://myProject/ads.txt to an external url http://a.location.that.has.the.ads.txt.file and thus serve the ads.txt file without using ftp to simply place the ads.txt in the root.
Given this minimal directory structure:
django projects
myProject
myapp
urls.py
views.py
someotherapp
yetanotherapp
myProject
settings.py
urls.py (this is the top urls.py)
views.py
in myProject/myProject/urls.py, (the “top” urls.py) I have as the first entry in the urlpatterns list, the lines:
urlpatterns = patterns('',
(r'^ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
followed by many more pattern matching regex’s. This does not work and fails with a 404. What does work is
urlpatterns = patterns('',
(r'^foo/ads\.txt', RedirectView.as_view(url='http://a.location.that.has.the.ads.txt.file', permanent=False)),
and then calling http://myProject/foo/ads.txt
Unfortunately, ads.txt files must be placed at the site root. I have tried many different regex’s that test fine in a regex validator, but just don’t work (returns 404). How do I do this without the extra dir “foo”? Any thoughts appreciated. Thank you.
Turns out you cannot redirect with the top level urls.py "routing table" to above the Django project root. A nginx server level redirect did the trick.

Trouble loading Django App after testing other Django App for several days? Always redirects to requested URL that doesn't exist?

Over the weekend I attended a hackathon, at which I created a Django app that ran on 127.0.0.1:8000/myapp/list on my local machine with
python manage.py runserver
Today, I tried to launch another Django app that I had made in the past for bug fixes and things, but when I ran manage.py runserver it keeps redirecting me to 127.0.0.1:8000/myapp/list, even though it's a totally separate app.
Anyone know how to resolve this issue?
urls.py for Django app that I'm trying to run:
from django.conf.urls import url
from TweeTest import views
urlpatterns = [
url(r'^$', views.view_home, name='home'),
url(r'^about/$', views.AboutPageView.as_view()),
url(r'^contact/$', views.ContactPageView.as_view()),
url(r'^result/$', views.view_tweet, name = 'result'),
]
urls.py for Django app I made over the weekend:
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myproject.myapp.urls')),
url(r'^$', RedirectView.as_view(url='/myapp/list/', permanent=True)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Is it something to do with the RedirectView line in the second urls.py?
I'm not sure why a totally different Django app is trying to connect to the same URL as the other one... seems strange.
If someone knows the answer to this, that would be greatly appreciated!
Thanks!
UPDATE:
When I try to connect to 127.0.0.1:8000 on incognito mode, it works fine. Could this mean it's a browser issue?
Davis
permanent=True sends a HTTP 301 Permanent redirect to the browser. Browsers tend to cache 301 responses very aggressively, meaning your web browser currently remembers that http://127.0.0.1:8000/ is a permanent redirect to http://127.0.0.1:8000/myapp/list/. Since your browser doesn't know that different Django apps might be running on 127.0.0.1:8000 at different times, it always follows the cached redirect.
You shouldn't use permanent=True unless you know what you are doing. While it can boost the performance on a high-traffic site, it is rarely necessary for low- to medium-traffic sites, and the permanent caching can be a real pain for developers.
Either clear your browser cache, run your Django test server on a different port, or use a different browser.
Have you tried using a different browser or clearing your browser history? This sounds like a browser issue to me.

Create a view in django

Very new to django. I'm using version 1.5.2 and I just did a fresh install. I'm using the django development server; I'll be moving to Apache down the road, but I want to understand the django's particular flavor of MVC methodology before doing taking that step.
So I start up the django server with `python manage.py runserver 0.0.0.0:8000' through the terminal in my project directory (django_books). I get this error:
ViewDoesNotExist at /
Could not import django_books.views.home. Parent module django_books.views does not exist.
So my view doesn't exist. My view.py file is empty because the tutorial I was following did not include one. I'm not sure if this is the problem. If it is, how do I create this file (what goes in it)?
Directory Structure:
django_books
beer (from the tutorial lol)
migrations
__init__.py
models.py
views.py
random_book
(same as beer above)
django_books (this is my actual django project, beer and random_book are apps)
__init__.py
settings.py
urls.py
wsgi.py
media
.gitignore
manage.py
requirements.txt (output from pip freeze command)
urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'django_books.views.home', name='home'),
# url(r'^django_books/', include('django_books.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
If you keep your urls.py the way it is, that means you need to create views.py within /django_books/django_books/
Within that file, create a new function called home.
Alternately, if you have any functions inside of /django_books/beer/, you could reference them from urls.py.
All urls.py does is expose a python path to a function and route a URL there. So you can see that you don't have a module or file called views within django_books/django_books, which is why you get the failure.
View is basically a python function that receives HTTP Request and returns HTTP Response.
Quote from docs:
A view function, or view for short, is simply a Python function that
takes a Web request and returns a Web response. This response can be
the HTML contents of a Web page, or a redirect, or a 404 error, or an
XML document, or an image . . . or anything, really. The view itself
contains whatever arbitrary logic is necessary to return that
response. This code can live anywhere you want, as long as it’s on
your Python path. There’s no other requirement–no “magic,” so to
speak. For the sake of putting the code somewhere, the convention is
to put views in a file called views.py, placed in your project or
application directory.
This line url(r'^$', 'django_books.views.home', name='home'), in urls.py points the index / of your site to the home view - you should create it.
Create a python function called home in views.py:
from django.http import HttpResponse
import datetime
def home(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Restart your development server and visit http://127.0.0.1:8000.
FYI, read the tutorial more carefully, part 3 is about dealing with urls and views.

how to solve ViewDoesNotExist

I'm new to Django 1.4 and try my first project. It's OK to create the app:
./manage.py startapp APP_NAME
and recognized by Django in urls.py as following:
(r'^home/$', 'APP_NAME.views.home'),
but when I only create a APP_NAME.py files in root directory of projects,and change the urls.py file as following:
(r'^home/$', 'APP_NAME.home'),
the debug page tell me ViewDoesNotExist. Even if I change the urls.py file to (with from...import and without single quotes):
from APP_NAME import home
(r'^home/$', home),
It also doesn't work.
How to solve it? It intricate to create APP for every view file.
Have you actually implemented your home function in 'APP_NAME.__init__.py'; if not, your code will not work, because there is nothing that can be called
python manage.py startapp news
In your created news directory there is a views.py. Now lets add something:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
now in your urls.py you add this:
(r'^news/$', 'news.index'),
Just start the devserver python manage.py runserver and point your browser to http://localhost:8000/news/
You should see "Hello".
Now you could add in your news/views.py some more stuff - like details, archive and so on.
Also be sure to have an empty __init__.py file in every directory you'd like to import.
from app.views import something will not work if there's no init-file within the app-directory.

Django run admin inteface with custom template

In django how to run /admin interface as well as customized admin index page. My template dirs is followed below.
TEMPLATE_DIRS = (
PROJECT_PATH + '/templates/',
)
And...
ADMIN_MEDIA_PREFIX = '//admin/'
If i will comment this line my other functions would not work, if i put it uncommented then ma admin interface shows my specified file.
What should i do to run both simultaneously. Thanks in advance
Leave TEMPLATE_DIRS alone, that affects more than just the admin, and that's not your problem anyways.
The way to override any admin page is to include the associated template from the default Django admin templates in your own 'yourproject/templates/admin' directory, and make the necessary modifications.
See the documentation: https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#overriding-admin-templates

Categories

Resources