title: If Content-Type is image-like do something, elif is text/html then do something else. How to do this?
body: <p>first of all, I'm apology for the title. I dont know how to explain this so i write the title like this.</p>
  
  <p>I want to check the Content-Type and if it is image/ run these code</p>
  
  <pre><code>from PIL import Image
  import io, uuid, requests
  
  class LinkCreateView(CreateView):
      model = Link
      form_class = LinkForm
  
      def form_valid(self, form):
          headers = {'content-type': 'image/'}
          r = requests.get(form.instance.url, headers=headers)
          i = Image.open(io.BytesIO(r.content)).convert('RGB')
          image_io = io.BytesIO()
          try:
              crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65, 65), bleed=0.5, centering=(0.5, 0.5))
              hash = str(uuid.uuid1())
              name = "{}.jpeg".format(hash)
              crop_image.save(image_io, format="jpeg")
              form.instance.img.save(name, ContentFile(image_io.getvalue()))
          finally:
              image_io.close()
  
          f = form.save(commit=False)
          f.rank_score = 0.0
          f.submitter = self.request.user
          f.save()
  
          return super(CreateView, self).form_valid(form)
  </code></pre>
  
  <p>If it is text/html then run these code;</p>
  
  <pre><code>from PIL import Image
  import io, uuid, requests, lxml.html
  
  url = ("http://website.com")
  page = requests.get(url)
  hash = str(uuid.uuid1())
  tree = lxml.html.fromstring(page.content)
  og = tree.xpath('/html/head/meta[@property="og:image"][1]/@content')
  r = requests.get(og[0])
  i = Image.open(io.BytesIO(r.content)).convert('RGB')
  crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65,65), bleed=0.5, centering=(0.5, 0.5))
  crop_image.save("{}.jpeg".format(hash))
  </code></pre>
  
  <p>I tried if, else things but it didn't work. I dont know how to implement this second piece of code to first one. Both code work perfectly by itself.</p>
  
  <p>I tried like these code:</p>
  
  <pre><code>if page.headers['Content-Type'] == 'text/html:
      .....
  elif page.headers['Content-Type'] == 'image/':
      ..... 
  </code></pre>
  
  <p>Thanks to LearnerEarner my problem is solved. Working code is below</p>
  
  <pre><code>class LinkCreateView(CreateView):
      model = Link
      form_class = LinkForm
  
      def form_valid(self, form):
          r = requests.get(form.instance.url)
          image_io = io.BytesIO()
          hash = str(uuid.uuid1())
          try:
              if r.headers['Content-Type'].startswith("image/"):
                  i = Image.open(io.BytesIO(r.content)).convert('RGB')
                  crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65, 65), bleed=0.5, centering=(0.5, 0.5))
                  name = "{}.jpeg".format(hash)
                  crop_image.save(image_io, format="jpeg")
                  form.instance.img.save(name, ContentFile(image_io.getvalue()))
              elif r.headers['Content-Type'].startswith("text/html"):
                  tree = lxml.html.fromstring(r.content)
                  og = tree.xpath('/html/head/meta[@property="og:image"][1]/@content')
                  r = requests.get(og[0])
                  i = Image.open(io.BytesIO(r.content)).convert('RGB')
                  crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65, 65), bleed=0.5, centering=(0.5, 0.5))
                  name = "{}.jpeg".format(hash)
                  crop_image.save(image_io, format="jpeg")
                  form.instance.img.save(name, ContentFile(image_io.getvalue()))
          finally:
              image_io.close()
  
          f = form.save(commit=False)
          f.rank_score = 0.0
          f.submitter = self.request.user
          f.save()
  
          return super(CreateView, self).form_valid(form)
  </code></pre>