Redirect from one django app to another one (django 2.1) - django-2.1

I have an app ('admin') that acts as a login/authentication framework for the admin side of my django project. From there, I intend to send the user to the pages of the various apps under its control.
The urls.py of that app is as follows:
urlpatterns = [
path('', views.login, name='login'),
path('home/', views.home, name='home'),
path('logout/', views.logout, name='logout'),
path('voters/', include('voters.urls'), name='voters'),
]
but when I try to use a link from this a template in the 'admin' app to redirect to the other app's page, it gives me the following error:
Exception Type: NoReverseMatch at /admin/home/
Exception Value: Reverse for 'voters' not found. 'voters' is not a valid view function or pattern name
The HTML used to redirect is as follows -
Voters
How can I redirect the user from this app to another app?

try : path('voters/', include('voters.urls')),
and please share more parts of your code like models, views and full template

Related

Dynamic Routes in NextJs don't load on different param

I am currently doing a project with NextJs about a blog posting application similar to medium.com. However, I am facing a little problem when it comes to dynamic routes.
Background Info: My pages structure looks like this:
pages
|-> profile
|-> [username].tsx
url path example: /profile/[username]
Use case: I am in the profile page of someone (/profile/someone), and in the navbar, which is globally accessible, you have the ability to go into your profile (/profile/yourName). Howerver, if you click View Your Profile, the url does update, but the page does not appear to load and therefore, you cannot see your profile.
Original Code:
<Link href={`/profile/${username}`}>
<button>View Profile</button>
</Link>
Current Solution:
if (router.asPath.includes('profile'))
router.replace({
pathname: '/profile/[username]',
query: { username: username },
}).then(() => router.reload())
else
router.push(`/profile/${username}`)
The solution provided works, but load times significally increases when compared to the "Original Code" block or when asPath does not include 'profile'. I think this is because I am using reload(). I want to know if there is another solution that will improve loading time.
Thank you

How to Specify the "Authorize" at the Area level with Razor Pages?

I am testing the WebApp authentication using simple cookie. The app has 2 areas: Public & Internal (see Area structure at the end of this post). Users are required to login when accessing all pages under the "Internal" area.
PROGRAM DESCRIPTION
The App is implemented with .NET 6 with Razor pages (without MVC). In the program.cs file, I tried to specify the "authorize" at the "Area" level for "Internal" area (see code segment here). The HTML 404.15 error occurred when testing the app (see HTML error in attached image below).
It looks like that "AuthorizeAreaFolder" works fine with any folder name inside of Internal area. Is there a way to setup "Authorize" at an Area level?
CONFIGURE AUTHORIZE
builder.Services.AddRazorPages(options =>
{
options.Conventions.AuthorizeAreaFolder("Internal","/");
});
HTML ERROR
WEBAPP AREAS
Make sure that your signing in page is accessible to anonymous users. An easy way to accomplish this is to add the AllowAnonymous attribute to the PageModel class of the relevant page e.g (assuming the signing in page is called Signin):
[AllowAnonymous]
public class SigninModel : PageModel

Cannot use same razor page for two addresses

I what to use the same razor page in more than one position in my web app.
I have the page /one and I want to reach also with /two and /three.
I can do it with the AddPageRoute :
options.Conventions.AddPageRoute("/one", "two");
options.Conventions.AddPageRoute("/one", "three");
But the taghelpers in the page always render formaction="/three?handler=Update" pointing to the last route, same for /one, /two or /three
This make impossible use the same razor page in different position in my web application.
Git sample : https://github.com/enricoe73/OnePage2Routes.git
You can reach the page using any of the routes you defined, but the outbound URL generation components in the framework (tag helpers, IUrlHelper etc) will always use the last route registered when generating an outbound URL, so the order in which the routes are registered plays a part.
If that doesn't suit you (because you may want to point to different routes at different times, for instance), the solution is to use the standard HTML formaction attribute instead of the custom asp-* attributes that belong to the tag helper:
<button formaction="/two?handler=update">Update</button>

Login to meteor set database redirect subdomain

