Django URL regex with variables - regex

Was hoping someone could point me in the right direction with this. I've tried nearly everything I can think of, but I can't seem to get it to work. I've got a set of URLs I'd like to match in Django:
www.something.com/django/tabs/
www.something.com/django/tabs/?id=1
Basically, I want to make it so that when you just visit www.something.com/django/tabs/ it takes you to a splash page where you can browse through stuff. When you visit the second URL however, it takes you to a specific page which you can browse to from the first URL. This page is rendered based on an object in the database, which is why the id number is there. I've tried to account for this in the URL regex, but nothing I try seems to work. They all just take me to the main page.
This is what I have in urls.py within the main site folder:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', include("tabs.urls")),
]
and within urls.py in the app's folder:
urlpatterns = [
url(r'\?id=\d+$', tab),
url(r'^$', alltabs)
]
Would anyone be so kind as to point me in the right direction? Thanks in advance!

You are not following the right approach here. Query paramers are used to change the behaviour of the page slightly. Like a added filter, search query etc.
What i would suggest is you have only one view and render different templates based on query parameters in the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tabs/', alltabs),
]
In your alltab views you can have something like this.
def alltabs(request):
if request.GET.get("id"):
id = request.GET.get("id")
your_object = MyModel.objects.get(id=id)
return render_to_response("tab.html", {"object":your_object})
return render_to_response("alltab.html")
Hope this helps

This is not the preferred 'django way' of defining urls patterns, I would say:-)
In the spirit of django would be something like
www.something.com/django/tabs/
www.something.com/django/tabs/1/
....
www.something.com/django/tabs/4/
and for this you define your url patterns within the app for example this way
tabs/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /tabs/
url(r'^$', views.index, name='index'),
# ex: /tabs/5/
url(r'^(?P<tab_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /tabs/5/results/
url(r'^(?P<tab_id>[0-9]+)/results/$', views.results, name='results'),
]
and something similar in your views
tabs/views.py:
from django.shortcuts import get_object_or_404, render
from tabs.models import Tab
def index(request):
return render(request, 'tabs/index.html')
def detail(request, tab_id):
tab = get_object_or_404(Tab, pk=tab_id)
return render(request, 'tabs/detail.html', {'tab': tab})
...
You can follow this django tutorial for more details:

Related

Change URL for wagtail blog index page

I currently have a website where the home page at www.mysite.com is the wagtail blog index page
I wish to move the blogindex page to another url
I can easily have a different homepage by amending my urls.py file:
#original
path("", include(wagtail_urls))
#new
path(
"",
TemplateView.as_view(template_name="pages/newhomepage.html"),
name="newhomepage",
),
However I would like the blogindex page available at e.g. myste.com/blog but I am not sure how to go about this. Adding the following to urls.py does not do it
path("blog/", include(wagtail_urls))
There are a couple of changes in project required to achieve this.
Firstly, create a urls.py file inside your specific app, this will be different from the one that you should already have alongside wsgi.py, asgi.py, etc. and it will only store app specific urls.
from django.urls import path
from .views import index
urlpatterns = [
path('test/', index, name='generalized-test-url'),
]
Now, you should render the template inside your view.
def index(request):
return render(request, 'pages/newhomepage.html', context)
Lastly, in your root urls.py file, you need to include the app urls. Lets say your app name is blog.
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
]

How to write a urls.py in django so that I can do something like */page

Here is the problem:
I have an app with the following models: project, position, outreach
A position is connected to a project and project only with a Foreign key
An outreach is connected to a position and a position only with a Foreign key
I can create a new project from almost anywhere in my app (same for the other objects). Currently I wrote that a new project is created from the url dashboard/newjobproject but I would to make it so that depending on the page that I am, the url simply becomes something like www.myapp.com/..../newproject
What's a way to write the urls.py to achieve that?
from django.urls import path
from action import views
app_name = 'action'
urlpatterns = [
# ex: /action/
path('', views.login, name='login'),
path('dashboard/', views.dashboard, name='dashboard'),
path('contacts/', views.contacts, name='contacts'),
path('projects/', views.project, name='project'),
path('contacts/newcontact', views.new_contact, name='new_contact'),
path('projects/newjobproject', views.new_outreach, name='new_outreach'),
path('dashboard/newjobproject', views.new_jobproject, name='new_jobproject'),
path('projects/<uuid>/newjobposition', views.new_jobposition, name='new_jobposition'),
]
However,
Try adding this to the bottom of urlpatterns:
path('<path:p>/newjobproject', views.new_jobproject, name='whatever-name-you-want'),
and in your views.py:
def new_jobproject(request, p):
Tbh though, this is sort of a hacky way to do it. It'll break in a few locations. If you have a main urlpatterns array in which you're including the urls for this 'action' app as APIs, this solution won't work outside the API urls.
For eg. if you have:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('action.urls')),
]
And you access your url like this -> www.myapp.com/api/v1/....../newjobproject, then the code will work. If you try to access www.myapp.com/..../newjobproject or www.myapp.com/admin/..../newjobproject then the code will break and it will not match any of the paths. I'm not sure how you'd get it to work in that case.
If the above scenario is not an issue, if you're not going to be using these views as APIs, and your urlpatterns looks something like this:
urlpatterns = [
path('admin', admin.site.urls),
path('/', include('action.urls')),
]
then the code should work for all urls except for the admin/.../newjobproject case.

