django project always trying (and failing) to load file in separate app - django

I'm going back to an old django project and am trying to make a new site in a new app I just made, popularify. I want to load an httpResponse and nothing else when I go to the /popularify path , but it keeps trying and failing to get a favicon.ico file, the 404 error always appears in the chrome web console and django logs.
I have it set up correctly so my popularify page loads a basic httpResponse:
my main urls.py file in my project folder has a path:
path('popularify/', include('popularify.urls')),
The popularify app urls.py file looks like this:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
And the popularify views.py file looks like this
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
All very basic to just load some text, but in console I am getting a unrelated 404 error trying to load a file I never directly specified, and dont want to load:
If I click the favicon.ico link it shows the html file _base.html I have in my templates folder, a file I use as a base file in my pages/ app, which is completely separate and something I don't want to load in my new popularify app. Do I need to tell my popularify path to specifically not use this _base.html file in my templates folder?

Related

how can i create a html file template in django application?

I'm learning Django and I have a problem that there is no answer for it on internet
here is the problem,
I created a folder as 'app', and I create another folder in app as 'challenges' (in VS Code in a Django application),
so we have 2 nested folders
now I want to create an HTML page, and I create a file as 'index.html',
but VS Code cannot know this file as an HTML file
You need to make a folder structure like this
>app_name
>>...
>>templates
>>>app_name
>>>>index.html
views.py
from django.shortcuts import render
def index(request):
return render(request, 'app_name/index.html)
urls.py
from . import views
urlpatterns = [
path('', views.index),
]

Loading a .html file in django url with parameters

I have a file which I have to load directly through the web browser but it would still run inside of my django app.
What I have done so far is
url(r"^file_name.html", views.regular_person, name='regular_person')
But when I do this it shows the page could not be found. error 404
Please how can i get that to work.
Thanks
edit
the file would also have some query parameters
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('foo/', TemplateView.as_view(template_name='filename.html'))
]
django documentation
Is this something you are looking for.

Where is and how to edit the default Django homepage

Created a test demo project as below, how to edit the default Django homepage (or create my own homepage) that still using the same homepage url http://127.0.0.1:8000/?
Note: I don't want to create a new app and put the homepage under the new app, because that could change the homepage url from http://127.0.0.1:8000/ to http://127.0.0.1:8000/new_app.
You merely need to add the appropriate html template file and put the path into your project urls.py file. For example you could create a templates folder, under which you could add base.html or home.html or whatever you want (with the associated view), and then in urls.py you add:
path('', views.home),
You can get the default page back by adding the following in the urls.py file of your main app.
…
from django.views import debug
…
urlpatterns = [
…
path('', debug.default_urlconf),
…
]

Universally usable files for django project - Django

I am making a django project and within the project I have 3 different apps. registration, users, groups. I dont want to have to print all of the import statements in the views.py file for each app. I also dont want to have to retype any of the methods for functions in each of the views.py file for small functions like generatingRandomNumbers. Is there a way to create a single file within the project directory that has all of the import statements or all of the methods that i can call at the beginning of each views.py file....
Also where would i put the file if possible, and how can i call the fill with all of the import statements at the top of each of the views.py file so i dont have to keep retyping all of it...
Here is my current directory:
Another question is how can i setup the urls in a way that i can redirect to any url within the project no matter which app it is localated in... Django documentation does not explain any of this at all...
an example would be a view in the registration app redirects to a url in the users app.. Here is what i have:
registration -> urls.py
urlpatterns = [
url(r'^$', views.Signup, name='user_signup'),
url(r'^setup_profile/$', views.ProfileSetup, name='profile_setup'),
]
users -> urls.py
urlpatterns = [
url(r'^home/$', views.UserHome, name='home_page'),
]
redirect statement from the registrations -> views.py file:
return redirect('user_home')

Django on APP Engine - error redirecting a URL path to an existing function in views.py (on urls.py)

I'm getting an error when I try to redirect a URL path to an existing function inside of a views.py file.
I realize where the problem is, but I cannot figure out how to solve it.
I have the following structure of folders on my project:
my_app_gae
app.yaml
main.py
settings.py
urls.py
my_app_django (<-- here is my django project)
dashboard
views.py
models
models.py
The problem comes here:
when I edit the urls.py file, when I try to redirect a specific URL path to an existing function inside of views.py (landing), I recieve the following error:
Request Method: GET
Request URL: http://localhost:8090/landing/
Exception Type: ImportError
Exception Value: No module named my_app_django
The value of my Python Path is: V:\Python~1\my_app_gae (the place where the structure of folders I wrote before is).
The url.py value that I'm trying to execute is:
from django.conf.urls.defaults import *
from my_app_django.dashboard.views import landing
urlpatterns = patterns(
'',
(r'^landing/$', landing),
)
If I copy the views.py file directly on the my_app_gae directory it works. The problem comes when the views.py file is inside of other directories.
Thanks a lot.
Regards
To be recognized as a Python package, you need empty files named __init__.py in each subdirectory.