Universally usable files for django project - Django - 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')

Related

How do I reference a path (URL) in Django? But this path is not in the base app, but in another app

In the base app, which I call it "mywebsite" (the one that contains the settings of the django project), has the urls.py file. But I do not want to reference this file, I want to reference a urls.py in another app, which I call it "account".
For the base file, I would reference as {% url'login' %}, for example. How do I reference to the other one?
Maybe {% account.url 'login' %}?
I tried {% account.url 'login' %} and {% account/url 'login' %}
Never mind, I just added to the base urls.py file: path('account', include("account.urls"))
I see that you already answered your question, but with no further details or explanations. I'll provide the context needed.
You have a project package. This folders contains files that affect all the application (the website, not just an app). Inside we have the following files: settings, wsgi, asgi and urls.
In the urls.py (also called URLconf module) file you control the routing of your application, that is, the mapping between URL path expressions to Python functions (your views).
The Django frameworks knows where to look up for this file because it's stablished in the settings.py file:
ROOT_URLCONF = 'django_project.urls'
This is the process (from the docs)
Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting...
Django loads that Python module and looks for the variable urlpatterns. This should be a sequence of django.urls.path() and/or django.urls.re_path() instances.
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.
Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view)...
But you also have apps, besides the project package. Each app can also have a URLconf module (app/urls.py).
To Django to know the routing to this apps, you have to include them in the global urls.py file (from the project package).
Finally, the answer:
How do you do it? With the include() function.
As you mentioned in your own answer, the code is:
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("account.urls")),
]
You're including the accounts paths into the global URLconf module.
So when the user types www.your-domain.com/ he will access the accounts app routes.
If instead you did like below, the user should type: www.your-domain.com/accounts/.
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include("account.urls")),
]

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

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?

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

Django views in project directory

I have a project with two apps with their respective views, but I want to create a views.py in django project directory for some generic pages such as about, login... It is correct to place the views.py and templates in the root project folder for this purpose?
I have been reading about this. The practice more recommended for this is create an app that creates cohesion between other apps, call it web_site or site whatever is better for you.
python manage.py startapp my_site
Everything in this site app will be done the regular way.
In the projects url you will want to import the url in this way so the web pages appear in the / url pattern.
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_site.urls'))
]
I do this for project level pages like you describe. These are usually simple pages that tie the project's apps together, and not contain any business logic.
You can do that by making views.py file into the project directory.
from django.contrib import admin
from django.urls import path, include
from attendance import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('insert.urls')),
path('login/', views.logins, name='login'),# relevant line
]
And this is the code of login view
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def logins(request):
return HttpResponse("THIS IS LOGIN PAGE !!!")