Error 404 on Browser not showing Static folder file - python-3.x

When i imported os at first and write at the most bottom of settings.py
STATICFILES_DIRS =[
os.path.join(BASE_DIR,
'static')
]
To run my static folder which contains test.txt
But when I tried to run it on Opera it shows Page not foun 404.
Please tell what Can be the issue in this Django program.

You need to run the following command to collect static files first
python manage.py collectstatic

Your settings.py file must include
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'))
After this you import static method in your urls.py file of project and then add
from django.conf.urls.static import static
urlpatterns = [ ]+
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Related

Django href link not routing to correct url

The links within my pages/templates/base.html which are used for a header template results in 404 error. The pages load correctly when manually written 'http://127.0.0.1:8000' 'http://127.0.0.1:8000/about/'.
I am using class based views and following chapter 3 of Django for beginners (William Vincent).
pages/templates/base.html:
<header>
Home |
About
</header>
pages/urls.py:
from django.urls import path
from .views import HomePageView, AboutPageView
urlpatterns = [
path("", HomePageView.as_view(), name="home"),
path("about/", AboutPageView.as_view(), name="about"),
]
portfolio/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("pages.urls")),
]
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages.apps.PagesConfig',
]
pages/views.py:
from django.views.generic import TemplateView
# Create your views here.
from django.http import HttpResponse
class HomePageView(TemplateView):
template_name = 'home.html'
class AboutPageView(TemplateView):
template_name = "about.html"
The error is as follows:
Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
about/ [name='about']
The current path, about/{% url 'about' %, didn’t match any of these.
Terminal error:
Not Found: /{% url 'about' %
[26/Oct/2022 11:40:02] "GET /%7B%%20url%20'about'%20% HTTP/1.1" 404 2497
I think the issue lies in pages/urls.py, as if click the 'home' link from 'http://127.0.0.1:8000/about/' the url is 'http://127.0.0.1:8000/about/%7B%%20url%20'home'%20%'. I've tried to remove the "about/" from " path("about/", AboutPageView.as_view(), name="about"),
"
I am also not sure whether the href tags have been written correctly. I have looked at this and this but can't figure it out. Thanks.
{% url 'about' % <- you need to close it properly.

Unable to upload images as Django admin

This is the models.py code to define the field type and to accept the input of type image
models.py
car_photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d')
car_photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py of (project)
urlpatterns [
....
....
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
but when I run the program and click on upload image for car_photo_1 the upload option is greyed out so I'm unable to save the input. Can someone assist on how to proceed here?
Check bellow necessary settings for the image handle in Django
Add Code in Project (urls.py)
from django.contrib import admin
from django.urls import path,include
from django.conf import settings # --------> this
from django.conf.urls.static import static # --------> this
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # --------> this
Add Code in Project (setting.py)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

I am unable to import Static Files in Django website. Any on please explain what i am missing in code?

I am facing issue while loading static files in the project. I have created the folder named as static, added (JS,CSS and Image files to the folder). I have provided the path in settings.py file. Please any one explain me that what i am missing and where i have to do modifications.
STATIC_URL = "/static/"
STATICFILES_DIR = [os.path.join(BASE_DIR, "static")]
LOGIN_URL = "account:login"
LOGIN_REDIRECT_URL = "public:index"
LOGOUT_REDIRECT_URL = "public:index"
Installed Apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website.apps.accounts',]
maybe you missed to put the STATIC_URL in your urls.py main file, as is shown in the official documentation
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Link official documentation
I have already added the static root directory with in URLS.py file

Import "users" could not be resolved Pylance(reportMissingImports)

I wanted to import my views.py file from my app "users" and the urls.py of the project. However, I'm getting an error that the "Import "users" could not be resolved".
Picture of the problem
The urls.py file:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from users import views as user_views # Why is there an error?
urlpatterns = [
# Admin Page
path('admin/', admin.site.urls),
# Homepage
path('', include('blog.urls')),
path('register/', user_views.register, name = 'register'),
path('profile/', user_views.profile, name = 'profile'),
path('login/', auth_views.LoginView.as_view(template_name = 'users/login.html'), name = 'login'),
path('logout/', auth_views.LogoutView.as_view(template_name = 'users/logout.html'), name = 'logout'),
]
The app is also in "INSTALLED_APPS" in the settings.py file:
INSTALLED_APPS = [
'crispy_forms',
'users.apps.UsersConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Folder structure
This is probably an easy-to-solve problem, however, I'm fairly new to Django and don't really understand why it says this is an error. In a similar project, everything works fine.
No error in a similar project, where I did everything the same way as far as I remember.
In your root directory in the vscode left pane, there is a folder .vscode.
click on that and the following key:value pair of your directories ex:["./users", "./blog"] it will resolve relative imports. It worked for me.
{
"python.analysis.extraPaths": ["./users", "./blog"]
}
This error has come because you have app users and blog which you have not written in settings.py file in INSTALLED_APPS
INSTALLED_APPS = [
'blog',
'users',
'crispy_forms',
'users.apps.UsersConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and include urls also in project urls.py

Unresolved reference 'views' in django

urls.py
from django.contrib import admin
from django.urls import path, include
from crudapplication import views
urlpatterns = [
path('admin/', admin.site.urls),
path('emp', views.emp),
path('show', views.show),
path('edit/<int:id>', views.edit),
path('update/<int:id>', views.update),
path('delete/<int:id>', views.destroy),
]
ModuleNotFoundError: No module named 'webapplication.crudapplication'
program structure hierarchy
from `.`crudapplication import views
You forgot Dot(.)
simply because they are on different packages. Try this approach:
Define the urls.py in your crudapplication then use include in your main urls
In your crudapplication package, create urls.py with this:
from django.urls import path
from . import views
urlpatterns = [
path('emp/', views.emp),
# the rest of your views..
]
back to your webapplication's urls.py, insert following:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('crudapplication.urls'))
]
This will resolve the IDE error and solve the URLS routing.
Note that, your current approach will throw IDE warning but i believe ur routing still works.

Resources