How to write url pattern for login() with an template name argument in django4 - django-templates

TypeError at /users/login/
login() got an unexpected keyword argument 'template_name'
from django.contrib.auth import login
from django.urls import path
from . import views
app_name = 'users'
urlpatterns = [
# Login page
path('login/', login, {'template_name': 'login.html'}, name='login'),
# Registering page
path('register/', views.register, name='register'),
]
I tried this url pattern but it gives me TypeError...
Any other way of passing template name in django login() function url pattern

Related

How do I create a module above App module in angular 12?

i have a Login module which contains login.html.
i want login to be the default module and login.html as the default opening page.
in main.ts file you will have something like this
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
replace this with
import { LoginModule } from './login.module';
platformBrowserDynamic().bootstrapModule(LoginModule);

Register, Login, Logout with django class based views

I am trying to make an authentication system with django class based views
What I have tried so far:
This is my views.py file:
from django.shortcuts import render
from django.views import generic
from .forms import UserRegistrationForm
from django.urls import reverse_lazy
class UserCreationView(generic.CreateView):
form_class = UserRegistrationForm
template_name = 'registration/register.html'
success_url = reverse_lazy('login')
This is forms.py file
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class UserRegistrationForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2',)
widgets = {
'email': forms.EmailInput(attrs={'class':'input', 'placeholder': 'Email Address'})
}
This the urls.py file:
from django.urls import path
from .views import UserCreationView
urlpatterns = [
path('register/', UserCreationView.as_view(), name = 'register'),
]
Other urls.py file(urls.py file present in project folder)
path('users/', include(urls)),
path('users/', include('authentication.urls')),
The problem with this code is Whenever I login I can still login and register by going to the login url(login url in this case:http://localhost:8000/users/login/), How can I restrict user to log in again
I found out that the default LoginView() class on
django.contrib.auth import views
has a boolean attribute called
redirect_authenticated_user
And I believe this is what you are looking for.
Just look for class LoginView in https://docs.djangoproject.com/en/3.2/topics/auth/default/ and you will see it.

How to use ActivatedRoute in Angular 5?

I am trying to do exactly the same thing as in this post: Angular 4 get queryString
I am using Angular 5.2.5.
ActivatedRoute seems to be the thing to use to retrieve querystring values off the URL when the user first visits the website. However, I am unable to figure out what I need to import to be able to use ActivatedRoute.
Could someone specify exactly what needs to be added to the app.module.ts file, and the component.ts file where I am trying to use ActivatedRoute?
This post specifies adding routing to the imports array of the #NgModule: No provider for ActivatedRoute - Angular 2 RC5. However, I don't have an app.routing.ts file. Do I have to create an app.routing.ts file to use ActivatedRoute?
ActivatedRoute Contains the information about a route associated with a component loaded in an outlet.
It can also be used to pass data from one component to another component using route such as Id, flag, state etc.
http://localhost:4200/quiz/edit_quiz/032
032 being id of the quiz you wanna edit.
Get this id in your component(in my case let it be edit_quiz.compontent.ts) to use by using Activated Route.
Step 1: Import ActivatedRoute from Router module.
import { ActivatedRoute } from '#angular/router';
Step 2: Inject ActivatedRoute in constructor.
constructor(private activatedRoute: ActivatedRoute) { }
Step 3: Get id on a local variable named quizId in ngOnInit(){}
ngOnInit() {
this.quiz_id = this.activatedRoute.snapshot.params['id'];
}
Now we have id in edit_quiz.component.ts to use.
I made the two changes Arun suggested. Then, to fix the "No provider for ActivatedRoute" error, I made the changes shown below.
1) I added this line to the app.module.ts:
import { RouterModule } from '#angular/router';
2) I added this line to the imports array of the #NgModule in app.module.ts:
RouterModule.forRoot([])
This article gave me the fix: Angular error: no provider for ActivatedRoute
Now it compiles. Hooray!
You need to import ActivatedRoute from #angular/router like
import { ActivatedRoute } from '#angular/router';
then add this line to the imports array of the #NgModule in app.module.ts:
imports:[
........,
RouterModule.forRoot()
],
then you can use any where as below:
constructor(private route: ActivatedRoute) {
console.log(route.snapshot.queryParamMap); // this
}
// or
queryString : string;
getQueryString(){
this.queryString = this.route.queryParamMap.get('myQueryParam');
}
No. You don't need app.routing.ts if you don't have to navigate pages within your app.
How to Get Route Parameters:
The Angular Router provides two different methods to get route parameters:
a. Using the route snapshot(ActivatedRoute),
b. Using Router Observables
ActivatedRoute in Angular:
Provides access to information about a route associated with a component that is loaded in an outlet
Step-1 Import the ActivatedRoute interface
import { Router, ActivatedRoute } from '#angular/router';
Step-2 Inject the ActivatedRoute in Constructor
constructor(private route: ActivatedRoute, private router: Router) {}
Step-3 To fetch a employee object by the given id and assign that object to its local employee property.
ngOnInit() {
this.employee = new Employee();
this.id = this.route.snapshot.params['id'];
Note: Property Description
snapshot: The current snapshot of this route
ActivatedRoute
I'm late to the conversation but hope the following works for the future programmers who encounter the same issue.
import the ActivatedRoute
import { ActivatedRoute } from '#angular/router';
Inject the dependency injection
constructor(
private route: ActivatedRoute,
) { }
and to grab the id from the link you can use the following
ngOnInit() {
this.route.paramMap.subscribe(params => {
this.product = products[+params.get('productId')];
});
}

Aurelia-Router: Modify route params and address bar from VM with Router

I want update the url-params in the address bar without routing.
But i'm not sure how to do this with Aurelia-router from a view-model.
In my case I send IDs in the url which gets picked up by the view-model's activate-method.
The route looks like this:
http://localhost:3000/#/test/products?0=2599037842&1=2599080552
Then I want to be able to remove IDs from the url without reactivating the view-model, url result exemple:
http://localhost:3000/#/test/products?0=2599037842
Hopefully there is support for this in Aurelia-router
Thanks!
/Mike
Yes, you can do that with router.navigateToRoute() method. navigateToRoute has additional parameters. Use options (third) parameter to modify how the navigation is done.
Example:
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
#inject(Router)
export class Products {
constructor(router) {
this.router = router;
}
activate(params) {
// TODO: Check your params here and do navigate according to values
this.router.navigateToRoute(
this.router.currentInstruction.config.name, // current route name
{ '0': params['0'] }, // route parameters object
{ trigger: false, replace: true } // options
);
}
}
From documentation hub:
navigateToRoute(route: string, params?: any, options?: any): boolean
Navigates to a new location corresponding to the route and params specified.
Params
route: string - The name of the route to use when generating the navigation location.
params?: any - The route parameters to be used when populating the route pattern.
options?: any - The navigation options.
With options you control how the history is updated.
trigger: false - prevents the router navigation pipeline to be triggered
replace: true - replaces the current URL in history with provided route (rewriting history), so it won't be triggered with browser back functionality

Mailbox not in the scope

I am trying to create a frontend using elm with elm-router in order to manage the routing.
I have this main file :
-- Main.elm
import Router exposing (Route, match, (:->))
import History exposing (path)
import Routing as Routing exposing (route)
import Signal exposing ((<~))
import Html exposing (Html)
import Signal exposing (Mailbox, mailbox, Signal)
import Task exposing (Task)
import Test as Test exposing (pathChangeMailbox)
main : Signal Html
main = route <~ path
port runTask : Signal (Task error())
port runTask =
pathChangeMailbox.signal
This module to handle a mailbox. I could use a mailbox by page but it's ugly so I tried make a module to handle all the common code.
module Test where
import Signal exposing (Mailbox, mailbox, Signal)
import Task exposing (Task)
pathChangeMailbox : Mailbox (Task error ())
pathChangeMailbox = mailbox (Task.succeed())
I also have a module Routing to list the route and associate action to them.
And here is the module to handle the menu, and so the navigation.
module Menu where
import Html exposing (a, text, Html, div)
import Html.Events exposing (onClick)
import Task exposing (Task)
import History exposing (setPath)
import Test as Test exposing (pathChangeMailbox)
display : String -> Html
display _ =
div []
[
a []
[ onClick Test.pathChangeMailbox.address (setPath "/CV.elm") ]
[ text "Mon CV" ]
]
When I try to run the program I got the following error :
Error in .\src\Menu.elm:
Error on line 14, column 22 to 47:
Could not find variable 'Test.pathChangeMailbox.address'.
Looks like the prefix 'Test.pathChangeMailbox' is not in scope. Is it spelled correctly?
Is it imported correctly?
You can read about how imports work at the following address:
Do you have anyidea, why even if I import the Test module it's keep saying that Test.pathChangeMailbox is not in scope ?
Edit : Add Routing.elm
module Routing where
import Html exposing (Html, div, text, br)
import Signal exposing (Mailbox, mailbox, Signal)
import Task exposing (Task)
import Router exposing (match, (:->), Route)
-- Import displays
import CV as CV exposing (display)
import Home as Home exposing (display)
route : Route Html
route = match
[
"/src/Main.elm" :-> Home.display
, "/CV.elm" :-> CV.display
] display404
display404 : String -> Html
display404 url =
div []
[
text "Erreur 404"
, br [] []
, text ("url : " ++ url ++ " not found.")
]
I think you've found a bug here. You should file an issue.
EDIT: good job :) Summary of the link: The problem comes from mixing the . used for qualified module access and record access.
Here's a workaround for now, import the name unqualified and use it unqualified. Note I also removed an extra []:
## -4,13 +4,13 ## import Html exposing (a, text, Html, div)
import Html.Events exposing (onClick)
import Task exposing (Task)
import History exposing (setPath)
import Test as Test exposing (pathChangeMailbox)
display : String -> Html
display _ =
div []
[
- a []
- [ onClick Test.pathChangeMailbox.address (setPath "/CV.elm") ]
+ a
+ [ onClick pathChangeMailbox.address (setPath "/CV.elm") ]
[ text "Mon CV" ]
]
(Menu.elm)