Building a link shortener in Django [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am using Django 1.9 to build a link shortener. I have created a simple HTML page where the user can enter the long URL. I have also coded the methods for shortening this URL. The data is getting stored in the database and I am able to display the shortened URL to the user.
I want to know what I have to do next. What happens when a user visits the shorter URL? Should I use redirects or something else? I am totally clueless about this topic.

Normally when you provide a url shortner, after calling the url, you have to redirect to main url by 301 Permanently moved.
def resolve_url(request,url):
origin_url=resolve(url) # read from redis or so.
return HttpResponseRedirect(origin_url)
EDIT:
add code using #danny-cullen hint

You could just navigate to the URL via HttpResponseRedirect

Write a middleware instead of writing same code in every view, such that, if the shortened url is in the model that you stored the you can redirect the shortened url to the long url using HttpResponseRedirect.
class RedirectMiddleware(object):
# Check if client IP is allowed
def process_request(self, request):
'''you can get the current url from request and just filter with the model and redirect to longurl with HttpResponseRedirect.'''
return HttpResponseRedirect(full_url)

Related

Why does Flask stop taking input for a dynamic url as soon as it reaches a '?'? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to build a web application that takes in as arguments, the youtube watch ID or something, for example: 'watch?v=[something random here]'.
Whenever I try to get this as a variable using the < > signs, in the #app.route decorator, the value that the variable stores, stops at the '?' (exclusive). Is there some way I can get this right? I want to take the whole 'watch?v=[random]' in my variable.
You are kind of confused as to what you are asking. Anyway there are two ways about it. If you are willing to accept dynamic values from the route, you can use request from flask.
Here is an example:
Solution 1
from flask import request
#app.route("/watch")
def home():
page = request.args.get('v')
# use the 'v' in what you want to do
return # what you want
Solution 2
Otherwise,
this is what you can do:
from package.file import Something
#app.route("/post/<int:post_id>")
def post(post_id):
post = Something.query.get_or_404(post_id)
return render_template('post.html', title=post.title, post=post)
The above part has a particular input defined. The dynamic part of the route does not start with the '?'
Guessing by your confusion, the first solution should work for you!
I am not sure what you exactly are referring to, adding some code would be helpful. But when it comes to getting the query string you want to look into the request object in flask that can return information about the URL. Hope this helps and add some code if possible.

Web Scraper Using django (python for web) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It's been a month or so, yeah?
I've been searching the web for web scraping using python and I have found beautifulSoup and lots of other scraping tools such as Scrapy, scraper, etc.
All of them are the same...a little differences there might be..
Most tutorials I watch or read, are the same to...
Okay, what I am trying to do here is the following:
Except of putting the URL that I wanna scrape into the code...I want the USER to INPUT the url and then the scraper scrapes that url that the user has pasted in the HTML field
All tutorials have the code like this:
url = (http://......)
No...I want it somehow like this:
url = (USER INPUT)
Example video:
Link scraper
It's funny how he didn't actually to this in his tutorials...I think?
but yes, that's what I am trying to do, if you have any tutorial or documentation of doing this, please please help me out!!
Thank you!
If your using django, set up a form with a text input field for the url on your html page. On submission this url will appear in the POST variables if you've set it up correctly. Then in your back end, where you handle the url that was POST'd, grab the user's input url.
see https://tutorial.djangogirls.org/en/django_forms/ if you don't know how to set up a form.
in your view
import requests
from bs4 import BeautifulSoup
create a form from where the user will post the url for scrapping
in the specific view function
url = form.cleaned_data.get['name of the input field']
data = requests.get(url)
and then do what you need to do with your scrapped data

How to write python wrapper for web interface? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
If I want to give input to the website through python program and display the result on terminal after online computation then how can I do it using python wrapper? As I am new to python, so Can anyone suggest me some tutorial for this?
It all depends on the website that you want to retrieve your result from, and how it accepts input from you. For example, if your webpage accepts GET or POST requests, then you can send it a HTTP request and print out the response onto terminal.
If your website accepts input via a submit form, on the other hand, you would have to find the link of the submit button and send your data to that page.
There is a Python library called Requests, which you can use to send HTTP requests to a webpage and get the response. I suggest you read its documentation, it has some good examples that you can base your idea off. Another library is the inbuilt urllib2, which would also work for your purposes.
The response to your request is most likely to be a HTTP webpage, so you may have to scrape out your desired content from inside that.

Information regarding urls.py [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to Django, I have started learning just a week ago. I am taking the reference of Django Docs, but I can't understand how to give url to an app. So can anybody please tell me how to give url to an app with description?
for example:- If I want to make a login form and when I click on login button it goes to the home page.
Django is a web framework
A web framework carry out common works for you(the web developer)
One of the common works is resolving the http request
One of the resolving is direct the request pointing to a URL to a specific piece of code which handles this request and generate the response.
We sometime call the stuff carrying out the above mechanism a URL router of the web framework. The piece of code in Django is a function(which generate a http response), we call it the view.
So we say the framework's URL router map a URL to a view.
You tell the router what URL maps to what view by listing your url-view mappings in the solo urls.py in the root directory of your project
If you dislike the solo urls.py being too large, you can split this file into multiple files, and put them in individual Apps directory. Then by using include syntax you aggregate them into the solo urls.py

Why is my code shown as messy while it isn't? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
class sss(webapp.RequestHandler):
def get(self):
url = "http://www.google.com/"
result = urlfetch.fetch(url)
if result.status_code == 200:
self.response.out.write(result.content)
When I change code to this:
if result.status_code == 200:
self.response.out.write(result.content.decode('utf-8').encode('gb2312'))
It shows something strange. What should I do?
When I use this:
self.response.out.write(result.content.decode('big5'))
The page is different with the one I saw Google.com.
How to get Google.com that I saw?
Google is probably serving you ISO-8859-1. At least, that is what they serve me for the User-Agent "AppEngine-Google; (+http://code.google.com/appengine)" (which urlfetch uses). The Content-Type header value is:
text/html; charset=ISO-8859-1
So you would use:
result.content.decode('ISO-8859-1')
If you check result.headers["Content-Type"], your code can adapt to changes on the other end. You can generally pass the charset (ISO-8859-1 in this case) directly to the Python decode method.
how to get google.com that i saw ?
It's probably using relative URLs to images, javascript, CSS, etc, that you're not changing into absolute URLs into google's site. To confirm this: your logs should be showing 404 errors ("page not found") as the browser to which you're serving "just the HTML" tries locating the relative-addressed resources that you're not supplying.

Categories