Is it possible to set 2 urls in get_absolute_url? - python

I have next task and dont know how to make it.
Web page shows to user list of objects. Every object has 2 button with urls like function_detail and function_change_history. First url need open the page about details of the object and the second url need to open the page about change history. I used get_absolute_url to make second url but dont know how to create first url cause I need to use get_absolute_url again.
models.py:
#reversion.register()
class Function(models.Model):
***FIELDS***
def get_absolute_url(self):
return reverse('project:function_change_history', args=[self.project_id, self.code])
urls.py:
url(r'^(?P<project_code>[0-9a-f-]+)/(?P<function_code>[0-9a-f-]+)/change_history/$',
function_change_history,
name='function_change_history'),
url(r'^(?P<project_code>[0-9a-f-]+)/(?P<function_code>[0-9a-f-]+)/detail/$',
function_detail,
name='function_detail'),
Is it possible to set 2 url in get_absolute_url?

Finally I found the decision. For function_detail I use get_absolute_url and for function_change_history I use in template {% url 'function_change_history' project_code=project.code function_code=function.code %}

Related

Displaying the content on a different html based on the primary key

I am using Django to create a site. I have Table called notice as below
Models.py
class Notice(models.Model):
Notice_No=models.AutoField(primary_key=True)
Content=models.TextField(max_length=5000, help_text="Enter Owner Name")
I am using a for loop to display the all these fields on my template page as
Notice No, Content, Date of issue, Date of expiry. I have provided a hyperlink to the all the content values which take it to another HTML play with a proper notice formal of a CHS. Now what I wanna do is if I click on let's say notice of notice no-1. I only want to display the content of that notice on the next page. If I click on notice _no 2, it should only display the contents of that notice. I'm new to python so not sure how to do this. How do I go about this?
Notice.html is the page that displays the table. Noticesoc.html display is where the content should be displayed.
views.py
def notices(request):
Notice_all=Notice.objects.all()[:50]
return render(
request,
'notices.html',
context={'Notice_all':Notice_all}
)
def noticesoc(request):
Notice_all=Notice.objects.all()
return render(
request,
'noticesoc.html',
context={'Notice_all':Notice_all}
)
Send the primary key of the data you want to see in detail.
<td style="color:white; font-family:helvetica; font-size:15px;">
<a class="nav-link" href="{% url 'noticesoc' Notice.pk %}">
Click here to view the contents</a>
</td>
url( r'^noticesoc/(?P<pk>\d+)/$', 'noticesoc')
Then in the view. Use .get to get the information Notice and then render it.
Ex:
def noticesoc(request, pk):
Notice=Notice.objects.get(id=pk)
return render(
request,
'noticesoc.html',
context={'Notice_all':Notice}
)
in reference to this ....
Still doesn't work. Error=local variable 'Notice' referenced before assignment. Can you see my url.py(previous comment) and see if it's correct
you will notice that the model name and the function name are similar hence instead of django differentiating the two it assumes them as similar with the first code to run as the model then the view function
try to use another name for your view function and call the name different and unique from the model name for instance
def noticesoc(request, pk):
Notice=Notice.objects.get(id=pk)
return render(
request,
'noticesoc.html',
context={'Notice_all':Notice}
)
use this instead
def noticesoc(request, pk):
note=Notice.objects.get(id=pk)**
return render(request,'noticesoc.html',context={'note':note}
emphasized text

Why is my django urlpattern not resolving?

So I'm creating a small ecommerce website for a friend and cannot work out why the url won't resolve itself.
For the sake of simplicity whilst making the website I'm using a product called "number 1". It has a slug field of "number-1" and on the product page clicking on the product takes a user to "/shop/number-1"
My url pattern for this is:
url(r'^<slug:url>', views.item, name='products')
with the view:
def item(request, url=""):
products = product.objects.get(url=url)
return render(request, 'shop\product.html', {'products', products})
As far as I can tell this should render my product.html template but instead it returns a 404 and I'm not sure why?
If it helps I have other views, such as product types set within the same views and they work fine so as far as I can tell its that the slug:url isn't being used in the views.item, or the view isn't getting the context properly.
Also I'm on django 1.11.7 for this project.
The url pattern you are trying to use (slug:url) is only valid in Django 2.
If you are on Django 1.11 then you need to use a regular expression - something like this:
url(r'^?P<url>[\w-]+', views.item, name='products')
Always make sure you're looking at the documentation for your version of Django ;-).
Please change url pattern to:
url(r'^?P<url>[\w-]+', views.item, name='products')

Django 1.10 - get site base url

How do you get base url of a site?
like - https://stackoverflow.com/
I would love to set it to settings.py
Thanks
Answer:
Really very sorry for such a poor question. :(
I was trying to link up a post by following code -
{{ related_question.question_text }}
it took me from current page url but i expected from base url. I thought, i would get a solution like site_url() like Wordpress and could use it in settings.py.
But i don't have to do that. Starting href with /.... starts from base url. So, above <a> tag should be -
{{ related_question.question_text }}
That's it.
I recommend you to write your own template context processor which will return a SITE_URL from your settings file.
See https://docs.djangoproject.com/en/1.10/ref/templates/api/#writing-your-own-context-processors
Your function simply has to return a dict such as {"SITE_URL": settings.SITE_URL} and then all your templates will have "SITE_URL" in the context.
Another option would be to add a method to the question model e.g. get_url() which returns a site url with a slug and then use related_question.get_url inside your template.
And you should probably use a "url" tag in your templates, such as {% url 'question' related_question.id %} instead of hardcoding the URLs.

Django/Python: How to create redirect from one view/url to another?

Django 1.8 / Python 3.4
I have a website.html that displays entries from my database, each of which is identified by its ID. Now, at the end of each displayed entry I have a link to an "edit" view, looking like this:
<td>edit</td>
The link is working fine and leads to the correct view:
def edit(request, object_id):
implemented in views.py. There some code is executed correctly too, and in the view's last line I have:
return redirect('website.html')
Obviously, after the chosen entry has been edited I want my website.html with the edited entry being displayed in the browser again: 127.0.0.1:8000/website/. However, what happens is that I get a Page not found (404) error, with the information:
Requested URL 127.0.0.1:8000/website/2/website.html
The "2" here is the ID of the entry.
Maybe I have the wrong idea about how redirects work, but I was assuming that instead of calling the respective view's url from url.py it would open the url provided in the redirect() function?!
What happens, though, is that this url gets appended to the view's url!
Here is my urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from www.list.views import website, edit
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^website/$', website, name="website"),
url(r'^website/(?P<object_id>\d+)/$', edit, name="edit"),
]
I'm pretty sure the third url entry is causing the problem but I have absolutely no idea how to change it for the redirect to work. Also, the edit view doesn't have an actual website (template), but I'm supposed to provide a URL for it, so this was the best I could think of.
The way this should work is: Click the "edit" link on website.html, the code from the edit-view is being executed, and, afterwards, the website.html with the change in the database entry gets displayed again.
^^ How to achieve this? Any help is appreciated!
Redirect uses names or absolute URLS. You should either use the name of your URL:
return redirect('website') # since name="website"
or an absolute URL, like:
return redirect('/website/')
you can use the reverse function instead of redirect
from django.core.urlresolvers import reverse
return reverse('website')
I found the mistake and the solution:
At the end of the edit-view it's correct to write "return redirect('website')". However, just as I assumed, the URL of edit in urls.py was wrong.
Instead of
url(r'^website/(?P<object_id>\d+)/$', edit, name="edit"),
it should just be
url(r'^(?P<object_id>\d+)/$', edit, name="edit"),
Thank you nonetheless!

How can i remove the GET parameters from url in django

I have the form which i am showing by normal view. Then i am send the GET parameters to djnago ChangeList view like django does for lookups like this
student/?region__id__exact=1&status__exact=Published
now is there any way to remove that from the URL in the address bar.
I don't users to see what i am doing
The whole point of GET is that they are retrieved from the URL itself, removing them from the URL removes them entirely.
If you want them 'hidden' you will need to use POST.
The HTTP GET method of form submission passes the information from template to views through URL. If you want to "hide" information from URL use POST instead. In your form do like this:
<form action="/view_name/" method="post">
and in views:
request.POST['name']

Categories