Django admin page loses styling when travelled to from link - django

When I travel directly to the admin page of my django website by entering http://localhost:8000/admin/ in my address bar, the admin page is styled as it should be.
However, I need to create a link in my navigation bar that takes the user to the admin page but when the admin page is accessed from this nav link the normal admin styling disappears even though it appears to take me to the exact same web address.
[1]: https://i.stack.imgur.com/MrdOF.png
When I inspect the site it instead seems to be recieving styling from my jquery mobile stylesheet instead.
Any idea why this may be the case only when travelling to the admin address from the link?
EDIT:
Here is the html code for the nav. The result is the same regardless of using href='/admin/' or href="{% url 'admin:index' %}"
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'admin:index' %}">{% trans 'Staff Login' %}</a>
</li>
</ul>
Here are my urls.py files for each of my apps:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='form_page'),
path('search/', views.search_page, name='search_page'),
path('list/', views.list_skills, name='list_skills_page'),
path('skill/<slug:code>/', views.show_skill, name='show_skill_page'),
path('select/<slug:code_1>/', views.select_second, name='select_second'),
path('select/<slug:code_1>/<slug:code_2>/', views.view_second, name='view_second'),
path('language_preferences/', views.language_preferences_page, name='language_preferences_page')
]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('Generator.urls')),
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]
Also, I have noticed that when inspecting the source in my browser the admin static files do not appear in the page sources when viewing the admin site after clicking the anchor link (left) but are present if I directly enter the admin address in my search bar (right).
see here

SOLVED:
I found that some jquery js found in my base html template seemed to be interfering with the admin static files and stopping them from being served when I navigated to the admin site from the link in the navigation bar (no idea why!)
removing the following line fixed the admin style issue when navigating from the link:
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
so I simply wrote a javascript script to remove this jquery resource from the DOM when the link is clicked and the issue was resolved.

Related

NoReverseMatch Unless Url Is In Main Project Urls.py