Make arbitrary url as homepage

I have such a urls.py configuration in project repository "forum"
# Project url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
url(r'^article/', include('article.urls',namespace='article')),
]
and with article/urls.py as
#Article app's url config
urlpatterns = [
# show the article list
url(r"^list/(?P<block_id>\d+)$", views.article_list, name="article_list"),
I attempt to take "^article/list/1$" as home page instead of "views.index".
How to make it redirect to "^article/list/1$" when I issue request "127.0.0.1:8000"?
You can redirect from your views.index. In your views.py
from django.shortcuts import redirect
def index(request):
return redirect('article:article_list')
which should take you to article/list. You can include that block_id parameter in the redirect function.
try this
redirect(reverse('article:article_list', kwargs={'block_id':2}))
and make sure to add kwargs in function like this
article_list(request,**kwargs):
I assume you want to this to debug your article page.
You have two ways of doing this.
Either redirect your index view to article view
views.py
from django.shortcuts import redirect
def index(request):
#We force a redirect to article_list view, and we pass arguments.
#Notice it has to come first.
redirect ("article_list", article_list_argument_view=1)
#it will redirect to localhost:8000/article/list/1
'''
Your Home page code.
'''
Put the url directly before the include
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
#The order is important.
url(r'^article/list/1$', "article_list", {'article_list_argument_view':1})
url(r'^article/', include('article.urls',namespace='article')),
]

django view for http://127.0.0.1:8000/

I've already searched for my topic but the results were showed fixes for e.g. 127.0.0.1:8000/polls, 127.0.0.1:8000/app_xy etc. but nor for 127.0.0.1:8000 direct.
I want my Homepage show view-result e. g. "This is my homepage" if I call it with 127.0.0.1:8000
My idea is to edit my website's main-urls.py (myWebsite/urls.py) with the following line:
urlpatterns = [
url(r'^$', <no idea what to do here>),
url(r'^app2/', include('app2.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
I created a view (myWebsite/views.py) which contains:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello from the Homepage')
I had no problems with the views of the apps (app2, polls) and they work fine, but I don't know how to call the view above from the main-urls.py. Or what do you advise me to do?
I hope I explained my problem well enough.
Thx for your help.
As I'm a little advanced now, I am going to answer my question on my own:
The solution is to add the following to make an import of the views and put an URL to the project's urls.py:
from first_app import views
and add the urlpatterns:
urlpatterns = [
... ,
url(r'^$', views.index, name='index')
]
If you go to your browser and connect to http://127.0.0.1:8000/ you will see the result:
Hello from the Homepage
This is a simple respond of the index-function in the views.py. Instead of this you better create a template (e. g. home.html) and render this in the index-function.

I can't get my Django URLs to work

My code is as follows:
root urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('app.urls')),
]
My applications url is like so
urlpatterns = [
url(r'^$',views.login, name='login'),
url(r'^homepage/$', views.homepage, name='homepage'),
]
The first screen the user will see is the login screen (views.login). At the moment I just want to set the login button to be a url that takes them to the homepage (just for practice) but it doesnt seem to work.
The login html is like so
<button type="button">Log-In</button>
This should go to my urls page above...find the name 'homepage' and take me to views.homepage which is as so:
def homepage(request):
return render(request, 'application/homepage.html', {})
but my homepage doesnt get rendered and I have absolutely no idea why its driving me crazy.
Any help would be appreciated.
This is nothing to do with Django, but a pure HTML problem. You can't put a link inside a button. A button needs to be part of a form, and submits to the action value of that form. Either do that, and take the a tag out; or, remove the button, and just use the a.