Django url conf - django-urls

When user logs in, he is redirected to /accounts/loggedin/ . Loggedin is the simple view that renders logged_in.html template which has menu on it.
Example of the menu item:
<li><a id="n-rsvp" href="rsvp/" title=""></a></li>
Rsvp is the app I created. The question is what should I put inside of template so after clicking on RSVP on the menu user redirects to the "global" rsvp url, so instead of looking like this:
http://127.0.0.1:8000/accounts/loggedin/rsvp
url should be looking like this
http://127.0.0.1:8000/rsvp

my first guess would be href="/rsvp/"

Related

How to manage a list elements staying on the same page

By default, rails has controller CRUD actions with corresponding routes. For instance, I have a simple bookstore app, where I have a book and category. I want to display all categories through a sidebar on a home page. Each category is a dropdown element accompanied with a books count integer. When expanding a dropdown I want to see a book title that belongs to a current and acts as link. That is easy to implement.
The question is how can I add a form hidden under, let's say a + sign, sticked next to a static Dropdown section title "Categories" if there are none?
A form for Create action in Category controller which would allow to create a new category and display it straight away without redirecting to Create category view and redirecting back.
Please advise any reliable solution or tutorial link. Thanks a lot.
It looks like you are looking for an AJAX form. In rails you can generate this with form_for ... remote:true. This can call a controller method with out refreshing the page. You can then return a response and use JS to update the page the user is on.
For example, in your view
<div class="hidden">
<%= from_for #category, remote:true %>
Your form here
<% end %>
</div>
Add in some JavaScript to un-hide the form when your button is clicked. In your controller
def create
#normal create stuff
if save
respond_to do |f|
f.js
f.html {#re render page just in case JavaScript is disabled}
end
else
#handle error
end
end
using the rails default f.js, Rails will call a file create.js.erb this file will have access to any public variable you make, for instance #category.
you can then do somthing along the lines of
$('.append_category').append('<%= j render 'your category layout partial', locals: { category: #category }%>');
or if you just need the category link
$('.append_category').append('<%= link_to #category %>');
A very nice guide can be found here http://guides.rubyonrails.org/working_with_javascript_in_rails.html
Note: depending on your version of rails, you may need to add include ActionController::MimeResponds to your application_controller.rb (I know this is required in rails 5)

How to redirect using ActionLink to specific View tab region in ASP .NET MVC

I need to make an ActionLink that redirects to the previous page, but the previous page is divided in 5 tabs. So for the URL say
host/Admin/AdminMain
which has the tabs, when I hover over tabs the link appears to be
host/Admin/AdminMain#tabs-1
host/Admin/AdminMain#tabs-2
and so on.
In tabs-2 I have a link to another View, like host/Admin/SomeController, and there I need to create the ActionLink in the View that redirects me back to tabs-2, instead of tabs-1 as it does. The ActionLink right now looks like this:
#Html.ActionLink("Back to Main", "Index", "AdminMain")
How can I specify to redirect me to #tabs-2?
The #tabs-2 portion is called the "fragment" of the URL. It's not actually part of the URI for the resource, and is only used client-side to "jump" the user to portion of the document with that id.
However, something like tabs require JavaScript to function, and the browser cannot automatically switch to a tab based on the fragment, because it doesn't know how to do that. You'd need to write some JavaScript read the fragment and then attempt to activate the appropriate tab, using whatever library you're using for that.
(function () {
var fragment = location.hash;
if (fragment !== '') {
// activate tab with that id
}
})();

What set an anchor tag href to scroll to a div in all URLs like "/" and "/Home" and "/Home/Index" without redirecting in MVC

I need to set HREF attribute of an anchor tag to work in all URLs like "/" and "/Home" and "/Home/Index" to scroll in top of a div.
the problem is if i set that to "/Home/Index#ToTop" and someone click on that anchor tag, he/she will redirect from "/" or "/Home" to "/Home/Index#ToTop" that is not very nice!
any idea?
If you just want to scroll to the top <a href="#"> will bring you back to the top of the page.
If you want to scroll to an element that's somewhere on the page use <a href="#id-of-div"> so without the /Home/foo/bar stuff.
See also: What is href="#" and why is it used?
Edit based on comment
So you want the link to scroll to top of home page if you're still on the homepage without redirect, and you want the link to redirect you to home page and scroll to top of it, if you're not on the home page?
I think you've to create a different link depending on the page you're on.
So if user is yoursite.com/home, the link will be <a href="#id-of-div"> and if user is yoursite.com/foo/bar the link will be <a href="./Home/Index#id-of-div">
You can determine the current URL in the controller. Quick search on SO gives me:
How to get current page URL in MVC 3
In .NET MVC, is there an easy way to check if I'm on the home page?
It's been a while since I've worked with MVC, so I can only help with some old memories and quick searching
Anchor Tag Helper in ASP.NET Core
You can use "asp-fragment"
<a asp-controller="Speaker" asp-action="Evaluations" asp-fragment="SpeakerEvaluations">Speaker Evaluations</a>
The generated HTML is:
Speaker Evaluations

Connecting An <a> tag to a ViewController mvc

I know this is probably straight forward, but this isnt working for me, I want to simply connect this to trigger a viewController when I click it. When I create a controller and a view and click it it throws a 404 and the code never hits the controller.
<div> All Accounts In the System</div>
You should probably follow few tutorials for mvc. Links are by default using System.Web.Routing magic (if you have default template, look into global.asax). Default url looks like /controller/action/id where id is optional, action defaulted to Index and controller defaulted to Home. For link construction in views use these helper methods:
#Html.ActionLink("All accounts in the system", "AdminReports", "Admin");
or
All accounts in the system
The link should look like /Admin/AdminReports with default settings.
You want something like this:
<a href='<%= Url.Action("Controller", "Action") %>'>Text</a>
where
Controller = Your controller name.
Action = Your action method.

Changing menu links, if user is authenticated or not

I need to change the menu links on my website (and leave the same items names) depending on the user is a guest, or authenticated user.
What's the standard way to do it ?
thanks
You cannot dynamically change a menu item's path, because menu items are cached.
Still, AFAIK, there are two ways to get what you want. Both methods require you to create your menu items with hook_menu in a custom module (not from the Menu UI).
The first method is to create two menu items with identical names and set the access rules so that one is only available for logged guests, the other for authenticated users. Since Drupal will only show menu items that the user is allowed to access, only one will show up at any given moment. In Drupal core, you can see how the user module creates a menu item for anonymous users by looking at the /user/login path in user_menu().
The second method is to create a single menu item and check in the menu callback if the user is logged in. If the user is logged in, you serve one page, if not you serve another. In Drupal core, the /user path works like this. See user_page to see how the code works.
You can dynamically change a menu item's path - see hook_translated_menu_link_alter.
This hook is called before every menu item is rendered IF it has the property ['options']['alter'] = TRUE.
You can set this property to menu items using hook_menu_link_alter.
Example code would be:
function MY_MODULE_menu_link_alter(&$item) {
$item['options']['alter'] = TRUE;
}
function MY_MODULE_translated_menu_link_alter(&$item, $map) {
if($item['mlid']==89) {
$item['link_path'] .= 'my-new-path';
}
}
Instead of altering the link, you coudl create the menues twice: once with the links for regular users and once with the links for registered/admin/... users
You can put a menu into a block and set it to only allow registered users to see the one block and non-registered users the other block. Either by selecting the proper radio button from the Drupal menu within the block creation form or via PHP that will evaluate and depending on it's return value (TRUE/FALSE) displays it. I suggest to go with the first approach.
You can change the menu by using a combination of nodeaccess module and linking to the corresponding pages.
For example, by default guest users cannot access /logout. If you create a link in a menu to logout, it will only display if a user is logged in. With nodeaccess, simply create a node, visit the grant tab and uncheck/check "authenticated users" or "anonymous users" for that node.
http://drupal.org/project/nodeaccess
Cheers,

Resources