I have a project called 'my_project' and within that project I have an app called 'my_app' so I have two urls.py files. All of my url's for my_app are located within it's urls.py file and work correctly, except one. That one is 'download_file'. My site works when this is included in my_project's urls.py, but when it's in my_app's urls.py I get a NoReverseMatch error on page load.
I don't know why this url only works when it's located in my main projects url's folder. I suspect it has something to do with the regex, though I can't figure it out.
The user would be on this page:
http://127.0.0.1:8000/user_area/username/classes
then click the 'download' link:
<a href="{% url 'download_file' file_path=item.instance.user_file %}" target='_blank'>{{ item.instance.filename }}</a>
my_project.py
urlpatterns = [
# reference to my_app
re_path(r'^user_area/(?P<username>[\w-]+)/', include('my_app.urls')),
]
# this works
url(r'^download_file/(?P<file_path>(.+)\/([^/]+))$', users_views.DownloadFile.as_view(), name='download_file'),
]
my_app.py
urlpatterns = [
path('classes', views.classes, name='classes'),
# if I remove the url from my_project.py this one returns NoReverseMatch on page load
url(r'^download_file/(?P<file_path>(.+)\/([^/]+))$', users_views.DownloadFile.as_view(), name='download_file'),
Thank you.
The problem is occurring because your URL template tag is providing only one parameter: file_path.
This works when the URL is declared in your project urls.py, because only one parameter is needed.
When you try to use the URL in my_app.urls, you need to also provide the username parameter. You will need to use something like:
<a href="{% url 'download_file' username=request.user.username file_path=item.instance.user_file %}" target='_blank'>{{ item.instance.filename }}</a>

Multiple auth login pages in Django

I have multiple apps in one django project
/user
/manager
/business`
Each needs a separate set of login and registration. How do I use django.contrib.auth to satisfy this?
I have urlpatterns in main are:
urlpatterns = [
path('admin/', admin.site.urls),
path('user/', include('user.urls')),
path('user/', include('django.contrib.auth.urls')),
path('manager/', include('manager.urls')),
path('manager/', include('django.contrib.auth.urls')),
path('business/', include('business.urls')),
path('business/', include('django.contrib.auth.urls')),
Urlpatterns in the apps are like those:
urlpatterns = [
path('index', views.index, name='index'),
path('register', views.register, name='register'),
]
and I have different views for login and register, also have different templates in each app: /templates/register/register.html and /templates/register/login.html
However, login and register views seem to be shared between apps. Is there a way to separate them with ease?
First of all I would suggest you make use namespaces in your urls so you don't get any conflicts with the names in your urls. So add app_name to the urls.py of your apps, e.g.
#user urls.py
app_name='user'
urlpatterns = [
...
Then something like {% url 'user:register' %} will point to register view of your user app, {% url 'manager:register' %} will point to the register view of your manager app and so on.
If you have defined individual login views for each app the same goes for these views too, providing you have imported the correct views in you apps urls.py.
As you are also including the django.contrib.auth.urls in each of your apps there is of course a second 'login/' path coming from the default path in the auth.urls. However, as far as I know the resolution of urls is done top down by django, so if you keep the order of urls as in your post your custom 'login/' will be hit first and used. So no problem with that.
Where I see a problem is with your templates. If I understand it correctly all are located in a templates/register/ folder in your apps. The django template loader does not differentiate between a register.html template in your user app and a register.html template in your manager app. So it is likely that you do not get the correct template. What I would suggest is moving the templates to an app specific subfolder, e.g. /templates/user/register. Then you can fetch the correct templates in your views (e.g. 'user/register.html').
I understand that you have individual app specific views. Of course you can always subclass the views django provides and adapt them to your needs. For instance by overriding the form_valid() method of the LoginView. This SO post provides an example.
If on the other hand you want to use the default django login view just with a custom template you can pass the template as a kwarg to your view. E.g.:
# user urls.py
from django.urls import path
from django.contrib.auth.views import LoginView
app_name='user'
urlpatterns = [
path('login/', LoginView.as_view(template_name='user/login.html'), name='login'),
# more patterns
]
If you want to redirect to some custom site after successful login you can add an input called 'next' to your login form containing the url to where to redirect, e.g.
<!-- user/login.html -->
<form action="{% url 'user:login' %}" method="POST">
{% csrf_token %}
{{ form }}
<input type="hidden" name="next" value="/user/index/">
<button type="submit">Login</button>
</form>
In your apps you will propably be using the user_passes_test decorator or something similar to check whether the user is allowed to access the view in that app.

How do create url to django Admin within my user page (HTML)

I'm new to coding, I'm trying to create URL link in NavBar to redirect my Django Admin exmple:
<a class="nav-link" href="{% url 'admin'%}"> Admin site </a>
However, Django do find; No-Reverse-Match,
It works for other URL links
Things I had to try:
Is adding a name in project URL
including URL path in my App URL
My project URL code:
from Django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(r'admin/', admin.site.urls, name='admin' ),
path(r'accounts/', include('django.contrib.auth.urls')),
path(r'api-auth/', include('rest_framework.urls'), name='rest_framework'),
path(r'', include('app.urls')),
My App URL code:
from django.urls import path, include
from . import views #function views
from django.contrib.auth.decorators import login_required, permission_required
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'tank', views.TankViewSet)
router.register(r'room', views.RoomViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path(r'',login_required(views.index), name='index'),
path (r'test/',views.Test),
path(r'^Water/',login_required(views.Lquid), name='tank'),
path(r'^Ambient/',login_required(views.Ambient), name='room'),
path(r'Rest-api/', include(router.urls)),
#path(r'admin/', admin.site.urls, name='admin' ), this somthing I had try
]
Basically, Django makes in a way that works and redirect to the URL link on any page
NAVBAR:
Admin site
App URL:
path(r'',login_required(views.index), name='index'),
With this two link it will redricet to the page you set.
Apprentice if you cold help
You need to specify the admin page you want to redirect to when doing a reverse lookup of admin. To get to the admin homepage you will want this reverse lookup
reverse('admin:index')
So to link to the admin homepage in your nav bar, it should look like this
<a class="nav-link" href="{% url 'admin:index' %}"> Admin site </a>
Check out the docs on how to reverse lookup admin pages

Django url configuration error

so I have created a Django application with three html pages. I want from a page which is not the homepage to another. However, the there seems to be some mix up in the url when the server tries to access the latter page.
Here's my application urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow.html/$',views.findnow,name='findnow'),
url(r'^more.html/$',views.more,name='more')
]
I wish to go from "findnow.html" to "more.html". I want the url to be "localhost:port/more" but instead the server goes to "localhost:port/findnow.html/more.html".
Here's my html code snippet for findnow:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
Here's my views.py:
def more(request):
return render(request,'myapp/more.html')
So adjust it to be:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.homepage,name='homepage'),
url(r'^findnow/$',views.findnow,name='findnow'),
url(r'^more/$',views.more,name='more')
]
And your template:
<body>
<div id="googleMap" style="width:500px;height:380px;text-align:center;"></div>
MORE
</body>
</html>
This is happening because you are linking more.html as a relative page.
You should either prepend a / to it:
MORE
Or reverse it using the url tag instead:
MORE

Django image only showing in some pages

I have some problems in django,
I can load images in my index.html, but not in other templates,
why is this happening?
Tried many methods and viewed many other post but non work for me.
This is my urls.py
urlpatterns = patterns('',
# Examples:
url(r'^$', 'userboard.views.home', name='home'),
url(r'^items/(?P<UserBoard_id>\d+)/$','userboard.views.details',name='details'),
url(r'^upload.html$', 'userboard.views.upload', name='upload'),
url(r'^items/(?P<UserBoard_id>\d+)/delete.html$','userboard.views.delete',name='delete'),
url(r'^items/(?P<UserBoard_id>\d+)/edit.html$','userboard.views.edit',name='edit'),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
This is my index.html, it works fine
<h1> Your Items: </h1>
<ul>
{% for items in ItemList %}
<li><a href="/items/{{items.id}}/">
<img src="{{items.itemImage}}"></a>
</li>
<hr />
{% endfor %}
</ul>
Add a new item
but this wont load my pictures
<img src="{{item.itemImage}}">
<h1>{{item.itemName}}<h1>
<h2>{{item.itemDesc}}</h2>
<br /><br/><br/>
Delete this item
<br />
Edit this item
i am not sure if you need my views, here they are
def home ( request) :
ItemList= models.UserBoard.objects.all()
return render_to_response('index.html',{'ItemList':ItemList})
def details(request,UserBoard_id):
try:
item=models.UserBoard.objects.get(pk=UserBoard_id)
except models.UserBoard.DoesNotExist:
raise Http404
return render_to_response('details.html' ,{'item':item})
Please let me know where I went wrong or at least give me a hint where I could start searching on.
EDIT:
here are my models
from django.db import models
class UserBoard(models.Model):
itemName=models.CharField(max_length=100)
itemDesc=models.CharField(max_length=400)
itemImage=models.ImageField(upload_to='../media/pictures')
if i print out the value of items.itemImage, it will show this ../media/pictures/d.jpg
Same value in both pages, but image will only show on 1. Templates for both pages are at the same folder.
And here is my settings.py media root
MEDIA_ROOT = '/home/rox/Documents/IP/userboard/media/'
MEDIA_URL = 'media/'
PROBLEM SOLVED
THANKS A LOT to little_birdie and the others out there who helped.
The problem was this . I didn't put the '/' infront of item.itemImage previously.
However, why is it that without the / it works in the index.html but not the others?
Not quite enough information but I think I know what's going on here.
You are getting a relative path from item.itemImage, that is.. it does not start with a slash, so the browser will load it relative to the url of the page the image is on.
Apparently you stored the full url path to your image in your image field, eg:
media/foo/myphoto.jpg
When loaded from / (home page) becomes '/media/foo/myphoto.jpg'.. which works.
The bottom line is, do this:
<img src="/{{item.itemImage}}">