Where is and how to edit the default Django homepage - django

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),
…
]

Related

Change URL for wagtail blog index page

I currently have a website where the home page at www.mysite.com is the wagtail blog index page
I wish to move the blogindex page to another url
I can easily have a different homepage by amending my urls.py file:
#original
path("", include(wagtail_urls))
#new
path(
"",
TemplateView.as_view(template_name="pages/newhomepage.html"),
name="newhomepage",
),
However I would like the blogindex page available at e.g. myste.com/blog but I am not sure how to go about this. Adding the following to urls.py does not do it
path("blog/", include(wagtail_urls))
There are a couple of changes in project required to achieve this.
Firstly, create a urls.py file inside your specific app, this will be different from the one that you should already have alongside wsgi.py, asgi.py, etc. and it will only store app specific urls.
from django.urls import path
from .views import index
urlpatterns = [
path('test/', index, name='generalized-test-url'),
]
Now, you should render the template inside your view.
def index(request):
return render(request, 'pages/newhomepage.html', context)
Lastly, in your root urls.py file, you need to include the app urls. Lets say your app name is blog.
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
]

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

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?

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 1.10 one app page with a link redirect to another app page

I'm new to django server and trying to build a simple website for user registration. Here is the problem, I create my own app with index.html as my homepage. I also used another user registration app from:
https://github.com/pennersr/django-allauth/tree/master/allauth
I was trying to add the app to my homepage with a 'sign up' link. Basically, the account part, and ideally, the link can direct to: http://127.0.0.1:8000/accounts/login/
However, when I run the server, it gives me error:
Reverse for 'base' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
server result:
Both apps work fine individually, but when I try to add the link to my homepage, the error occurs.
The related code in index.html file in my first app:
<li>Log In</li>
The full path for index.html in my project is:
project/app1/templates/app1/index.html
The full path for base.html in my project is:
project/allauth/templates/base.html
I know I probably need to add a url line in my first app's urls.py file, and a view to show it, but how can I do it? Can anyone help me with this, much appreciate.
<li>Log In</li>
this line uses URL reversing, 'allauth:base' is the URL patterns, allauth prefix is the namespace, base is the named URL. You must define the namespace and named URL in the urls.py first.
Define your namespace in project's urls.py file like this:
from django.conf.urls import include, url
urlpatterns = [
url(r'^author-polls/', include('polls.urls', namespace='author-polls')),
url(r'^publisher-polls/', include('polls.urls', namespace='publisher-polls')),
]
Define your named URL in app's urls.py file like this:
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
...
]
all the help you need is in this document: Naming URL patterns

How can I fix my Wagtail URL namespace and explorer when adding wagtail to a current project?

My issue is my URL is coming up mysite.com/test-news-1/ instead of mysite.com/news/test-news-1/
I've started a new project using cookiecutter-django. I then wanted to add wagtail cms to the project and followed the docs to do so.
I get the wagtail admin page up fine, at /cms/ instead of /admin/ like it says to do on this page - http://docs.wagtail.io/en/v1.3.1/getting_started/integrating_into_django.html
I added a few apps just to practice and get used to wagtail, one directly copied from the wagtail blog example. This is where my issue starts.
The wagtail admin Explorer does not list my apps like it shows on the wagtail site https://wagtail.io/features/explorer/, instead it just says "Welcome to your new Wagtail site!" When I select Add Child Page it allows me to select the app pages I have set up and seems to go by my models just fine. But when I post something and click go to live site it comes up as mysite.com/blog1/ instead of mysite.com/blog/blog1/
I believe my problem that I dont understand the final part of the doc page that I linked above. It says,
Note that there’s one small difference when not using the Wagtail
project template: Wagtail creates an initial homepage of the basic
type Page, which does not include any content fields beyond the title.
You’ll probably want to replace this with your own HomePage class -
when you do so, ensure that you set up a site record (under Settings /
Sites in the Wagtail admin) to point to the new homepage.
I tried adding the homepage model from the doc page, but this didn't seem to help at all.
I'm very inexperienced, this is my urls.py file, if you need to see other files please let me know.
urls.py
from __future__ import unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name="about"),
# Django Admin, use {% url 'admin:index' %}
url(settings.ADMIN_URL, include(admin.site.urls)),
# User management
url(r'^users/', include("contestchampion.users.urls", namespace="users")),
url(r'^accounts/', include('allauth.urls')),
# Your stuff: custom urls includes go here
# Wagtail cms
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
url(r'^400/$', default_views.bad_request, kwargs={'exception': Exception("Bad Request!")}),
url(r'^403/$', default_views.permission_denied, kwargs={'exception': Exception("Permission Denied")}),
url(r'^404/$', default_views.page_not_found, kwargs={'exception': Exception("Page not Found")}),
url(r'^500/$', default_views.server_error),
]
These two url confs are in conflict =
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'', include(wagtail_urls)),
One must change, otherwise Django will always resolve the base url of `yourdomain.com/' to the first entry. One easy way to fix this is to update the second-
url(r'^content/', include(wagtail_urls)),
Now your Wagtail root page will be accessible at yourdomain.com/content.
As for mysite.com/blog/blog1/ not coming up, that Url would assume that, from your Wagtail root page, there's a page w/ slug 'blog', and then a child page of that with slug blog1. The tree structure of your Wagtail site determines the URLs by default. If you want to override that you'll have to use the RoutablePageMixin as described here.