Favicon redirect in django 2.0 - django

I'm trying to redirect the default browsers request of /favicon.ico to serve the picture from my static folder and I get a 404(server log screenshot).
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
urls.py
from django.contrib import admin
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import include, path
from django.views.generic.base import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path(
'favicon.ico',
RedirectView.as_view(url=staticfiles_storage.url('upload.ico'))
),
path('', include('uploader.urls'))
]
My directory structure:
../
├ imguploader/
| ├ __init__.py
| ├ settings.py
| ├ urls.py
| └ wsgi.py
├ static/
| ├ admin/
| └ upload.ico
├ manage.py
|...

You can set STATICFILES_DIRS in settings.py, works for me:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.views.generic.base import RedirectView
urlpatterns = [
path('favicon.ico', RedirectView.as_view(url=settings.STATIC_URL + 'favicon.ico')),
path('admin/', admin.site.urls),
]

Related

Static Files not loading when DEBUG = False

I'm using namecheap shared hosting and I hosted my site using cpanel. My site is working fine but if i made DEBUG = False in project settings.py file the static files are not loading. My site url: https://drshahidabegum.com/
settings.py
-----------
# Static files (CSS, JavaScript, Images)
STATIC_DIR = [
BASE_DIR / "static",
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py in project folder
-------------------------
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dr_shahida.urls')),
]
# Configure Admin Titles
admin.site.site_header = "DR. Shahida Begum"
admin.site.site_title = "DR. Shahida Begum"
admin.site.index_title = "Welcome to Admin Dashboard"
handler404 = 'dr_shahida.views.error_404_view'
urls.py in app folder
-------
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index, name='index'),
path('testimonial/', views.testimonial, name='testimonial'),
path('contact/', views.contact, name='contact'),
path('blog/', views.blog, name='blog'),
path('blog/post/<int:post_id>', views.blogdetailview, name='blogdetailview'),
path('set_language/<str:ln>', views.set_lang, name='set_lang'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
I want my static files to work when DEBUG = False in project settings.py file.

I ruined the admin page, how do I fix it? ( Django 4.1 )

I have followed the steps from the django tutorial ( https://docs.djangoproject.com/en/4.1/intro/tutorial01/ ) and once I'd finished all the steps I stopped developing the project for 2 weeks at least without giving it much thought.
Once I've decided to check how the pages were looking like that's when I found this:
The admin page
And I don't know how I did it.
Here's how the documentation looks like:
meuSite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
polls/
migrations/
static/
templates/
admin/
base.html
polls/
detail.html
index.html
results.html
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
db.sqlite3
manage.py
SETTINGS:
meuSite/settings.py
INSTALLED_APPS = [
'polls.apps.PollsConfig',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
URLS:
# meuSite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("polls/", include('polls.urls')),
]
# polls/urls.py
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:question_id>/', views.DetailView.as_view(), name='detail'),
path('<int:question_id>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote')
]
If there's something else that I didn't show that may have the root of the problem, please tell me
I tried solving it by deleting the templates/admin directory, but it didn't change how the page looks

Django deployment Failed to load resource: the server responded with a status of 404 (Not Found)

I have some product pictures uploaded in media folder that displays fine in development server, but after deployment I receive the 404 ressource not found error.
In settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'node_modules')]
STATIC_ROOT = os.path.join(BASE_DIR, 'productionStatic')
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
In urls.py
from django.contrib import admin
from django.urls import path
from index import views
from index.views import indexPage, hdnytorv
from webshopRestaurant.views import hd2900_webshop_Main, AddressCheckForDeliverability, ChangeItemQuantity
from webshopCustomer.views import TakeawayCheckout, totalPriceDeliveryPossible, DeliveryForm, PickUpForm, localDeliveryCheckoutAddressCheck, Payment, PaymentComplete
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('', indexPage.as_view(), name = 'index'),
path('hdnytorv', hdnytorv.as_view(), name='hdnytorv'),
path('hd2900', views.hd2900.as_view(), name='hd2900'),
path('hd2900_takeaway_webshop', hd2900_webshop_Main.as_view(), name="hd2900_takeaway_webshop"),
path('check-address-for-deliverable', AddressCheckForDeliverability.as_view()),
path('changeItemQuantityInBasket', ChangeItemQuantity.as_view()),
path('isPriceAboveDeliveryLimit', totalPriceDeliveryPossible.as_view()),
path('hdbynight', views.hdbynight.as_view(), name='hdbynight'),
path('takeawayCheckout', TakeawayCheckout.as_view()),
path('deliveryFormCheckout', DeliveryForm.as_view()),
path('pickupFormCheckout', PickUpForm.as_view()),
path('local_delivery_checkout_is_address_deliverable', localDeliveryCheckoutAddressCheck.as_view()),
path('localDeliveryPayment', Payment.as_view()),
path('paymentComplete', PaymentComplete.as_view()),
]
#When in production medida url must always be added to urlpatterns
#if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
To display the product in view I have in my html template
<img class="card-img embed-responsive-item" src="{% get_media_prefix %}{{ item.product.image_path }}" alt="{{item.meta_description}}">
The pictures are uploaded to this location django_project_folder/media/productImages. I have assured that it is owned by www-data group and also media and all subfolders have 775 access rights. In development server the images shows up all right but in production I got 404 not found error.
This issue is a duplicate. See the answer here I fixed it by adding in urls.py
from django.urls.conf import re_path
from django.views.static import serve
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),

Refused to apply style because its MIME type ('text/html') in django and next js

hello i did everything with integration but nothing seems working,its again saying the same error
all next js files are default no changes
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'frontend/.next/static/css')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.test)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
pls help me find a solution to this problem i have done everything that i can do with this integration

django favicon icon does not display

I have the following django project file structure:
lecture3/
lecture3/
urls.py
tasks/
static/
img/
favicon.ico
urls.py
my tasks/urls.py file is:
from django.urls import path
from . import views
from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic.base import RedirectView
app_name = 'tasks'
urlpatterns = [
path("",views.index, name="index" ),
path("add",views.add, name="add" ),
path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('img/favicon.ico'))),
]
but when I run I get a 404 error. How do I fix this?
Try looking in your settings.py file for STATIC_URL = '/static/'. I moved my favicon.ico into this directory and my problem went away. I don't have it in my urlpatterns.