I like to build a to do list for multi company on one server. I build a to do list, but now I like to "scale" to app to different "sub servers"
I like to explain it by this example
Start: example.com - There is a login button. When you enter your account you are redirect to a subdomain
company1.example.com Here is the to do list for company 1. This company uses his own database for example mongodb://localhost:27017/compagny1
When I direct go to company1.example.com without a login I will be redirected to example.com
I this possible or is there a other way to set user and database for meteor. I don't like to have multi servers for example: localhost:3001 localhost:3002 etc
Thank you for giving me a direction for this question
I would do something similar:
add user field to store company name.
(eg. user.company = company1)
use iron router and put a onBeforeAction to the page that user is redirected after login
onBeforeAction: function (pause) {
if (Meteor.user()) { // only if user is logged in
var comp = Meteor.user().company
if(comp == 'company1'){
this.render('company1'); // will render example.com/company1 if setup
}
// pause this rendering of the rest of the before hooks and the action function
pause();
}
},
Update: i do not think many databases would be great. I better recomend making separate collections for each companys todos and subscribe them individually.

How do I set push-state in durandaljs 2.0 the works on refresh?

I'm using durandaljs 2.0. I've installed the durandal starter-kit as suggested and explained here. In the shell I'm returning router.activate({ pushState: true } ); as explained in the relevant documentation (see the bottom of the page).
Happily, the URL is indeed in a 'push state' format, e.g. http://localhost:61285/flickr - the problem is that when I refresh the page I get a 404 error telling me "the resource cannot be found". If I set push-state to false ({pushState: false} above) I get a hashed URL, e.g. http://localhost:61285/#flickr - and now a refresh does work. So, how do I set up a push state mode in durandaljs 2.0 that will work with refresh?
Thanks,
Elior
Maybe to late...but
just change the routes config.
simple as this :
routes.MapRoute(
name: "Default",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" }
);
When you refresh the page, the browser will make a request to the server with that URL http://localhost:61285/flickr.
What's probably happening is that if you are using ASP.NET MVC, the server is trying to locate a Controller called flickr and it throws an exception because obviously there isn't any resource with that name.
In order to get rid of this exception you should configure the server to serve the same HTML of the APP but for unknown URL's. This can be achieved using IIS URL Rewrite in ASP.NET.
So after setting up properly the server, by requesting an unknown URL it would return the initial view for the app plus whatever you pass in the query string parameters so the router can do its job at client side.
In this blog post you will find more information about how to configure ASP.NET to handle this scenarios. In the article the author uses AngularJS, however it will be the same for Durandal.
RainerAtSpirit and margabit, you're both right, thank you. Here is how I implemented the server side:
First I should note that all the interaction with the server is done via WebApi controllers.
so, for example, if the URL is:
http://localhost:61285/home/category2/subCategory22 (for a localhost), the server tries to look for a controller called 'home' and an action in it called 'category2'. Since there's no such action, I get a 404 error.
What I wanted is that the server WILL call the 'home' controller, but send the rest of the URL as parameters to the client. My solution was to add a hash after the controller's name, so that the URL will look like this: http://localhost:61285/home/#/category2/subCategory22. If this would happen then the client will take care of the hashed part with no 404 error.
For this to happen:
I added the following to 'web.config':
<customErrors mode="On" defaultRedirect="Error">
<error statusCode="404" redirect="Error" />
</customErrors>
Then I create a controller named 'ErrorController' with the following class in it:
public class ErrorController : ApiController
{
[HttpGet, HttpPost, HttpPut, HttpDelete, HttpHead, HttpOptions, AcceptVerbs("PATCH"), AllowAnonymous]
public HttpResponseMessage Handle404()
{
string [] parts = Request.RequestUri.OriginalString.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
string parameters = parts[ 1 ].Replace("aspxerrorpath=","");
var response = Request.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Location = new Uri(parts[0].Replace("Error","") + string.Format("#{0}", parameters));
return response;
}
}
what happens is that when the server get a URL with no relevant action as I mentioned above, it redirects it to this controller in the following format: http://localhost:61285/Error?aspxerrorpath=home/category2/subCategory22
as you can see, I manipulate this to add the hash and remove the unnecessary info: http://localhost:61285/home/#/category2/subCategory22 and the redirect the server to the 'home' controller.
You might wonder why I do all of this - the reason is that Durandal, a wonderful platform, enables me to use push state, but in order for that to happen I have to work-around the server getting a non-hashed URL and pass it to the client despite the fact there's no relevant controller/action; Durandal get's a hashed URL but removes the hash automatically and the user eventually sees a hash-less URL while Durandal provides all the necessary push state functionality.
Elior